标签:dict show 封装 display string mem 记录 logs new
对于控件赋值,刚接触客户端(WPF)的时候是一个控件一个控件的传值 ,如:this.xxx.Text ="123",后来,觉得这样太麻烦了,就用循环控件进行绑定值以及得到json格式的值(https://www.cnblogs.com/zhixiaoxiao/p/11897143.html),太好用了。妈妈再也不用担心我因为给N个控件赋值而累死了。
下面是我第一次接触Winform的笔记,笔记粗糙, 没有封装成一个完整的类,后面项目中会整理,记录下来:
循环绑定的代码如下:
foreach (Control item in this.panel.Controls) { if (item is TextBox) { var tex = item as TextBox; tex.Text = t.Textb; } if (item is CheckBox) { var ch = item as CheckBox; ch.Checked = t.Checkb; } if (item is DataGridView) { var dg = item as DataGridView; dg.DataSource = tests; } if (item is ListBox) { var lis = item as ListBox; lis.DisplayMember = "Textb"; lis.ValueMember = "Checkb"; lis.DataSource = tests; } }
多控件循环取值,放到Json中,代码如下:
Dictionary<string, object> keyValues = new Dictionary<string, object>(); foreach (Control item in this.panel.Controls) { if (item is TextBox) { var tex = item as TextBox; keyValues.Add(tex.Name, tex.Text); } if (item is CheckBox) { var ch = item as CheckBox; keyValues.Add(ch.Name, ch.Checked); } if (item is DataGridView) { var dg = item as DataGridView; keyValues.Add(dg.Name, (dg.CurrentRow.DataBoundItem as Test).Textb); } if (item is ListBox) { //var lis = item as ListBox; //lis.DisplayMember = "Textb"; //lis.ValueMember = "Checkb"; } } string jsonStr = JsonConvert.SerializeObject(keyValues); MessageBox.Show(jsonStr);
标签:dict show 封装 display string mem 记录 logs new
原文地址:https://www.cnblogs.com/zhixiaoxiao/p/11975851.html