how can i find the checked state of radio buttons of one form from another form in c sharp .net 2010.
to use in situation like


IN FIRST FORM

if( otherform.radiobuttonname.checked=true)

{ ,,,,,do,,,}

)

Dani AI

Generated

's direct-instance approach is correct and the simplest when both forms share the same object reference. A common pitfall is accidentally creating a new instance of the target form (which has fresh control states) instead of referencing the already-open instance. The WinForms designer makes controls private by default, so exposing a public read-only property or using an event is usually better than making controls public.

An alternative, decoupled pattern is to have the child form raise an event when its selection changes and let the parent subscribe. Example publisher/subscriber sketch:

// Form2 (publisher)
public event Action<bool> SelectionChanged;

private void AnyRadio_CheckedChanged(object sender, EventArgs e)
{
    var rb = sender as RadioButton;
    if (rb != null && rb.Checked)
        SelectionChanged?.Invoke(true);
}

// Form1 (subscriber)
var f2 = new Form2();
f2.SelectionChanged += isChecked => MessageBox.Show(isChecked ? "Checked" : "Not checked");
f2.Show();

Practical tips: use ShowDialog plus a read-only SelectedValue property for modal workflows; use Application.OpenForms.OfType<T>().FirstOrDefault() when a reference was not preserved (careful with multiple instances); always check for null before accessing another form; avoid static UI state; and remember WinForms controls must be accessed on the UI thread (use Invoke/BeginInvoke when crossing threads). These patterns keep code safer and easier to maintain than directly poking at another form's controls.

Recommended Answers

All 3 Replies

I will show you how to do in both ways, becuase you didnt exactly specify in which direction you want to get a radio button check state.
So from from1 to check the radioButton on form2, and from form2 to check th radioButton on form1:

//FORM1
    public partial class Form1 : Form
    {
        Form2 f2;
        public Form1()
        {
            InitializeComponent();
        }

        public bool GetRadioButtonState_Form1()
        {
            return radioButton1.Checked ? true : false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            f2 = new Form2(this);
            f2.Show();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //check for radio button on Form2:
            if (f2 != null)
            {
                bool bChecking = f2.GetRadioButtonState_Form2();
                MessageBox.Show("Radio button on Form1 is " + (bChecking ? "CHECKED!" : "NOT CHECKED!").ToString());
            }
            else
                MessageBox.Show("First open form2...");
        }
    }

//FORM2:
    public partial class Form2 : Form
    {
        Form1 f1;
        public Form2(Form1 _f1)
        {
            InitializeComponent();
            this.f1 = _f1;
        }

        public bool GetRadioButtonState_Form2()
        {
            return radioButton1.Checked ? true : false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Check for radio button on Form1:
            bool bChecking = f1.GetRadioButtonState_Form1();
            MessageBox.Show("Radio button on Form1 is " + (bChecking ? "CHECKED!" : "NOT CHECKED!").ToString());
        }
    }

really...thanks a lllotttt :)

You can mark the thread as answered, if you are satisfied with the asnwer, or add some at vote as helpful.
bye, bye

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.