开发者

How do I refrence form1 from form2 without causing an infinite loop?

In my main form (form1) I have checkboxes that when checked should also check the corresponding box in form2. I also want if checkboxes in form2 are checked they check the corresponding boxes on form1. The problem that I believe I am encountering is that form1 can make an object of form2 to reference, however if I instantiate an object of form1 within form2 I believe it creates an infinite loop? Any help figuring this out is appreciated.

Form1 creates an object of form2:

    Form2 formSettings = new Form2();

Now when I have an event I can update form2:

      public void logScanResultsToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (logScanResultsToolStripMenuItem.Checked)
        {
            formSettings.chbxLogScanResults.Checked = true;

        }
        else
        {
            formSettings.chbxLogScanResults.Checked = false;

        }        
    }         

But if I try to do something similar in Form2:

   Form1 form1 = new Form1();

So that I can reference form1's menu item from within fo开发者_开发问答rm2(formSettings) I end up creating an object (form1) that calls to make an object of Form1, which within Form1 includes a call to create an object of Form2 and thus an endless loop.


You shouldn't create an instance every time a checkbox is checked off. You need to maintain the instances alive and hide/show them as needed. Also, the constructor of one of the forms should receive the other one as parameter in its constructor so they can reference each other.


Hopefully this is clear enough. It isn't a straight out answer as you really dont have much detail in your question.

Basically you have two forms, Form1 and Form2, which will be throwing events (OnChangeEvent?) on the change of some checkboxes.

Form1 listens for events from Form2 and Form2 does the same from Form1.

If Form1's event listen receives a OnChangeEvent and changes its checkbox then it should raise an OnChangeEvent. If on the other hand it doesn't change its checkbox (as it already has the correct value) then it should not raise an OnChangeEvent.


In the body of Form1 you need to declare Form2 to hold an instance of it for referencing and to open it. When you call the Form2.Show method from Form1, you will pass a reference of itself to Form2 which you then can use to gain access back to Form1.

public partial class Form1 : Form
{
    Form2 form2 = new Form2();

    public Form1()
    {
        InitializeComponent();
    }

    private void form1Button_Click(object sender, EventArgs e)
    {
        form2.Show(this);
    }

    private void form1CheckBox_CheckedChanged(object sender, EventArgs e)
    {
        form2.ChangeCheck(form1CheckBox.Checked);
    }

    public void ChangeCheck(bool isItChecked)
    {
        form1CheckBox.Checked = isItChecked;
    }
}

In Form2 you can now reference Form1 as the owner.

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    private void form2CheckBox_CheckedChanged(object sender, EventArgs e)
    {
        ((Form1)this.Owner).ChangeCheck(form2CheckBox.Checked);
    }

    public void ChangeCheck(bool isItChecked)
    {
        form2CheckBox.Checked = isItChecked;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜