Pregunta

I have a problem when checking if a textBox is empty. I have read many questions and answers here, and I have used the codes mentioned but with no luck.

I just want to check if a textBox is empty, and if it is then do something, if not, do something else.

So far I have tried the following codes:

(textBox4 != null && !string.IsNullOrEmpty(textBox4.Text))
(textBox4.Text == "")
(!String.IsNullOrEmpty(textBox4.text)
(textBox4.Text != Stirng.Empty)
(textBox4.Text.Trim() == "")
(textbox4.Text.Length == 0)

None of the codes show any errors, but they still do not work. Here is my complete code for what I am trying to do:

     private void textBox4_TextChanged(object sender, TextChangedEventArgs e)
   {
    try
        {
            double a = Convert.ToDouble(textBox10.Text);                
            double c = Convert.ToDouble(textBox2.Text);
            double d = Convert.ToDouble(textBox3.Text);
            double ee = Convert.ToDouble((0.13 * d) * 2);

            if (string.IsNullOrEmpty(textBox4.Text))
            {
                textBox15.Text = Math.Round((((((ee / 2) / c) * 0.13) + (d * 0.13)), 4).ToString();
            }
            else
            {
                textBox15.Text = (((((a / 2) / c) * 0.13) + (d * 0.13)) / b).ToString(); 
            }
        }
        catch
        {
        }
    }

(textBox 3,10,15 are IsReadOnly="True", and the Keyboard is InputScope="Number", if that matters)

What am I doing wrong? I feel like I have tested every code there is, and I don't understand why it doesn't check if the textBox4 is empty.

EDIT: When I write something in textBox4, the code works, and when I delete the entry from textBox4 it detects that the textBox4 is empty (but that's late)

EDIT 2: This same If/Else code for textBox15 is written under the TextChanged events for all of the other textBoxes involved (1,2,3,4,10,15).

When I put a Breakpoint on the If and Else statements, it gets executed only when:

  • The code works for the If statement only when I type something in textBox4 and then delete it.

  • It works for Else statement only when I type something, then delete it, and then retype it again.

¿Fue útil?

Solución 4

OK, I have solved my own problem by using string.IsNullOrWhiteSpace Method.

"I just want to check if a textBox is empty, and if it is then do something, if not, do something else."

Here is how the code worked:

  private void textBox4_TextChanged(object sender, TextChangedEventArgs e)

try
        {
            if (string.IsNullOrWhiteSpace(textBox4.Text))
            {
                //do something
            }
        }

        catch
        {
        }

Thank you all for helping me :)

Otros consejos

Did you try !string.IsNullOrEmpty(textBox4.Text.Trim())?

That should help you to remove any spaces that might still be lingering in the text box.

"it somehow doesn't detect that the textBox4 is empty, and nothing happens. When i fill in the textBox4 with some value the code works. "

So am i correct in understanding that you expect to run this code and for the "textBox4_TextChanged" method to be called? Because that looks like an event handler that will be called only when textBox4 changes

So indeed, not yuor if statement is the problem, your whole method is not being called initially.

What happens if you fill in something in textBox4 and then delete it so it changes to empty? I would dare to bet that your code works then.

So you want to initialize your textBox15 value correctly :)

Simply assign the correct value (for when textBox4 is empty) to textBox15 when your program is starting up :) This can be in the constructor of your form, or when you create textBox15, I don't know how you have set up the rest of your project.

An extra tip: using the debugger and setting a breakpoint on your if statement, you would have noticed that your code is never getting executed until you actually change the value of textBox4. (Although the name of the method should also have been a hint ;) )

If it start to handle TextChanged event, that mean your textBox4 is not empty any more, because you've entered something in textBox4. Even though all you entered was whitespace, it will still invoke the textBox4_TextChanged method.

So I think you shouldn't put the textBox4 checking line in textBox4_TextChanged method.

If you jump into the event-handler Textbox4 can't be null. So I would suggest using

"if (!string.isNullOrEmpty(TextBox4.Text))"

As others mentioned just use a debugger. Usually works nice in Winforms using F5.

If you are absolutely sure there can't be anything in that textbox and the condition still does not work, check your designer-file if the textbox4_TextChanged REALLY is assigned to TextBox4.

But I repeat: Simply use a debugger to look what happens there stepping through the code with F10

I do not understand why your (textBox4 != null && !string.IsNullOrEmpty(textBox4.Text)) not working however, while using IsNullOrEmpty you do not need to check null like you did there. Try to put your value to a string then check for null or empty.

Update : After reading your comment i think you need to reverse your if condition , When a user enters something you need to execute else condition, So when you enter something into textbox4 then on textBox4_TextChanged event textbox4 is not empty, So you just need to check your business logic or change your condition .

string strtxt4 = Convert.toString(textBox4.text);

if (string.IsNullOrEmpty(strtxt4 ))
        {
            textBox15.Text = Math.Round(((((((ee / b) + c) * b) * 0.0025) + (d * 0.0025)) / b), 4).ToString();
        }
        else
        {
            textBox15.Text = ((((((a / b) + c) * b) * 0.0025) + (d * 0.0025)) / b).ToString(); 
        }

Try with Validating Event. I have tried for the same and i think that worked well according to my opinion. Keep the logic same but write the code in validating event. If i am wrong please inform me.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top