如何从 form2 引用 form1 而不导致无限循环?

发布于 2024-12-05 10:45:15 字数 893 浏览 1 评论 0原文

在我的主窗体(form1)中,我有一些复选框,选中这些复选框时还应该选中 form2 中的相应框。我还希望如果选中 form2 中的复选框,它们会选中 form1 上的相应框。我相信我遇到的问题是 form1 可以引用 form2 的对象,但是如果我在 form2 中实例化 form1 的对象,我相信它会创建一个无限循环?任何帮助解决这个问题的帮助都会受到赞赏。

Form1 创建 form2 的对象:

    Form2 formSettings = new Form2();

现在,当我有一个事件时,我可以更新 form2:

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

        }
        else
        {
            formSettings.chbxLogScanResults.Checked = false;

        }        
    }         

但如果我尝试在 Form2 中执行类似的操作:

   Form1 form1 = new Form1();

这样我就可以从 form2(formSettings) 中引用 form1 的菜单项,我最终会创建一个对象 ( form1) 调用创建 Form1 的对象,在 Form1 中包含创建 Form2 对象的调用,从而形成无限循环。

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 form2(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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情泪▽动烟 2024-12-12 10:45:15

您不应该在每次选中复选框时都创建一个实例。您需要保持实例处于活动状态并根据需要隐藏/显示它们。此外,其中一种形式的构造函数应该在其构造函数中接收另一种形式作为参数,以便它们可以相互引用。

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.

岁月打碎记忆 2024-12-12 10:45:15

希望这已经足够清楚了。这不是一个直接的答案,因为您的问题确实没有太多细节。

基本上你有两个表单,Form1 和 Form2,它们将在某些复选框的更改时抛出事件(OnChangeEvent?)。

Form1 侦听来自 Form2 的事件,Form2 也对 Form1 执行相同的操作。

如果 Form1 的事件侦听接收到 OnChangeEvent 并更改其复选框,那么它应该引发 OnChangeEvent。另一方面,如果它没有更改其复选框(因为它已经具有正确的值),那么它不应该引发 OnChangeEvent。

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.

枯寂 2024-12-12 10:45:15

在 Form1 的主体中,您需要声明 Form2 来保存它的一个实例以供引用并打开它。当您从 Form1 调用 Form2.Show 方法时,您会将其自身的引用传递给 Form2,然后您可以使用该引用来重新访问 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;
    }
}

在 Form2 中,您现在可以将 Form1 作为所有者引用。

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

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文