C# 在C中的其他窗体中的复选框之间传递值#
我需要你的帮助。 我有两张表格,表格一和表格二。在表格1中我有一个复选框1,在表格2中我有一个复选框2。我想要的只是checkBox1在checkBox2中自动转换的值C# 在C中的其他窗体中的复选框之间传递值#,c#,checkbox,C#,Checkbox,我需要你的帮助。 我有两张表格,表格一和表格二。在表格1中我有一个复选框1,在表格2中我有一个复选框2。我想要的只是checkBox1在checkBox2中自动转换的值 提前感谢首先,在多表单申请中,不允许表单直接接触。然而,我可以想到一种方法,我在这里提出的考虑到你的是一个特殊的情况。因此,该方法可能会违反通常的最佳实践 这里是方法 将表单中的复选框公开。您可以通过在设计模式中单击复选框,然后在“属性”窗口的“修改器”下选择“公共”来完成此操作。此步骤使您的复选框可以从表单实例外部访问 在表格
提前感谢首先,在多表单申请中,不允许表单直接接触。然而,我可以想到一种方法,我在这里提出的考虑到你的是一个特殊的情况。因此,该方法可能会违反通常的最佳实践 这里是方法
((Form2)(Application.openForms["Form2"])).checkBox1.Checked = this.checkBox1.Checked;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
Form2 a = new Form2();
a.c = checkBox1.Checked;
a.ShowDialog();
}
}
表格2:
public partial class Form2 : Form
{
public bool c;
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
checkBox1.Checked = c;
}
}
MDI父级中Form2的所有实例都将反映对放置在表单上的CustomCheckBox控件Checked属性的任何更改。当然,当在任何MDI子窗体上放置CustomCheckBox时,只要设置适当的事件(如Form2),就会出现这种情况
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1 {
public partial class Form2 : Form {
public Form2() {
InitializeComponent();
CustomCheckBox.CheckChanged += (object sender, EventArgs e) => {
if (sender != m_customCheckBox) { m_customCheckBox.Checked = CustomCheckBox.GetCheck(); }
};
FormClosed += (object _sender, FormClosedEventArgs _e) => {
CustomCheckBox.CheckChanged -= (object __sender, EventArgs __e) => { };
};
}
};
};
////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1 {
public class CustomCheckBox : CheckBox {
static private bool? m_checked = null;
static private object m_synchRoot = new object();
static public event EventHandler CheckChanged;
public CustomCheckBox() {
if (HasCheckValue) {
// Do this BEFORE we set up the CheckChanged event so that
// we do not needlessly kick off the CustomCheckBox.CheckChanged
// event for any other existing CustomCheckBoxes (as they already
// have their Checked property properly set)...
Checked = CustomCheckBox.GetCheck();
}
this.CheckedChanged += new EventHandler(OnCheckedChanged);
}
protected void OnCheckedChanged(object sender, EventArgs e) {
if (CustomCheckBox.HasCheckValue && this.Checked == CustomCheckBox.GetCheck()) {
return;
} else {
CustomCheckBox.SetCheck(this.Checked);
if (CustomCheckBox.CheckChanged != null) {
CustomCheckBox.CheckChanged(sender, e);
}
}
}
static public bool HasCheckValue {
get {
lock (m_synchRoot) {
return m_checked.HasValue;
}
}
}
static public bool GetCheck() {
lock (m_synchRoot) {
return m_checked.Value;
}
}
static private void SetCheck(bool _check) {
lock (m_synchRoot) {
m_checked = _check;
}
}
};
};
这是Winforms还是WebForms?Form1和Form2是同时实例化的,还是一个表单调用另一个表单?+1用于阻止不良做法,但仍然提供了答案。也许不完全是您想要的,但我认为我的解决方案有点酷!