On the parent form, change the state of the CheckBox control to open and close the subform. On the child form, after clicking the close button, the parent form CheckBox control becomes unselected.
1. Method
Here we use the delegate event method to realize mutual access of forms.
2. Parent form (main form)
Place 5 CheckBox controls on the parent form. And assign them to the CheckBox[] array so that the code can be called in a loop.
CheckBox[] checkBox;public MainFormBERT() { InitializeComponent(); checkBox = new CheckBox[5]; checkBox[0] = this.checkBox1; checkBox[1] = this.checkBox2; checkBox[2] = this.checkBox3; checkBox[3] = this.checkBox4; checkBox[4] = this.checkBox5; }
Add events to the CheckBox control. And define the OpenOrCloseSubFormPPG(int i) method to open or close the subform. This method calls the event of the subform, and uses the corresponding RecvInfo(int number) method to set the CheckBox control to the unselected state:
subFormPPGTx[i].SendToParent += new SubFormPPG.SendFun(RecvInfo);
private void checkBox1_CheckedChanged(object sender, EventArgs e) { OpenOrCloseSubFormPPG(0); }private void checkBox2_CheckedChanged(object sender, EventArgs e) { OpenOrCloseSubFormPPG(1); }private void checkBox3_CheckedChanged(object sender, EventArgs e) { OpenOrCloseSubFormPPG(2); }private void checkBox4_CheckedChanged(object sender, EventArgs e) { OpenOrCloseSubFormPPG(3); }private void checkBox5_CheckedChanged(object sender, EventArgs e) { OpenOrCloseSubFormPPG(4); } SubFormPPG[] subFormPPGTx = { null, null, null, null, null};private void OpenOrCloseSubFormPPG(int i) { try { if (checkBox[i].Checked) { string formTitle, formName; if (i < 0 || i > 4) { throw new IndexOutOfRangeException("Channel is out of range"); } else if (i == 4) { formTitle = "ParallelWrite"; formName = "subFormPPG" + formTitle; } else { formTitle = "Tx" + i; formName = "subFormPPG" + formTitle; } if (subFormPPGTx[i] == null || subFormPPGTx[i].IsDisposed) { subFormPPGTx[i] = new SubFormPPG(); subFormPPGTx[i].Text = formTitle; subFormPPGTx[i].Name = formName; subFormPPGTx[i].Tag = i; subFormPPGTx[i].SendToParent += new SubFormPPG.SendFun(RecvInfo);//调用子窗体的事件 subFormPPGTx[i].Show(this); } else { subFormPPGTx[i].WindowState = FormWindowState.Normal; subFormPPGTx[i].Activate(); } } else { //subFormPPGTx[0].Close(); subFormPPGTx[i].Dispose(); subFormPPGTx[i] = null; } } catch (IndexOutOfRangeException ex) { MessageBox.Show(ex.Message); } catch (Exception ex) { MessageBox.Show(ex.Message); } }private void RecvInfo(int number) { this.checkBox[number].Checked = false; }
3. Subform
Add the event SendToParent. When the child form is closed, respond to the event and pass the information to the parent form.
public delegate void SendFun(int number); public event SendFun SendToParent; private void SubFormPPG_FormClosed(object sender, FormClosedEventArgs e) { if (SendToParent != null) { SendToParent((int)this.Tag); } }
The above is the content of self-study C#05 from 0--mutual access between forms. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!