大量控件 动态赋值问题

yuanlai123 2010-04-02 01:23:07
WinForm窗体中有大量TextBox控件,现有一个对象,请问如何将对象中的各个属性赋值给各个TextBox?
注:不是给每个TextBox分别赋值,要一个通用、简便的方法!
...全文
448 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhukui 2010-04-08
  • 打赏
  • 举报
回复
楼主的意思是否想用绑定呀?可以参考Control.DataBindings 属性。如果需要遍历对象的属性,可以采用反射获取PropertyInfo
huiming_zhou 2010-04-08
  • 打赏
  • 举报
回复
assky124 2010-04-08
  • 打赏
  • 举报
回复
不是有PropertyGrid控件么,专门显示属性的
誰是方長 2010-04-08
  • 打赏
  • 举报
回复
取值,在赋值
debug1984 2010-04-08
  • 打赏
  • 举报
回复

/// <summary>
/// 载入信息<para></para>
/// 控件命名要求已3字母前缀加实体属性名(大小写敏感)
/// <para>文本 txt;日期 dtp;下拉框 drp;数字 dec</para>
/// </summary>
/// <param name="container">数据录入控件的容器,通常为panel</param>
/// <param name="item">已设置主键的实体</param>
protected void LoadInfo(Control container, EntityObject item)
{
item.Retrieve();
object obj = null;
Type objtype = item.GetType();
for (int i = 0; i < container.Controls.Count; i++)
{
if (container.Controls[i].Name.StartsWith("dec"))
{
TextBox txt = container.Controls[i] as TextBox;
obj = objtype.GetProperty(txt.Name.Substring(3)).GetValue(item, null);
string c = obj == null ? string.Empty : obj.ToString();
decimal dc = 0;
decimal.TryParse(c, out dc);
txt.Text = dc.ToString("##########.####");
if (string.IsNullOrEmpty(txt.Text))
{
txt.Text = "0";
}
}
else if (container.Controls[i].Name.StartsWith("txt"))
{
TextBox txt = container.Controls[i] as TextBox;
obj = objtype.GetProperty(txt.Name.Substring(3)).GetValue(item, null);
txt.Text = obj == null ? string.Empty : obj.ToString();
}
else if (container.Controls[i].Name.StartsWith("drp"))
{
ComboBox drp = container.Controls[i] as ComboBox;
obj = objtype.GetProperty(drp.Name.Substring(3)).GetValue(item, null);
string v = obj == null ? string.Empty : obj.ToString();
//drp.Text = v;
if (obj != null)
{
drp.SelectedIndex = drp.FindString(obj.ToString());
}
else
{
int emptyindex=drp.FindString("空白");
drp.SelectedIndex = emptyindex;
}
}
else if (container.Controls[i].Name.StartsWith("tmp"))
{
CheckBox chk = container.Controls[i] as CheckBox;
DateTimePicker dtp = container.Controls[container.Controls[i].Name.ToString().Substring(3)] as DateTimePicker;
obj = objtype.GetProperty(dtp.Name.Substring(3)).GetValue(item, null);
if ((obj.ToString() != "0001-1-1 0:00:00") && (obj.ToString() != "0001/1/1 0:00:00"))
{
chk.Text = string.Empty;
chk.Checked = true;
dtp.Visible = true;
dtp.Value = DateTime.Parse(obj.ToString());
}
else
{
dtp.Visible = false;
chk.Text = "点击选择日期";
chk.Checked = false;
}
}
//统一设置DataGridView格式:
else if (container.Controls[i].Name.StartsWith("dgv"))
{
DataGridView dgview = container.Controls[i] as DataGridView;
dgview.BackgroundColor = SystemColors.Window;
dgview.DefaultCellStyle.BackColor = Color.White;
dgview.DefaultCellStyle.ForeColor = Color.Gray;
dgview.DefaultCellStyle.SelectionForeColor = Color.Blue;
dgview.DefaultCellStyle.SelectionBackColor = Color.Aqua;
}
else if (container.Controls[i].Name.StartsWith("int"))
{
TextBox txt = container.Controls[i] as TextBox;
obj = objtype.GetProperty(txt.Name.Substring(3)).GetValue(item, null);
string c = obj == null ? string.Empty : obj.ToString();
int dc = 0;
int.TryParse(c, out dc);
txt.Text = dc.ToString();
if (string.IsNullOrEmpty(txt.Text))
{
txt.Text = "0";
}
}
}
}
wazdoyang 2010-04-08
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 cjcgy 的回复:]

大量控件是你的第一个设计错误。
动态赋值则是你的第二个设计错误。
[/Quote]
放个屁就跑 顶一下
Net85 2010-04-07
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 mxc1225 的回复:]
C# code

for (int i = 0; i < LeaveFormGroupB.Controls.Count; i++)
{
if (LeaveFormGroupB.Controls[i] is TextBox)
{
TextBox temp = ……
[/Quote]



string[] Value ={ "1", "2", "3","4" };

for (int i = 0; i < LeaveFormGroupB.Controls.Count; i++)
{
if (LeaveFormGroupB.Controls[i] is TextBox)
{
TextBox temp = (TextBox)LeaveFormGroupB.Controls[i];

temp.Text = Value[LeaveFormGroupB.Controls.Count-i-1];


}
}
itliyi 2010-04-07
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 mxc1225 的回复:]
C# code

for (int i = 0; i < LeaveFormGroupB.Controls.Count; i++)
{
if (LeaveFormGroupB.Controls[i] is TextBox)
{
TextBox temp = ……
[/Quote]貌似只有这种类似的解决方法
yuanlai123 2010-04-07
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 cjcgy 的回复:]

大量控件是你的第一个设计错误。
动态赋值则是你的第二个设计错误。
[/Quote]
TO :libinguest ,cjcgy
请教一下:1。我有六十多个属性需要显示,我应该如何做?不使用大量的控件的话有什么好的办法吗?
2。原来我是使用“每个控件赋值”的方法,但发现这样太麻烦,后来使用反射,不过十行代码就解决问题了,请问您这样动态赋值有什么不好的吗?

真诚等待您的答案!
wjq 2010-04-02
  • 打赏
  • 举报
回复
你的控件名和属性名能对应上么?可以的话,循环通过属性名用反射获取对应的文本框,然后赋值。
Sphonix 2010-04-02
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wzp144650 的回复:]

反射取值,然后赋值
[/Quote]

这个方法好。
风之影子 2010-04-02
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 cjcgy 的回复:]
大量控件是你的第一个设计错误。
动态赋值则是你的第二个设计错误。
[/Quote]


你说实话了.
指间的风 2010-04-02
  • 打赏
  • 举报
回复
学习 学习
cjcgy 2010-04-02
  • 打赏
  • 举报
回复
foreach跑Controls集合。
在里面调用GetType看Type是不是TextBox
然后再看Name属性。
guanmingle 2010-04-02
  • 打赏
  • 举报
回复
@##¥%¥……&*(
y_sc1413 2010-04-02
  • 打赏
  • 举报
回复
后台动态生成空间,
动态生成时赋值
cjcgy 2010-04-02
  • 打赏
  • 举报
回复
大量控件是你的第一个设计错误。
动态赋值则是你的第二个设计错误。
jimh 2010-04-02
  • 打赏
  • 举报
回复
总有一个属性跟对象的PropertyName对应,一般是控件名,或者Tag属性也可以,另外单独一份mapping表也可以,然后循环控件或循环对象属性,根据mapping关系找出对应的另外一半,然后使用反射读取对象数据,赋值到textbox就可以了
jin20000 2010-04-02
  • 打赏
  • 举报
回复
控件名字与对象属性关联上,直接循环赋值吧
tzc 2010-04-02
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 mxc1225 的回复:]
C# code

for (int i = 0; i < LeaveFormGroupB.Controls.Count; i++)
{
if (LeaveFormGroupB.Controls[i] is TextBox)
{
TextBox temp = ……
[/Quote]

这个循环赋值就挺好
加载更多回复(3)

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧