Search code examples
c#winformswindows-forms-designer

displaying my selected radio button in textbox from another form


How do I display the selected item from my form2 groupbox radiobutton (3 radiobuttons and i can only display one) inside my Form1 texbox.

This is my problem as I only know the code to display 1. if statments didn't work.

Form1:

public void button5_Click(object sender, EventArgs e)
{    
    Form2 fr2 = new Form2();
    textBox1.Text = fr2.receivet(textBox1.Text, fr2.radioButton1);
    
}

Form2:

internal string receivet(string textBox1, RadioButton radio)
{
    return textBox1 = radio.Text;
}

Solution

  • No need to create method like receivet, you can directly get RadioButton from Form object

    public void button5_Click(object sender, EventArgs e)
    {    
        Form2 fr2 = new Form2();
        string radioTxt = ""
        if (fr2.RadioButton1.Checked) 
        {
             radioTxt = fr2.RadioButton1.Text
        }
        else if (fr2.RadioButton2.Checked) 
        {
             radioTxt = fr2.RadioButton2.Text
        }
        else if (fr2.RadioButton3.Checked) 
        {
             radioTxt = fr2.RadioButton3.Text
        }
    
        textBox1.Text = radioTxt;
    
    }