0

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.

7
  • Sounds like a job for the debugger. What is the value of textBox4.Text when these conditions are failing? Commented Nov 12, 2013 at 15:34
  • Are you creating textBox4 dynamically, if not then you do not need textBox4 != null. Commented Nov 12, 2013 at 15:38
  • textBox4 stays empty until the user fill something in. It is a calculator, so everything is filled with numbers. When the textBox4 is emtpy, it needs to execute the first If statement, but when the user enters something in the textBox4 it need to execute the second Else statement Commented Nov 12, 2013 at 15:38
  • You want the fist statement to run if textBox4 is blank, then if (string.IsNullOrEmpty(textBox4.Text)) should do. !string.IsNullOrEmpty(textBox4.Text) means the textbox is not empty. Commented Nov 12, 2013 at 15:40
  • 1
    Are you sure nothing is working? Some of your tests test for an empty box, others test for the opposite, so if you just replace your if-condition, you get different results... And Bolu has a good point, can you please tell us what is going wrong? Does textBox15 stay empty? Because in that case the problem i snot with your if-statement. Commented Nov 12, 2013 at 15:46

7 Answers 7

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

"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 ;) )

13 Comments

Yes, you are right. When i enter a value, and then delete it, the code works. :) I am sorry, i am a noob, and i am trying to explain everything as much as i can.
I didn't initialize textBox15. Could you help me do this? How should i do this?
@Oerkelens Don't you think just reversing the IsNullOrEmpty method will do ?
@SurajSingh Nopes, I think we have already established that the whole method never gets called initially, which explain why the textBox stays empty.
@Oerkelens I've tried to write the code under InitializeComponent(); but it doesn't work. I must be writing it wrong. What should i write under InitializeComponent(); in order for it to work. I've tried using the same code as in textBox4_TextChanged that I've mentioned above, and also textBox4.Text = "". ?
|
1

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.

2 Comments

Should i use another method? Where should i put the checking line for textBox4?
You should put the checking with if-else code in other TextChanged event handler of textBoxes which are not readOnly, such as textBox1 and textBox2. But for textBox4_TextChanged you don't need to have if scope, just put the calculation in each scope.
1

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 :)

Comments

0

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

Comments

0

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(); 
        }

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.