in the previous blog, the use of the method of writing a third-party control, implemented to the form control Text property assignment, such as: TextBox , Combox and so on. There are assignments, and of course there are value operations. From the control of the form to take a value, and then into the variable or entity properties, passed to the data access layer to add, update, and so on, we often use. How do you implement a key value?
the method used has been explained in detail in the previous blog post, and it is no longer The statement of exhaustion up.
The code is written here:
The function of this method is to iterate over the controls in the incoming form and get the corresponding properties whose values are assigned to the entity class.
////<summary>///From the form control to populate the student entity class in///</summary> <param name= "Enstudent" > Student entities </param> public static void Fillstuentitybyform (Stuentity enstudent, Form thisfrm) {//traverse the control on the form foreach (Control ctrl in THISFRM. Controls) {//control inherits the interface if (Ctrl is Interface1) { Whether it belongs to the text box if (Ctrl is Textboxex) {//Assign a value to the Entity property Setstuvalue (Enstudent, ((TEXTBOXEX) ctrl). DataField, CTRL. Text); }//if it belongs to the drop-down box else if (Ctrl is Comboxex) {Setstuvalue (enstudent , ((COMBOXEX) ctrl). DataField, ((COMBOXEX) ctrl). Text); } } } }
Use reflection to assign values to each property of an entity:
<summary>////////</summary>// <param name= "enstudent" > Student entity Class </ param>// <param name= "datafiled" > Matching string </param>// <param name= "value" > Value of Form Control </ param> public static void Setstuvalue (Stuentity enstudent, String datafiled, String Value) { PropertyInfo field = Enstudent.gettype (). GetProperty (datafiled); Field. SetValue (enstudent, Value, null); }
Takes the property of the returned entity out and spells it into a string.
<summary>//// combine attributes of entities into strings to return///</summary>// <param name= "Enstudent" ></ param> //<returns></returns> public static string Getstr (Stuentity enstudent) { propertyinfo[] fields = Enstudent.gettype (). GetProperties (); StringBuilder sb = new StringBuilder (); foreach (PropertyInfo pi in fields) { sb. Append (pi. Name). Append (":"). Append (pi. GetValue (enstudent, NULL)). Append ("\ r \ n"); } Return SB. ToString (); }
Client calling Code:
private void Btngetvalue_click (object sender, EventArgs e) { stuentity enstudent = new stuentity (); Call the function Frmhelper.fillstuentitybyform (enstudent, this) that fills the entity's properties; Displays the properties of the entity as a string str = FRMHELPER.GETSTR (enstudent); MessageBox.Show (str); }
Display effect:
The implementation process is like this, there is nothing wrong place to welcome the point!
Oノo════════════════════════════╲
│ヽ. In programming learning. Welcome to the Exchange. │
│ http://blog.csdn.net/u010028869 .ヽ│
╲══════════════════════════ ═══ヾ
C # Write a third-party control that implements a key value for a form control