Bind a business object to a form or control Container Using Reflection in winform

Source: Internet
Author: User

In webform, you can use reflection to bind a business object to an ASP. NET form control. For more information about the winform project, seeCodeImplement the same function.

Winform does not provide the findcontrol method similar to webform, so I wrote a method similar to webform in the way of traversing the control, considering that many of the winform controls are placed in label and tabcontrol, the method is recursive.

The controls of winform and winform are also different. For example, in winform, datetimepicker uses the Value Attribute and SelectDate attribute in webform. In winform, The numericupdown control is available, the value also uses the value attribute. Therefore, the binding method is also different from webform.

//// The code is as follows:
Using system;
Using system. Windows. forms;
Using system. reflection;
Using system. collections;

Namespace bindtest
{

Public sealed class formbinding
{
/// <Summary>
/// Bind a business object to a form or control container
/// </Summary>
/// <Param name = "OBJ"> Business Object </param>
/// <Param name = "Container"> form or control container </param>
Public static void bindobjecttocontrols (Object OBJ, control container)
{
If (OBJ = NULL) return;

Type objtype = obj. GetType ();
Propertyinfo [] objpropertiesarray = objtype. getproperties ();

Foreach (propertyinfo objproperty in objpropertiesarray)
{
Control control = findcontrol (container, objproperty. Name );
If (control = NULL) continue;

If (control is datetimepicker)
{
Datetimepicker = (datetimepicker) control;
Datetimepicker. value = (datetime) objproperty. getvalue (OBJ, null );
}
Else
{
// Obtain control attributes
Type controltype = control. GetType ();
Propertyinfo [] controlpropertiesarray = controltype. getproperties ();

// Common attributes
Bool success = false;
Success = findandsetcontrolproperty (OBJ, objproperty, control, controlpropertiesarray, "checked", typeof (bool ));

If (! Success)
Success = findandsetcontrolproperty (OBJ, objproperty, control, controlpropertiesarray, "value", typeof (string ));

If (! Success)
Success = findandsetcontrolproperty (OBJ, objproperty, control, controlpropertiesarray, "text", typeof (string ));

If (! Success)
Success = findandsetcontrolproperty (OBJ, objproperty, control, controlpropertiesarray, "selectedvalue", typeof (string ));

}
}
}


/// <Summary>
/// Find the control in the container based on the control name. Consider that some controls are placed in the container of the form and recursive search is used.
/// </Summary>
/// <Param name = "Container"> Control container </param>
/// <Param name = "controlname"> Control name </param>
/// <Returns> </returns>
Private Static Control findcontrol (control container, string controlname)
{
Control findcontrol = NULL;
Foreach (Control in container. Controls)
{
If (control. Controls. Count = 0)
{
If (control. Name = controlname)
{
Findcontrol = control;
Break;
}
}
Else
{
Findcontrol = findcontrol (control, controlname );
}
}
Return findcontrol;
}

/// <Summary>
/// Set the Control Value
/// </Summary>
/// <Param name = "OBJ"> </param>
/// <Param name = "objproperty"> </param>
/// <Param name = "control"> </param>
/// <Param name = "controlpropertiesarray"> </param>
/// <Param name = "propertyname"> </param>
/// <Param name = "type"> </param>
/// <Returns> </returns>
Private Static bool findandsetcontrolproperty (Object OBJ, propertyinfo objproperty, control Control, propertyinfo [] controlpropertiesarray, string propertyname, type)
{
Foreach (propertyinfo controlproperty in controlpropertiesarray)
{
If (controlproperty. Name = propertyname & controlproperty. propertytype = type)
{
Controlproperty. setvalue (control, convert. changetype (objproperty. getvalue (OBJ, null), type), null );
Return true;
}
}
Return false;
}

Public static void bindcontrolstoobject (Object OBJ, control container)
{
If (OBJ = NULL) return;

// Obtain the attributes of the Business Object
Type objtype = obj. GetType ();
Propertyinfo [] objpropertiesarray = objtype. getproperties ();

foreach (propertyinfo objproperty in objpropertiesarray)
{

control = findcontrol (container, objproperty. name);
If (control = NULL) continue;
If (control is datetimepicker)
{< br> datetimepicker = (datetimepicker) control;
objproperty. setvalue (OBJ, convert. changetype (datetimepicker. value, objproperty. propertytype), null);

}< br> else
{< br> type controltype = control. getType ();
propertyinfo [] controlpropertiesarray = controltype. getproperties ();

Bool success = false;
Success = findandgetcontrolproperty (OBJ, objproperty, control, controlpropertiesarray, "checked", typeof (bool ));

If (! Success)
Success = findandgetcontrolproperty (OBJ, objproperty, control, controlpropertiesarray, "value", typeof (string ));

If (! Success)
success = findandgetcontrolproperty (OBJ, objproperty, control, controlpropertiesarray, "text", typeof (string);

If (! Success)
Success = findandgetcontrolproperty (OBJ, objproperty, control, controlpropertiesarray, "selectedvalue", typeof (string ));
}
}
}

Private Static bool findandgetcontrolproperty (Object OBJ, propertyinfo objproperty, control Control, propertyinfo [] controlpropertiesarray, string propertyname, type)
{
// Iterate the entire control attribute
Foreach (propertyinfo controlproperty in controlpropertiesarray)
{
// Check the matched name and type
If (controlproperty. Name = "text" & controlproperty. propertytype = typeof (string ))
{
// Set the property of the control
// Business Object Property Value
Try
{
Objproperty. setvalue (OBJ, convert. changetype (controlproperty. getvalue (control, null), objproperty. propertytype), null );
Return true;
}
Catch
{
// The widget from the form cannot be
// Convert the data
// Objproperty. propertytype
Return false;
}
}
}
Return true;
}

}
}

// Usage:
// Business object:
Public class model
{
Public Model ()
{
}

Private string test1;
Private datetime Test2;
Private string test3;

Public String test1
{
Set {test1 = value ;}
Get {return test1 ;}
}

Public datetime Test2
{
Set {Test2 = value ;}
Get {return Test2 ;}
}

Public String test3
{
Set {test3 = value ;}
Get {return test3 ;}
}
}

In a winform, put two textbox controls, one datetimepicker control, and one panel control, named test1, Test2, and test3 respectively. Put the text3 control in panel1.
Bind the business object to the form:
Model = new model ();
Model. test1 = "Hello, world! ";
Model. Test2 = datetime. Now. addmonths (-2 );
Model. test3 = "nice to meet u! ";
Formbinding. bindobjecttocontrols (model, this );

Bind a form to a business object:
Model = new model ();
Formbinding. bindcontrolstoobject (model, this );
MessageBox. Show (model. test1 + "" + model. test2.to1_datestring () + "" + model. test3 );

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.