如何验证已启用的空文本框而不考虑已禁用的文本框 [英] How to validate enabled empty textboxes disregarding the disabled textboxes

查看:115
本文介绍了如何验证已启用的空文本框而不考虑已禁用的文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该如何声明仅验证已启用的文本框?"

How should I declare "validate only Text Boxes which are enabled?"

 public static bool IsEmpty(Form myform)
        {
            foreach (Control myControl in myform.Controls)
            {
if ((myControl.GetType() == typeof(TextBox)) || (myControl.GetType() == typeof(ComboBox)))
                {
                    myControl.Text = myControl.Text.Trim();
                    if (myControl.Text == "")
                    {
                        MessageBox.Show(myControl.Name.Substring(3).ToString() + " is empty.");
                        myControl.Focus();
                        return true;
                    }
                    myControl.Text = TrimBetween(myControl.Text);
                }

推荐答案

添加对Enabled属性的检查:
Add a check for the Enabled property:
if ((myControl.GetType() == typeof(TextBox)) || (myControl.GetType() == typeof(ComboBox)))
    {
    //myControl.Text = myControl.Text.Trim();
    if (myControl.Enabled && string.IsNullOrWhiteSpace(myControl.Text))
        {
        MessageBox.Show(myControl.Name.Substring(3).ToString() + " is ");
        myControl.Focus();
        return true;
        }
    myControl.Text = TrimBetween(myControl.Text);
    }

BTW:我已经删除了Trim,并更改了检查-如果您使用IsNullOrWhiteSpace方法,它将为您完成此操作.

BTW: I have removed the Trim, and changed the check - if you use the IsNullOrWhiteSpace method it does that for you.


这篇关于如何验证已启用的空文本框而不考虑已禁用的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆