2009-09-30 8 views

Odpowiedz

47

Szukacie

foreach (Control x in this.Controls) 
{ 
    if (x is TextBox) 
    { 
    ((TextBox)x).Text = String.Empty; 
    } 
} 
3

można wykonać następujące czynności:

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... 
    } 
} 
4
foreach (Control X in this.Controls) 
{ 
    if (X is TextBox) 
    { 
    (X as TextBox).Text = string.Empty; 
    } 
} 
58

Jeśli używasz C# 3.0 lub nowszy można wykonać następujące

foreach (TextBox tb in this.Controls.OfType<TextBox>()) { 
    .. 
} 

Bez C# 3.0 można wykonać następujące czynności

foreach (Control c in this.Controls) { 
    TextBox tb = c as TextBox; 
    if (null != tb) { 
    ... 
    } 
} 

albo jeszcze lepiej, zapisz OfType w C# 2.0.

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)) { 
    .. 
} 
+0

uwaga dla tych, którzy będą Ta: jeśli masz pod swoimi panelu sterowania, należy określić, że takie jak: this.myPanel.Controls.OfType (), możesz również użyć this.myPanel.Controls.OfType () .Where (x => x.Name.StartsWith ("specificName")), jeśli musisz wybrać comboboxes, które mają określoną nazwę. – Rose

+0

Zakładam, że "ten" jest formularzem, a kod działa w tej samej formie. Co jeśli chcę przeczytać wszystkie formularze w złożeniu 'Assembly.GetExecutingAssembly(). GetTypes()', a następnie uzyskać dostęp do 'control's w nim w środowisku wykonawczym, aby zmodyfikować jakąś właściwość? – AaA

8

Sztuką jest to, że nie jest ControlsList<> lub IEnumerable ale ControlCollection.

polecam używając rozszerzenia Kontroli że powróci coś more..queriyable;)

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; 
     } 
    } 

Następnie można zrobić:

foreach(var textbox in this.Controls.All().OfType<TextBox>) 
{ 
    // Apply logic to the textbox here 
} 
0

Wystarczy dodać inne rodzaje kontroli:

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; 

     } 

    } 
} 
0
foreach (Control x in this.Controls) 
{ 
    if (x is TextBox) 
    { 
    ((TextBox)x).Text = String.Empty; 
//instead of above line we can use 
    *** x.resetText(); 
    } 
} 
0

Sprawdź to:

foreach (Control x in this.Controls) 
{ 
    if (x is TextBox) 
    { 
     x.Text = ""; 
    } 
} 
0

proste użycie linq, zmień według własnego uznania, niezależnie od tego, z czym masz do czynienia.

 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; 
     } 
    } 
0

Nawet lepiej, można encapsule to, aby usunąć dowolny typ kontroli, który ma w jednej metody, na przykład:

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"; 
      } 
     } 
2

Ponadto można używać LINQ. Na przykład dla jasnego Textbox tekstu zrobić coś takiego:

this.Controls.OfType<TextBox>().ToList().ForEach(t => t.Text = string.Empty); 
Powiązane problemy