Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
385 views
in Technique[技术] by (71.8m points)

c# - Checking if all textboxes in a panel a filled

I currently work on a Windows Forms application and I have 2 Panels with textboxes in them and I need to check the panel's textboxes separately if they are not empty, so it is not an option to loop through all the controls in the form.

            foreach (Control child in this.Controls)
        {
            TextBox textBox = child as TextBox;
            if (textBox != null)
            {
                if (!string.IsNullOrWhiteSpace(textBox.Text))
                {
                    MessageBox.Show("Text box can't be empty");
                }
            }
        }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Perhaps something like this:

    foreach(Panel pnl in Controls.OfType<Panel>())
    {
        foreach(TextBox tb in pnl.Controls.OfType<TextBox>())
        {
            if(string.IsNullOrEmpty(tb.Text.Trim()))
            {
                MessageBox.Show("Text box can't be empty");
            }
        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...