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.
Nun, ein combobox * nicht * eine Textbox, was Sie versuchen, hier zu tun? –
Suchst du relational nach 'as', das wird zu 'null' ausgewertet, wenn es nicht umgewandelt werden kann? 'TextBox txtBox = Kontrolle als TextBox;'? –
Auch, warum nicht einfach für 'control.Text' gehen? –