On the parent form, change the state of the CheckBox control to enable the subform to open and close. On the subform, when you click the Close button, the parent Form CheckBox control becomes unchecked.
1. Methods
The method of delegating events is used here to achieve mutual access of forms.
2. Parent form (main form)
A 5 CheckBox control is placed 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; }
Adds an event to the CheckBox control. and define the OPENORCLOSESUBFORMPPG (int i) method implementation to open or close the subform. The method invokes the subform's event, setting the CheckBox control to unchecked, with the corresponding recvinfo (int number) method:
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};p rivate void openorclosesubformppg (int i) { try {if (Checkbox[i]. Checked) {string formtitle, formName; if (I < 0 | | i > 4) {throw new IndexOutOfRangeException ("Channel is out O F 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);//Invoke the subform's event 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
Adds an event sendtoparent, which responds to an event when the subform is closed, and passes the information to the parent form.
public delegate void Sendfun (int number);p ublic event sendfun sendtoparent;private void Subformppg_formclosed (object sender, Formclosedeventargs e) { if (sendtoparent! = null) { sendtoparent ((int) this. TAG); } }
The above is from the 0 self-learning c#05--form of mutual access to content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!