2016-05-23 11 views
2

Ich versuche, eine Reihe von Steuerelementen innerhalb einer GroupBox in einer formularbasierten Anwendung zu validieren.Kann das Objekt des Typs 1 nicht in Type2 umgewandelt werden?

Ich kann nicht scheinen, um die ComboBox der Anwendung für sie zu erkennen und den Fehler zu generieren und es tut nur so für die TextBox.

private void groupBox1_Validating(object sender, CancelEventArgs e) 
{ 
    foreach (Control control in groupBox1.Controls) 
    { 
     string controlType = control.GetType().ToString(); 
     var lst = new List<string>() { "System.Windows.Forms.TextBox" ,"System.Windows.Forms.ComboBox"}; 

     //if (controlType == "System.Windows.Forms.TextBox") 
     if (lst.Contains(controlType, StringComparer.OrdinalIgnoreCase)) 
     { 
      TextBox txtBox = (TextBox)control; 
      ComboBox combo = (ComboBox)control; 
      if (string.IsNullOrEmpty(txtBox.Text) && string.IsNullOrEmpty(combo.Text)) 
      { 
       MessageBox.Show(txtBox.Name + " Can not be empty"); 
      } 
     } 
    } 
} 

ist hier der Fehler Ich erhalte:

Konnte nicht das Objekt des Typs 'System.Windows.Forms.ComboBox' werfen 'System.Windows.Forms.TextBox' eingeben.

+2

Nun, ein combobox * nicht * eine Textbox, was Sie versuchen, hier zu tun? –

+3

Suchst du relational nach 'as', das wird zu 'null' ausgewertet, wenn es nicht umgewandelt werden kann? 'TextBox txtBox = Kontrolle als TextBox;'? –

+1

Auch, warum nicht einfach für 'control.Text' gehen? –

Antwort

2

Da Sie gerade die Text und Name Eigenschaft überprüfen wollen und weil die beiden TextBox und ComboBox erbt von Control Klasse, die Sie Ich brauche kein Casting. Dies sollte für Sie arbeitet:

foreach (Control control in groupBox1.Controls) 
{ 
    if (!lst.Contains(control.GetType().ToString(), StringComparer.OrdinalIgnoreCase)) continue; 
    if (string.IsNullOrEmpty(control.Text) && string.IsNullOrEmpty(control.Text)) 
    { 
     MessageBox.Show(control.Name + " Can not be empty"); 
    } 
} 

Oder mit Linq:

foreach (Control control in from Control control in groupBox1.Controls 
            where lst.Contains(control.GetType().ToString(), StringComparer.OrdinalIgnoreCase) 
            where string.IsNullOrEmpty(control.Text) && string.IsNullOrEmpty(control.Text) 
            select control) 
{ 
    MessageBox.Show(control.Name + " Can not be empty"); 
} 
+0

Das Problem dort ist, dass die gruupbox andere Dinge abgesehen von Textboxen und Combos haben kann, die nicht überprüft werden müssen – Aimnox

+1

@Aimnox ... Und dann könnte das OP überprüfen dafür auf seine Art wie folgt: 'if (lst.Contains (control.GetType(). ToString(), StringComparer.OrdinalIgnoreCase))' –

2

Verwenden Sie den is Betreiber zu überprüfen, ob Sie die richtige Art haben:

if(control is TextBox) 
{ 
    TextBox txtBox = (TextBox)control; 

    // Do something with txtBox 
    if (string.IsNullOrEmpty(txtBox.Text)) 
    { 
     MessageBox.Show(txtBox.Name + " Can not be empty"); 
    } 
} 

if(control is ComboBox) 
{ 
    ComboBox combo = (ComboBox)control; 

    // Do something with combo 
    if (string.IsNullOrEmpty(combo.Text)) 
    { 
     MessageBox.Show(combo.Name + " Can not be empty"); 
    } 
} 
3

Lassen Sie uns zusammenfassen, was Sie tun möchten:

  1. Für jede Sonde in groupBox1
  2. .. des Typs TextBox oder ComboBox
  3. Überprüfen Sie, ob das Steuerelement nicht leer ist, wenn dies der Fall ist, zeigen Sie a an Meldungsfeld

Hier einige wichtige Punkte:

  1. Jede Steuerung, die von Control erbt eine öffentliche Text Eigenschaft hat, brauchen Sie nicht wirklich wissen, ob es sich um eine Textbox oder eine Combobox für diesen Teil ist
  2. keine Kontrolle ist sowohl eine Textbox und ein combobox (das heißt, erbt keine Steuerklasse von beiden TextBox und ComboBox), so einer dieser Abgüsse wird jedes Mal
  3. scheitern
  4. Sie könnten as anstelle des Hartguss verwendet werden, die null in der Besetzung eines gegossenen zurückkehren würde, die nicht getan werden kann, aber mit Punkt 1 oben ist dies nicht notwendig

So, hier ist der neu geschriebenen Code mit der über wissen:

private void groupBox1_Validating(object sender, CancelEventArgs e) 
{ 
    foreach (Control control in groupBox1.Controls) 
    { 
     if (!(control is TextBox || control is ComboBox)) 
      continue; 
     if (!string.IsNullOrEmpty(control.Text)) 
      continue; 
     MessageBox.Show(control.Name + " Can not be empty"); 
    } 
} 

Beachten Sie, dass, wenn Sie auch andere Dinge tun wollen, die würde müssen wissen, ob die Steuerung eine Textbox oder ein combobox ist, würde ich es stattdessen wie folgt umschreiben:

private void groupBox1_Validating(object sender, CancelEventArgs e) 
{ 
    foreach (Control control in groupBox1.Controls) 
    { 
     var textbox = control as TextBox; 
     if (textbox != null) 
     { 
      ... do your processing of textboxes here 
      continue; 
     } 
     var combobox = control as ComboBox; 
     if (combobox != null) 
     { 
      ... do your processing of comboboxes here 
      continue; 
     } 
     ... do your processing of other controls here 
    } 
} 
+0

Falscher bedingter Operator. Sollte '||' nicht 'oder' sein. – Ulric

+0

Danke, das bekomme ich von SQL in den ganzen Morgen :) –

1

Wie @LasseV.Karlsen darauf hinwies, nahm ich den langen Weg, indem ich jedes Steuerelement einzeln hinzufügte. Ich habe einfach die Kontrolle anstelle von etwas mehr als spezifischen Kontrollen hinzugefügt.

Hier ist, was meine aktualisierten Code wie jetzt aussehen:

foreach (Control control in groupBox1.Controls) 
     { 
      string controlType = control.GetType().ToString(); 
      var lst = new List<string>() { "System.Windows.Forms.TextBox" ,"System.Windows.Forms.ComboBox"}; 

      //if (controlType == "System.Windows.Forms.TextBox") 
      if (lst.Contains(controlType, StringComparer.OrdinalIgnoreCase)) 
      { 
       // TextBox txtBox = (TextBox)control; 
       // ComboBox combo = (ComboBox)control; 
       if (string.IsNullOrEmpty(control.Text) && string.IsNullOrEmpty(control.Text)) 
       { 
        MessageBox.Show(control.Name + " Can not be empty"); 
       } 
      } 
3

In Ihrem Code, es warf jede Textbox und jede Combo in einen Text und ein Combo.

Sie müssen es umwandeln, was es nur ist.

private void groupBox1_Validating(object sender, CancelEventArgs e) 
{ 
    foreach (Control control in groupBox1.Controls) 
    { 
     if (control is ComboBox) 
     { 
      ComboBox combo = (ComboBox)control; 
      if (string.IsNullOrEmpty(combo.Text)) MessageBox.Show(combo.Name + " Can not be empty"); 
     } 
     else if (control is TextBox) 
     { 
      TextBox txtBox = (TextBox)control; 
      if (string.IsNullOrEmpty(txtBox.Text)) MessageBox.Show(txtBox.Name + " Can not be empty"); 
     } 
    } 
} 

Wenn die groupbox nur Textbox und Combobox haben, können Sie auch tun:

private void groupBox1_Validating(object sender, CancelEventArgs e) 
{ 
    foreach (dynamic control in groupBox1.Controls) 
    {    
     if (string.IsNullOrEmpty(control.Text)) MessageBox.Show(control.Name + " Can not be empty");    
    } 
} 
Verwandte Themen