查找哪个文本框是空的 [英] Finding which textbox is empty

查看:42
本文介绍了查找哪个文本框是空的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简短的 Windows 程序,用于快速添加信息.但现在我正在努力增强它.正在寻找一种更有效的方式来检查空文本框,以及该框是否为空以查找它是哪个并将焦点设置回仅该框.目前我遍历所有这些并检查是否有任何框是空的,如果它只是显示一条消息.但是必须查看哪个框缺少文本.代码如下:

I have short windows program I use to add information quickly. But now I'm trying to enhance it. Was looking for a more efficient want to check for empty text boxes and if the box was empty to find which one it was and set the focus back to only that box. Currently I loop through all of them and check to see if any box was empty if it is just display a message. But have to look to see which box is missing text. Heres the code:

bool txtCompleted = true;
string errorMessage = "One or more items were missing from the form";
foreach(Control c in Controls)
{
    if (c is TextBox) 
    {
        if (String.IsNullOrEmpty(c.Text))
        {
            txtCompleted = false;                        
        }
    }
}
if (txtCompleted == false)
{
    MessageBox.Show(errorMessage);
}

推荐答案

在循环中将焦点设置在控件上,完成后中断.

Set the focus on the control while in your loop, then break when done.

    foreach(Control c in Controls)
    {
        if (c is TextBox) 
        {
            if (String.IsNullOrEmpty(c.Text))
            {
                txtCompleted = false; 
                c.Focus();  
                MessageBox.Show(errorMessage);
                break;
            }
        }
    }

这篇关于查找哪个文本框是空的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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