Finding which textbox is empty
Asked Answered
P

3

5

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);
}
Publicity answered 12/9, 2014 at 19:24 Comment(1)
if you want to check one TextBox for example you can also use the sender property like this ((System.Web.UI.WebControls.TextBox)sender).Items.Clear();Havenot
T
2

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;
            }
        }
    }
Tade answered 12/9, 2014 at 19:31 Comment(5)
I thought the question was about Focus too, but OP just wants to enhance his current code.Frederico
This does, he does not loop through every control unless there are no empty text boxes. As soon as one if found it stops. In his code he loops through all regardless. Linq has to loop through all controls to return the ones he wants so this is more efficient as far as processing. Linq is more efficient in code lines.Tade
@CyberneticTwerkGuruOrc the title also states that the OP wants to find the empty textbox. Based on that it's not just about enhancing (since his current code isn't "finding" anything)Skantze
His code is finding it, he just does not know which it is which s why he wants to focus. This code i posted will do what he needs efficiently.Tade
@Default OP's code does find what OP wants. OP loops all user user controls that are TextBox. Then he checks to see if the Text is empty or not. That's exactly what OP does. OP does ask about Focus(), but that's a very google-able answer. I had posted a similar answer and realized OP mentioned enhancement. It's a difficult to understand question. :/Frederico
I
6

Your approach using foreach looks promising to me. Howver you can use LINQ as well

if(this.Controls.OfType<TextBox>().Any(t => string.IsNullOrEmpty(t.Text)) {
    ...
}

You can use the focus() method to set the focus to the empty text box.

Inadmissible answered 12/9, 2014 at 19:27 Comment(1)
When you master LINQ, you own C#. Nice solution!Ancier
T
2

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;
            }
        }
    }
Tade answered 12/9, 2014 at 19:31 Comment(5)
I thought the question was about Focus too, but OP just wants to enhance his current code.Frederico
This does, he does not loop through every control unless there are no empty text boxes. As soon as one if found it stops. In his code he loops through all regardless. Linq has to loop through all controls to return the ones he wants so this is more efficient as far as processing. Linq is more efficient in code lines.Tade
@CyberneticTwerkGuruOrc the title also states that the OP wants to find the empty textbox. Based on that it's not just about enhancing (since his current code isn't "finding" anything)Skantze
His code is finding it, he just does not know which it is which s why he wants to focus. This code i posted will do what he needs efficiently.Tade
@Default OP's code does find what OP wants. OP loops all user user controls that are TextBox. Then he checks to see if the Text is empty or not. That's exactly what OP does. OP does ask about Focus(), but that's a very google-able answer. I had posted a similar answer and realized OP mentioned enhancement. It's a difficult to understand question. :/Frederico
S
1

To get a reference to the empty textbox you use almost the same solution as R.T. presents, but use FirstOrDefault instead:

var emptyTextBox = Controls.OfType<TextBox>().FirstOrDefault(t => string.IsNullOrEmpty(t.Text)
if (emptyTextBox != null)
{
    // there is a textbox that has no Text set
    // set focus, present error message etc. on emptyTextBox
}
Skantze answered 12/9, 2014 at 19:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.