Home > Net >  Custom controls automatic data binding and form scale size
Custom controls automatic data binding and form scale size

Time:10-19

Style figure as follows (OK to have a BAD layer below the layer) :


1. Custom control code, this control includes the control of automatic scaling reference (https://blog.csdn.net/wwwxrlbjcom/article/details/80568041)
 
Public partial class ucResultItem: UserControl
{
Public ucResultItem ()
{
InitializeComponent ();
}

[the Category (" custom property "), the Description (" GroupText ")]
Public string GroupText
{
The get {return enclosing groupBox. Text; }
The set {this. GroupBox. Text=value; }
}

[the Category (" custom property "), the Description (" DisPrecision ")]
Public int DisPrecision
{
The get=& gt; DisPrecision;

Set
{
If (value> 0 & amp; & The value & lt;
=5){
DisPrecision=value;
PrecisionStr="0."
PrecisionStr=precisionStr. PadRight (disPrecision + 2, '#');
}

}
}

Private int disPrecision=3;
Private string precisionStr="0. # # #";


Public void Binding (ResultItem result)
{
Lb_Value. DataBindings. Add (" Text ", result, GetPropName (RST=& gt; Result. The Value));
Cb_Enable. DataBindings. Add (" Checked ", result, GetPropName (RST=& gt; Result. CmpEnable));
Tb_UpValue. DataBindings. Add (" Text ", result, GetPropName (RST=& gt; Result. UpValue));
Tb_LowValue. DataBindings. Add (" Text ", result, GetPropName (RST=& gt; Result. LowValue));
Lb_OK. DataBindings. Add (" Visible ", result, GetPropName (RST=& gt; Result. The result));

//here DataBindings result can be used inside interface implementation
}
String GetPropName (Expression Expr)
{
The switch (expr. Body. NodeType)
{
Case ExpressionType. MemberAccess:
Return ((MemberExpression expr). The Body). Member. The Name;
Case ExpressionType. Convert:
Return ((MemberExpression) (the) (UnaryExpression expr) Body). The Operand). Member. The Name;
Default:
return null;
}
}


Private void lb_Value_TextChanged (object sender, EventArgs e)
{
Lb_Value. Text=the Convert. ToDouble (lb_Value. Text). ToString (precisionStr);
}

Private float x=140;
Private float y=160;

///& lt; Summary>
Item///will control the width and height, the left and top margins and font size staging to tag attribute
///& lt;/summary>
///& lt; Param name="cons" & gt; Recursive control of control items & lt;/param>
Private void SetTag (Control cons)
{
Foreach (Control con in cons. Controls)
{
Con. The Tag=con. Width + ":" + con. Height + ":" + con. Left + ":" + con. Top + ":" + con. The Font. The Size;
If (con. Controls. Count & gt; 0)
SetTag (con);
}
}
Private void SetControls (float newx, float newy, Control cons)
{
//traverse control in the form item, reset the control item of the value of the
Foreach (Control con in cons. Controls)
{
String [] mytag=con. The Tag. The ToString (). The Split (new char [] {} ', ');//to get control of the Tag attribute values, and storage after segmentation character string array
Float a=System. The Convert. ToSingle (mytag [0]) * newx;//according to form scale control item of the value of the width of the
Con. Width=(int) a;//width
A=System. The Convert. ToSingle (mytag [1]) * newy;//height
Con. Height=(int) (a);
A=System. The Convert. ToSingle (mytag [2]) * newx;//the left distance
Con. Left=(int) (a);
A=System. The Convert. ToSingle (mytag [3]) * newy;//on the edge distance
Con. Top=(int) (a);
Single currentSize=System. The Convert. ToSingle (mytag [4]) * newy;//font size
Con. The Font=new Font (con. The Font. The Name, currentSize, con. The Font, Style, con. The Font. The Unit).
If (con. Controls. Count & gt; 0)
{
SetControls (newx, newy, con);
}
}
}


Private void ucResultItem_Resize (object sender, EventArgs e)
{
Float newx=(. This Width)/x;//window width scaling
Float newy=(. This Height)/y;//form highly scaling
SetControls (newx, newy, this);//change with the form control item size
}

///& lt; Summary>
///fixed aspect ratio
///& lt;/summary>
///& lt; Param name="sender" & gt;
///& lt; Param name="e" & gt;
Private void ucResultItem_SizeChanged (object sender, EventArgs e)
{
This. Width=this. Height * 7/8;
}

Private void ucResultItem_Load (object sender, EventArgs e)
{
This. The Resize +=new EventHandler (ucResultItem_Resize);
Enclosing SizeChanged +=new EventHandler (ucResultItem_SizeChanged);
SetTag (this);//call the method
UcResultItem_Resize (new object (), new EventArgs ());
}

}

Definition 2. Need to bind data entity, the entity to assign a value to trigger properties change
 
[Serializable]
Public class ResultItem: BaseResult
{
Private double value=https://bbs.csdn.net/topics/default (double);
Private double upValue=https://bbs.csdn.net/topics/default (double);
Private double lowValue=https://bbs.csdn.net/topics/default (double);
Private bool result=default (bool);
Private bool cmpEnable=default (bool);


Public double Value
{
The get=& gt; The value;
Set
{
If (this. The value!=value)
{
This value=https://bbs.csdn.net/topics/value;
OnPropertyChanged (" Value ");
}
}
}


nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C#
  • Related