Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在用户控件之间传递复选框值_C#_.net_Winforms - Fatal编程技术网

C# 在用户控件之间传递复选框值

C# 在用户控件之间传递复选框值,c#,.net,winforms,C#,.net,Winforms,我正在尝试将复选框值从UserControl3传递给UserControl1 关于UserControl3 public void materialCheckBox1_CheckedChanged(object sender, EventArgs e) { if (materialCheckBox1.Checked) { Environment.Exit(0) } else { //Nothing } } 如

我正在尝试将复选框值从UserControl3传递给UserControl1

关于UserControl3

public void materialCheckBox1_CheckedChanged(object sender, EventArgs e)
{

    if (materialCheckBox1.Checked)
    {
        Environment.Exit(0)
    }
    else
    {
        //Nothing
    }


}
如何将该值添加到UserControl1

例如,单击UserControl1上的按钮将检查UserControl3上是否选中了复选框。

与代表一起工作


要阅读更多关于该点击!有关更多信息,请查看此msdn。

控件之间的通信有多种解决方案


您已经在控件之间的交互中看到了此类功能,如
BindingNavigator
BindingSource
,其中
BindingNavigator
具有类型为
BindingSource
的属性,每次单击导航按钮,
BindingNavigator
调用
BindingSource
的方法

为了自己实现它,例如,在
UserControl2
中,您可以创建一个公共属性,公开您希望
UserControl1
能够检查的信息,然后在
UserControl1
中,您应该有一个
UserControl2
类型的属性。这样,当您在设计时或运行时将
UserControl2
的实例分配给属性时,就可以使用公开的信息

例如,请遵循以下步骤:

1) 在您的
UserControl2
中,公开您需要在无法控制的情况下使用的信息

public bool CheckBoxValue 
{
    get { return checkBox1.Checked; }
    set { checkBox1.Checked = value; }
}
2) 在
UserControl1
中,创建类型为
UserControl2
的属性。因此,您可以使用分配给它的实例,并找到
CheckBoxValue
属性的值

public UserControl2 UserControl2Instance { get; set; }

private void button1_Click(object sender, EventArgs e)
{
    if(UserControl2Instance!=null)
    {
        if(UserControl2Instance.CheckBoxValue)
            MessageBox.Show("Checked");
        else
            MessageBox.Show("Unchecked");
    }
}

3) 将
UserControl1
UserControl2
都放到表单上,并使用设计器(或在运行时)将
UserControl2
的实例分配给
UserControl1
UserControl2Instance
属性。然后,当您运行程序并单击您的
UserControl1
按钮1
时,您可以看到
checkBox1
的值,该值位于
UserControl2

中您先前在中回答的类似问题。为什么不试试建议的解决方案呢?那就是文本框对文本框。这确实适用于这个问题。但是这是如何转化为布尔值的。同样,该解决方案不适用于表单(非parent)到用户控件。您是否见过
BindingNavigator
如何与
BindingSource
结合使用?尝试使用这样的解决方案。您已经在控件之间的交互中看到了这样的功能,例如
BindingNavigator
BindingSource
,其中
BindingNavigator
具有类型为
BindingSource
的属性,每次单击导航按钮,
BindingNavigator
调用
BindingSource
的方法。