简体   繁体   English

复选框。选中的其他表单上的更新

[英]checkbox.Checked dosent update on other forms

I have 2 forms, UserInterface and Client I'm passing checkbox2.Checked info to Client but it only works however it was at launch. 我有2个表单, UserInterfaceClient我正在传递checkbox2.Checked信息给Client但是它仅在启动时起作用。 When I tick or untick and close and reopen Client it wont notice the change. 当我勾选或取消勾选并关闭并重新打开Client它不会注意到更改。

Modifiers is Public on checkbox2 at UserInterface form. ModifiersUserInterface表单的checkbox2上为Public。

Here is Client code: 这是Client代码:

public partial class Client : Form
{
    private UserInterface ui1;

    public Client()
    {
        InitializeComponent();
    }
    public void CheckBoxCheck()
    {
        if (ui1.checkBox2.Checked == true)
        {
            MessageBox.Show("true");
        }
        else
        {
            MessageBox.Show("false");
        }
    }
}

If the checkbox is ticked at launch Client will show "true" but if I click it (untick) and run Client it will still show "true". 如果在启动时选中了该复选框,则Client将显示“ true”,但是如果我单击(取消选中)并运行Client它仍将显示“ true”。

What do I need to add or modify so checkbox2 will be updated in realtime. 我需要添加或修改什么,以便checkbox2会实时更新。 Thank you. 谢谢。

Note: I'm pretty new with coding, explanations are appreciated. 注意:我对编码还很陌生,请多加解释。

Your Forms should be connected. 您的表格应已连接。 It looks like ui1 is a different instance of UserInterface form. 看来ui1是UserInterface表单的另一个实例。

There are different approaches to pass the data between forms and it depends on your demands. 有多种方法可以在表单之间传递数据,这取决于您的需求。

For instance you could create UserInterface form inside of Client. 例如,您可以在客户端内部创建UserInterface表单。 And use the Show() method to show it. 并使用Show()方法显示它。

You should probably be making use of the Checkbox.Checked event inside UserInterface class and then fire a custom event that your Client can subscribe to. 您可能应该利用UserInterface类中的Checkbox.Checked事件,然后触发客户可以订阅的自定义事件。

public event EventHandler<EventArgs> CheckboxCheckedChanged;
protected void OnCheckboxCheckedChanged(EventArgs e)
{
    if (CheckboxCheckedChanged != null)
        CheckboxCheckedChanged(this, e);
}

private void checkbox2_CheckedChanged(object sender, EventArgs e)
{
    OnCheckboxCheckedChanged(e);
}

And then in Client: 然后在客户端中:

ui1.CheckboxCheckedChanged += ui1_CheckboxCheckedChanged;
private void ui1_CheckboxCheckedChanged(object sender, CheckBoxEventArgs e)
{
    // Your code here
}

I'll be building on noMad17's answer, you have to subscribe to your CheckBox event in your UserInterface form. 我将以noMad17的答案为基础,您必须以UserInterface表单订阅CheckBox事件。 But the change is that now we will send the CheckBox that was clicked in the event. 但是更改是,现在我们将发送在事件中单击的CheckBox。 So this code is for your UserInterface form: 因此,此代码适用于您的UserInterface表单:

public event EventHandler SomeEvent;
protected void OnSomeEvent(object sender, EventArgs e)
{
    EventHandler eh = SomeEvent;
    if(eh != null)
    {
        eh(sender, e);
    }
}

private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
    OnSomeEvent(sender, e);
}

Now for the Client, it needs to know what a UserInterface is so we have to pass UserInterface to the Client in the constructor, otherwise it won't initialize. 现在,对于Client,它需要知道UserInterface是什么,因此我们必须将UserInterface传递给构造函数中的Client,否则它将不会初始化。 Also here we are gonna work out the CheckBox event that the parent form is gonna give us. 同样在这里,我们将计算出父窗体将给我们的CheckBox事件。 And in the end we have to unsubscribe the event. 最后,我们必须退订该事件。 So this code is for your Client: 因此,此代码适用于您的客户:

public partial class Client : Form
{
    private UserInterface ui1;

    public Client(UserInterface ui1)
    {
        InitializeComponent();
        this.ui1 = ui1;
        ui1.SomeEvent += UI1_SomeEvent;
    }

    private void UI1_SomeEvent(object sender, EventArgs e)
    {
        //Your code...
        CheckBox c = sender as CheckBox;
        if(c.Checked == true)
        {
            MessageBox.Show("true");
        }
        else
        {
            MessageBox.Show("false");
        }
    }

    private void Client_FormClosing(object sender, FormClosingEventArgs e)
    {
        ui1.SomeEvent -= UI1_SomeEvent;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM