110,499
社区成员
发帖
与我相关
我的任务
分享
public partial class ucResultItem : UserControl
{
public ucResultItem()
{
InitializeComponent();
}
[Category("自定义属性"), Description("GroupText")]
public string GroupText
{
get { return this.groupBox.Text; }
set { this.groupBox.Text = value; }
}
[Category("自定义属性"), Description("DisPrecision")]
public int DisPrecision
{
get => disPrecision;
set
{
if(value>0 && value <=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<ResultItem>(rst => result.Value));
cb_Enable.DataBindings.Add("Checked", result, GetPropName<ResultItem>(rst => result.CmpEnable));
tb_UpValue.DataBindings.Add("Text", result, GetPropName<ResultItem>(rst => result.UpValue));
tb_LowValue.DataBindings.Add("Text", result, GetPropName<ResultItem>(rst => result.LowValue));
lb_OK.DataBindings.Add("Visible", result, GetPropName<ResultItem>(rst => result.Result));
//此处DataBindings里面的result可以用接口实现
}
string GetPropName<T>(Expression<Func<T, object>> expr)
{
switch (expr.Body.NodeType)
{
case ExpressionType.MemberAccess:
return ((MemberExpression)expr.Body).Member.Name;
case ExpressionType.Convert:
return ((MemberExpression)((UnaryExpression)expr.Body).Operand).Member.Name;
default:
return null;
}
}
private void lb_Value_TextChanged(object sender, EventArgs e)
{
lb_Value.Text = Convert.ToDouble(lb_Value.Text).ToString(precisionStr);
}
private float x=140;
private float y =160;
/// <summary>
/// 將控制項的寬,高,左邊距,頂邊距和字體大小暫存到tag屬性中
/// </summary>
/// <param name="cons">遞歸控制項中的控制項</param>
private void SetTag(Control cons)
{
foreach (Control con in cons.Controls)
{
con.Tag = con.Width + ":" + con.Height + ":" + con.Left + ":" + con.Top + ":" + con.Font.Size;
if (con.Controls.Count > 0)
SetTag(con);
}
}
private void SetControls(float newx, float newy, Control cons)
{
//遍歷窗體中的控制項,重新設置控制項的值
foreach (Control con in cons.Controls)
{
string[] mytag = con.Tag.ToString().Split(new char[] { ':' });//獲取控制項的Tag屬性值,並分割後存儲字元串數組
float a = System.Convert.ToSingle(mytag[0]) * newx;//根據窗體縮放比例確定控制項的值,寬度
con.Width = (int)a;//寬度
a = System.Convert.ToSingle(mytag[1]) * newy;//高度
con.Height = (int)(a);
a = System.Convert.ToSingle(mytag[2]) * newx;//左邊距離
con.Left = (int)(a);
a = System.Convert.ToSingle(mytag[3]) * newy;//上邊緣距離
con.Top = (int)(a);
Single currentSize = System.Convert.ToSingle(mytag[4]) * newy;//字體大小
con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
if (con.Controls.Count > 0)
{
SetControls(newx, newy, con);
}
}
}
private void ucResultItem_Resize(object sender, EventArgs e)
{
float newx = (this.Width) / x; //窗體寬度縮放比例
float newy = (this.Height) / y;//窗體高度縮放比例
SetControls(newx, newy, this);//隨窗體改變控制項大小
}
/// <summary>
/// 固定纵横比
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ucResultItem_SizeChanged(object sender, EventArgs e)
{
this.Width = this.Height * 7 / 8;
}
private void ucResultItem_Load(object sender, EventArgs e)
{
this.Resize += new EventHandler(ucResultItem_Resize);
this.SizeChanged += new EventHandler(ucResultItem_SizeChanged);
SetTag(this);//調用方法
ucResultItem_Resize(new object(), new EventArgs());
}
}
[Serializable]
public class ResultItem : BaseResult
{
private double value = default(double);
private double upValue = default(double);
private double lowValue = default(double);
private bool result = default(bool);
private bool cmpEnable = default(bool);
public double Value
{
get => value;
set
{
if (this.value != value)
{
this.value = value;
OnPropertyChanged("Value");
}
}
}
public double UpValue
{
get => upValue;
set
{
if (upValue != value)
{
upValue = value;
OnPropertyChanged("UpValue");
}
}
}
public double LowValue
{
get => lowValue;
set
{
if (lowValue != value)
{
lowValue = value;
OnPropertyChanged("LowValue");
}
}
}
public bool Result
{
get => result;
set
{
if (result != value)
{
result = value;
//resultStr = result ? "OK" : "NG";
OnPropertyChanged("Result");
//OnPropertyChanged("ResultStr");
}
}
}
public bool CmpEnable
{
get => cmpEnable;
set
{
if (cmpEnable != value)
{
cmpEnable = value;
OnPropertyChanged("CmpEnable");
}
}
}
public void ComputedResult()
{
if (CmpEnable)
{
Result = (LowValue <= Value) && (Value <= UpValue);
}
else
{
Result = true;
}
}
}
public class BaseResult : INotifyPropertyChanged
{
#region INotifyPropertyChanged 成员
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (propertyName == null)
{
throw new ArgumentNullException("propertyName");
}
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
ucResultItem1.Binding(CurrentInfo.result1);