我有两个表格:
Form1
Form2
每当我选中/取消选中复选框时 checkBox1在 Form2我要更新 textbox1.Readonly那是在 Form1 .如果两者 textbox1和 checkbox1一直在相同的表格上,这很容易:
private void checkBox1_CheckedChanged(object sender, EventArgs e) {
textbox1.Readonly = checkBox1.Checked;
}
当textbox1时,我该怎么办?和 checkbox1在不同 Forms ?
请您参考如下方法:
你可以这样说:
public partial class Form1: Form {
...
// textBox1 is private (we can't access in from Form2)
// so we'd rather create a public property
// in order to have an access to textBox1.Readonly
public Boolean IsLocked {
get {
return textBox1.Readonly;
}
set {
textBox1.Readonly = value;
}
}
}
...
public partial class Form2: Form {
...
private void checkBox1_CheckedChanged(object sender, EventArgs e) {
// When checkBox1 checked state changed,
// let's find out all Form1 instances and update their IsLocked state
foreach (Form fm in Application.OpenForms) {
Form1 f = fm as Form1;
if (!Object.RefrenceEquals(f, null))
f.IsLocked = checkBox1.Checked;
}
}
}
