我有两种形式:
Form1
Form2
每当我在
checkBox1
上选中/取消选中CheckBox 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;
}
}
}
关于c# - 通过在form2中的命令来锁定和解锁form1上的文本框,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23514083/