首页 > 代码库 > 根据对象属性给控件赋值
根据对象属性给控件赋值
当界面控件比较多的时候,给控件赋值就是一件比较困难的事情了,但是我们可以使用下面这样的办法给控件赋值
/// <summary> /// 绑定帮助类 /// </summary> public class QueryBondHelper { /// <summary> /// 绑定属性到控件 /// </summary> /// <param name="control">control</param> /// <param name="mStockClientProductinfoQueryBycodeOut">mStockClientProductinfoQueryBycodeOut</param> public static void BindPropertyToControl(Control control, StockClientProductinfoQueryBycodeResp resp) { //Dictionary<string, string> mWY = new Dictionary<string, string>(); //mWY.Add("publishAmount", "发行总额(万元)"); //mWY.Add("assetSize", "资产规模(万元)"); foreach (Control ctrl in control.Controls) { if (ctrl is TextBox) { TextBox textbox = ctrl as TextBox; ctrl.Enabled = true; textbox.ReadOnly = true; PropertyInfo pi = resp.GetType().GetProperty(textbox.Name.Substring(3)); if (pi != null) { string txtValue = http://www.mamicode.com/pi.GetValue(resp, null) + ""; if (pi.PropertyType == typeof(long) || pi.PropertyType == typeof(decimal) || pi.PropertyType == typeof(double)) { double i = 0.0; if (double.TryParse(txtValue, out i)) { if (textbox.Name.EndsWith("_WY")) { textbox.Text = i.ToString("N6"); } else { //textbox.Text = i.ToString("N2"); textbox.Text = i.ToString(); } } else { textbox.Text = txtValue; } } else { textbox.Text = txtValue; } } } else if (ctrl is DicComboBox) { ctrl.Enabled = false; DicComboBox dicctrl = ctrl as DicComboBox; PropertyInfo pi = resp.GetType().GetProperty(dicctrl.Name.Substring(6)); if (pi != null) { string data= http://www.mamicode.com/pi.GetValue(resp, null) + ""; if(data.EndsWith(",") || data.EndsWith(";")) { data = data.Substring(0, data.Length - 1); } dicctrl.Data = data; } } else if (ctrl is DicCheckBoxComboBox) { ctrl.Enabled = false; DicCheckBoxComboBox dicctrl = ctrl as DicCheckBoxComboBox; PropertyInfo pi = resp.GetType().GetProperty(dicctrl.Name.Substring(6)); if (pi != null) { string data = http://www.mamicode.com/pi.GetValue(resp, null) + ""; if (data.EndsWith(",") || data.EndsWith(";")) { data = data.Substring(0, data.Length - 1); } dicctrl.Data = data; } } else if (ctrl is NullableDateTimePicker) { ctrl.Enabled = false; NullableDateTimePicker dtpctrl = ctrl as NullableDateTimePicker; PropertyInfo pi = resp.GetType().GetProperty(dtpctrl.Name.Substring(3)); if (pi != null) { string txtValue = http://www.mamicode.com/pi.GetValue(resp, null) + ""; if (txtValue != null) { long i; if (Int64.TryParse(txtValue, out i)) { dtpctrl.Date = i; } } } } } } /// <summary> /// 通过名字找控件。 /// </summary> /// <param name="container">container</param> /// <param name="name">name</param> /// <returns>returns</returns> public Control findControlByName(Control container, string name) { if (name == null || name.Length == 0) { return null; } int count = container.Controls.Count; for (int i = 0; i < count; i++) { Control curControl = container.Controls[i]; if (name.Equals(curControl.Name)) { return curControl; } else { Control foundControl = this.findControlByName(curControl, name); if (foundControl != null) { return foundControl; } } } return null; } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。