The previous article introduced the way a delegate event was implemented to invoke a control of a parent form by a subform. This article will implement a common entity object in the same way as parameter passing.
The code used in this article is also modified on the basis of the previous article.
1. Parent form
Innobert the pooled entity object as a parameter to the subform constructor. The code follows the "Modify this line" section.
if (subformppgtx[i] = = NULL | | subformppgtx[i]. isdisposed) { Subformppgtx[i] = new SUBFORMPPG (Innobert, I);//Modify this line 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 (); }
2. Subform
You first define a class field and then modify the subform's constructor, adding parameters to receive the entity objects that the parent form passes. This makes it possible to invoke the entity object Innobert in the subform.
Innolightbert Innolightbert;bool formstatus = false;private int id;public delegate void Sendfun (int number);p Ublic event S Endfun sendtoparent;public subformppg (innolightbert innobert, int ID) { InitializeComponent (); This.innolightbert = innobert;//Receive object this.id = ID;}
3. Application
The subform invokes the properties of the shared entity object.
private void Subformppg_load (object sender, EventArgs e) {this . Getppgsetting (this.id);} private void getppgsetting (int channel) { try { switch (channel) {case 0: This.comboBoxSwing.SelectedIndex = (int) innolightbert.tx1_swing; break; Case 1: this.comboBoxSwing.SelectedIndex = (int) innolightbert.tx2_swing; break; Case 2: this.comboBoxSwing.SelectedIndex = (int) innolightbert.tx3_swing; break; Case 3: this.comboBoxSwing.SelectedIndex = (int) innolightbert.tx4_swing; break; Default: return; } } catch (Exception ex) { MessageBox.Show (ex. Message);} }
The above is from 0 self-learning c#06--multi-form common object content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!