比如: foreach (Controls controls1 as form1.Controls) if(controls1==....
foreach (Control ctl in this.Controls) { ArrayList list = new ArrayList(); if (ctl.GetType() == typeof(System.Windows.Forms.TextBox)) { list.Add(ctl.Text); } int max =0; if (list.Count != 0) { try { max = (int)list[0]; } catch (Exception ee) { MessageBox.Show("Error!"); } } for (int i = 0; i < list.Count; i++) { try { if(max<(int)list[i]) { max = (int)list[i]; } } catch(Exception eee) { MessageBox.Show("Error!"); } } }
这段代码是循环赋值,你可以改成取值的foreach (system.windows.forms.control control in this.controls) { if (control is system.windows.forms.textbox) { system.windows.forms.textbox tb = (system.windows.forms.textbox)control ; tb.text = string.empty ; } }
int value = int.MinValue; foreach (Control ctrl in this.Controls) { if (ctrl.GetType() == typeof(TextBox)) { int i = int.Parse((ctrl as TextBox).Text); value = i > value ? i : value; } } MessageBox.Show(value.ToString());
如果值是string 就用string.Compare()比较
1、首先遍历窗体所有是TextBox的控件,并将TextBox的值获取出来存入数组当中.ArrayList array = new ArrayList(); for (int i = 0; i < Controls.Count; i++) { if (Controls[i] is TextBox) { array.Add( (Controls[i] as TextBox).Text.Trim()); } }2、然后在ArrayList中存有你想要的值,你在写一个冒泡的方法返回最大值就行.
if(controls1==....
{
ArrayList list = new ArrayList();
if (ctl.GetType() == typeof(System.Windows.Forms.TextBox))
{
list.Add(ctl.Text);
}
int max =0;
if (list.Count != 0)
{
try
{
max = (int)list[0];
}
catch (Exception ee)
{
MessageBox.Show("Error!");
}
}
for (int i = 0; i < list.Count; i++)
{
try
{
if(max<(int)list[i])
{
max = (int)list[i];
}
}
catch(Exception eee)
{
MessageBox.Show("Error!");
}
}
}
{
if (control is system.windows.forms.textbox)
{
system.windows.forms.textbox tb = (system.windows.forms.textbox)control ;
tb.text = string.empty ;
}
}
int value = int.MinValue;
foreach (Control ctrl in this.Controls)
{
if (ctrl.GetType() == typeof(TextBox))
{
int i = int.Parse((ctrl as TextBox).Text);
value = i > value ? i : value;
}
}
MessageBox.Show(value.ToString());
for (int i = 0; i < Controls.Count; i++)
{
if (Controls[i] is TextBox)
{
array.Add( (Controls[i] as TextBox).Text.Trim());
}
}2、然后在ArrayList中存有你想要的值,你在写一个冒泡的方法返回最大值就行.
6楼的好
[/Quot并没有说,一定是个数值型的吧。
foreach (Control ctl in this.Controls)
{
}
判断控件类型,是textbox 取值,实现最大值比较
{
}