我正在寻找一种更好的方法来验证表单中没有任何字段留空,目前这是我的实现,如果您有更好的实现,它将受到欢迎,请注意我正在使用 KryptonControls。
private bool verify(Control c)
{
switch (c.GetType().Name)
{
case "KryptonTextBox":
{
if (((KryptonTextBox)c).Text == "")
{
((KryptonTextBox)c).StateCommon.Border.Color1 = Color.Red;
((KryptonTextBox)c).GotFocus += new EventHandler(ControlGotFocus);
return false;
}
}
break;
case "KryptonComboBox":
{
if (((KryptonComboBox)c).SelectedIndex < 0)
{
((KryptonComboBox)c).StateCommon.ComboBox.Border.Color1 = Color.Red;
((KryptonComboBox)c).GotFocus += new EventHandler(ControlGotFocus);
return false;
}
}
break;
case "KryptonDataGridView":
{
if (((KryptonDataGridView)c).Rows.Count <= 0)
{
((KryptonDataGridView)c).StateCommon.HeaderColumn.Border.Color1 = Color.Red;
((KryptonDataGridView)c).GotFocus += new EventHandler(ControlGotFocus);
return false;
}
}
break;
default:
break;
}
if (c.Controls.Count > 0)
{
foreach (Control cc in c.Controls)
{
if (!verify(cc))
{
return false;
}
}
}
return true;
}
因此,当用户将焦点设置到必须验证的控件时,此代码将运行:
void ControlGotFocus(object sender, EventArgs e)
{
switch (sender.GetType().Name)
{
case "KryptonTextBox":
{
((KryptonTextBox)sender).StateCommon.Border.Color1 = Color.Gray;
}
break;
case "KryptonComboBox":
{
((KryptonComboBox)sender).StateCommon.ComboBox.Border.Color1 = Color.Gray;
}
break;
case "KryptonDataGridView":
{
((KryptonDataGridView)sender).StateCommon.HeaderColumn.Border.Color1 = Color.Black;
}
break;
default:
break;
}
}