验证表单数据

发布于 2024-08-21 01:52:29 字数 2313 浏览 8 评论 0原文

我正在寻找一种更好的方法来验证表单中没有字段留空,目前这是我的实现,如果您有更好的方法,我们将受到欢迎,请注意我正在使用 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;
    }
}

I am looking for a better way to verify that no field is left blank in a form, currently this is my implementation, if you have a better one it will be welcomed, note tha I am using 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;
}

so when the user sets focus to a control that has to be verified this code runs:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一直在等你来 2024-08-28 01:52:29

您可以像这样优化代码:

switch (c.GetType().Name)  {  case "KryptonTextBox": }

to:

TextBox tb = c as TextBox;
if (tb != null)
    return string.IsNullOrEmpty(tb.Text);

ComboBox cb = c as ComboBox;
if (cb != null)
   return cb.SelectedIndex < 0;

etc.

但我建议对这些 puproses 使用验证器。

You may optimize code something like this:

switch (c.GetType().Name)  {  case "KryptonTextBox": }

to:

TextBox tb = c as TextBox;
if (tb != null)
    return string.IsNullOrEmpty(tb.Text);

ComboBox cb = c as ComboBox;
if (cb != null)
   return cb.SelectedIndex < 0;

etc.

But I suggest to use validators for these puproses.

无妨# 2024-08-28 01:52:29

我推荐 Deborah Kurata 编写的验证类, http: //msmvps.com/blogs/deborahk/archive/2009/07/16/validation-class.aspx
这确实帮助我验证了包含大量文本框的表单。

I recommend the validation class written by Deborah Kurata, http://msmvps.com/blogs/deborahk/archive/2009/07/16/validation-class.aspx
This really helped me validate a form with lots of text boxes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文