Home > OS >  displaying my selected radio button in textbox from another form
displaying my selected radio button in textbox from another form

Time:10-24

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;
}

CodePudding user response:

In your code you create a new Form2 instance and take its value. You need to call the shown Form2 instance's function (and set it to public modifier).

CodePudding user response:

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;

}
  • Related