我有两种形式:

  Form1
  Form2


每当我在checkBox1上选中/取消选中CheckBox Form2时,我都想更新textbox1.Readonly上的Form1。如果textbox1checkbox1都使用相同的表格,那将很容易:

  private void checkBox1_CheckedChanged(object sender, EventArgs e) {
    textbox1.Readonly = checkBox1.Checked;
  }


如果textbox1checkbox1在不同的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/

10-13 04:59