Problem in validating Form1 Controls in Form 2

S

Suma

Hi,
i have a problem with the controls .i have to validate Form1 Radio
button option is checked or Not in the Form2. even after checking the
radio button also in FOrm2 it is always taking as "False" only.

please see the following code where i did wrong.
I ahve created A Visual C# .Net project. for that i ahve added Form2
now my project contains Form1 and Form2.

In Form1 i have placed Control "RadioButton1"

Follwing code i have placed in the FOrm1.

Form2 f2 = new Form2();
private void Form1_Load(object sender, EventArgs e)
{
f2.Show(); (Displays Form2)


}

private void radioButton1_CheckedChanged(object sender,
EventArgs e) (when radio button option ia checked or unchecked Form2
function will be called)
{
f2.function1();
}


In Form2 No controls are avialable.
following function i have implemented in the Form2


public void function()
{

Form1 f1 = new Form1();
MessageBox.Show("came inside"); //To know when the Radio
button is checked or un checked

if (f1.radioButton1.Checked == true)
{
MessageBox.Show("Checked");

}



But in the above My problem is Even After "Checking the radio
button(Enabled) option" ((f2.radioButton1.Checked == true) is always
showing as false when i put the break point.
it is not updating the checkoption valus to "true" even after checking
the radio button.

please help on this. i am not getting solution for this.

Thanks in Advance.
If i have written a silly question plase forgive me. i am new to
development.

Thanks in Advance
Suma
 
D

DeveloperX

The issue is you have created a new form1 in function() when you
actually need a reference to the original form1. A simple solution is
to change function() to be function(bool pState) and pass through the
state of the radio button.

A little warning though, the way you are going about this will leave
you with all sorts of issues if the GUI becomes complex. A better way
to handle this would be to create a new class which holds your state
information and which can raise events when things occur, for example
when the radio button on form1 changes state.
Add a new new constructor to form2 which takes your state object as a
parameter and in the constructor wire up the events on form2.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top