【发布时间】:2009-09-30 15:55:36
【问题描述】:
如何使用 Foreach 语句对我的 TextBox 做一些事情?
foreach (Control X in this.Controls)
{
Check if the controls is a TextBox, if it is delete it's .Text letters.
}
【问题讨论】:
如何使用 Foreach 语句对我的 TextBox 做一些事情?
foreach (Control X in this.Controls)
{
Check if the controls is a TextBox, if it is delete it's .Text letters.
}
【问题讨论】:
如果您使用的是 C# 3.0 或更高版本,您可以执行以下操作
foreach ( TextBox tb in this.Controls.OfType<TextBox>()) {
..
}
如果没有 C# 3.0,您可以执行以下操作
foreach ( Control c in this.Controls ) {
TextBox tb = c as TextBox;
if ( null != tb ) {
...
}
}
或者更好的是,在 C# 2.0 中编写 OfType。
public static IEnumerable<T> OfType<T>(IEnumerable e) where T : class {
foreach ( object cur in e ) {
T val = cur as T;
if ( val != null ) {
yield return val;
}
}
}
foreach ( TextBox tb in OfType<TextBox>(this.Controls)) {
..
}
【讨论】:
this 是一个表单并且代码以相同的表单运行。如果我想读取程序集中 Assembly.GetExecutingAssembly().GetTypes() 中的所有表单,然后在运行时访问其中的 controls 以修改某些属性怎么办?
上面的很多工作。 只是补充。如果您的文本框不直接在表单上,而是在其他容器对象(如 GroupBox)上,则您必须获取 GroupBox 对象,然后遍历 GroupBox 以访问其中包含的文本框。
foreach(Control t in this.Controls.OfType<GroupBox>())
{
foreach (Control tt in t.Controls.OfType<TextBox>())
{
// do stuff
}
}
【讨论】:
你在寻找
foreach (Control x in this.Controls)
{
if (x is TextBox)
{
((TextBox)x).Text = String.Empty;
}
}
【讨论】:
您也可以使用 LINQ。例如,对于清除 Textbox 文本,请执行以下操作:
this.Controls.OfType<TextBox>().ToList().ForEach(t => t.Text = string.Empty);
【讨论】:
foreach (Control X in this.Controls)
{
if (X is TextBox)
{
(X as TextBox).Text = string.Empty;
}
}
【讨论】:
这里的诀窍是Controls 不是List<> 或IEnumerable,而是ControlCollection。
我建议使用 Control 的扩展,它会返回更多内容..可查询 ;)
public static IEnumerable<Control> All(this ControlCollection controls)
{
foreach (Control control in controls)
{
foreach (Control grandChild in control.Controls.All())
yield return grandChild;
yield return control;
}
}
那么你可以这样做:
foreach(var textbox in this.Controls.All().OfType<TextBox>)
{
// Apply logic to the textbox here
}
【讨论】:
您可以执行以下操作:
foreach (Control X in this.Controls)
{
TextBox tb = X as TextBox;
if (tb != null)
{
string text = tb.Text;
// Do something to text...
tb.Text = string.Empty; // Clears it out...
}
}
【讨论】:
只需添加其他控件类型:
public static void ClearControls(Control c)
{
foreach (Control Ctrl in c.Controls)
{
//Console.WriteLine(Ctrl.GetType().ToString());
//MessageBox.Show ( (Ctrl.GetType().ToString())) ;
switch (Ctrl.GetType().ToString())
{
case "System.Windows.Forms.CheckBox":
((CheckBox)Ctrl).Checked = false;
break;
case "System.Windows.Forms.TextBox":
((TextBox)Ctrl).Text = "";
break;
case "System.Windows.Forms.RichTextBox":
((RichTextBox)Ctrl).Text = "";
break;
case "System.Windows.Forms.ComboBox":
((ComboBox)Ctrl).SelectedIndex = -1;
((ComboBox)Ctrl).SelectedIndex = -1;
break;
case "System.Windows.Forms.MaskedTextBox":
((MaskedTextBox)Ctrl).Text = "";
break;
case "Infragistics.Win.UltraWinMaskedEdit.UltraMaskedEdit":
((UltraMaskedEdit)Ctrl).Text = "";
break;
case "Infragistics.Win.UltraWinEditors.UltraDateTimeEditor":
DateTime dt = DateTime.Now;
string shortDate = dt.ToShortDateString();
((UltraDateTimeEditor)Ctrl).Text = shortDate;
break;
case "System.Windows.Forms.RichTextBox":
((RichTextBox)Ctrl).Text = "";
break;
case " Infragistics.Win.UltraWinGrid.UltraCombo":
((UltraCombo)Ctrl).Text = "";
break;
case "Infragistics.Win.UltraWinEditors.UltraCurrencyEditor":
((UltraCurrencyEditor)Ctrl).Value = 0.0m;
break;
default:
if (Ctrl.Controls.Count > 0)
ClearControls(Ctrl);
break;
}
}
}
【讨论】:
foreach (Control x in this.Controls)
{
if (x is TextBox)
{
((TextBox)x).Text = String.Empty;
//instead of above line we can use
*** x.resetText();
}
}
【讨论】:
检查一下:
foreach (Control x in this.Controls)
{
if (x is TextBox)
{
x.Text = "";
}
}
【讨论】:
使用 linq 简单,根据您认为适合您处理的任何控件进行更改。
private void DisableButtons()
{
foreach (var ctl in Controls.OfType<Button>())
{
ctl.Enabled = false;
}
}
private void EnableButtons()
{
foreach (var ctl in Controls.OfType<Button>())
{
ctl.Enabled = true;
}
}
【讨论】:
更好的是,您可以将其封装起来,以一种方法清除您想要的任何类型的控件,如下所示:
public static void EstadoControles<T>(object control, bool estado, bool limpiar = false) where T : Control
{
foreach (var textEdits in ((T)control).Controls.OfType<TextEdit>()) textEdits.Enabled = estado;
foreach (var textLookUpEdits in ((T)control).Controls.OfType<LookUpEdit>()) textLookUpEdits.Enabled = estado;
if (!limpiar) return;
{
foreach (var textEdits in ((T)control).Controls.OfType<TextEdit>()) textEdits.Text = string.Empty;
foreach (var textLookUpEdits in ((T)control).Controls.OfType<LookUpEdit>()) textLookUpEdits.EditValue = @"-1";
}
}
【讨论】:
private IEnumerable<TextBox> GetTextBoxes(Control control)
{
if (control is TextBox textBox)
{
yield return textBox;
}
if (control.HasChildren)
{
foreach (Control ctr in control.Controls)
{
foreach (var textbox in GetTextBoxes(ctr))
{
yield return textbox;
}
}
}
}
【讨论】:
我发现这很好用,但最初我的文本框在面板上,所以我的文本框都没有按预期清除,所以我添加了
this.panel1.Controls.....
这让我想到你有很多不同功能的文本框,只有一些需要清除,也许使用多个面板层次结构你可以只定位几个而不是全部。
foreach ( TextBox tb in this.Controls.OfType<TextBox>()) {
..
}
【讨论】: