topofsteel
topofsteel

Reputation: 1277

Form retaining textBox values - Reset?

I have a form the checks that several textBox's aren't null. If either of them are, it is supposed to show a message box, reset the text boxes and let the user try again. I believe i'm checking the textbox wrong. How can I do this? Thanks.

    public void ShowPaths()
    {
        if (textBox1.Text == null | textBox2.Text == null)
        {
            MessageBox.Show("Please enter a Project Name and Number");
        }
        else
        {
            sm.projNumber = textBox1.Text;
            sm.projName = textBox2.Text;

            textBox3.Text = sm.Root("s");
            textBox4.Text = sm.Root("t");
        }
        textBox1.ResetText();
        textBox2.ResetText();           
    }

Upvotes: 1

Views: 2420

Answers (6)

pyrocumulus
pyrocumulus

Reputation: 9290

The .Text property of a TextBox will never be null. What you are looking for is an empty string, so:

if (textBox1.Text.Equals(string.Empty) || textBox2.Text.Equals(string.Empty))

or

if (textBox1.Text == "" || textBox2.Text == "")

or

if (String.IsNullOrEmpty(textBox1.Text) || String.IsNullOrEmpty(textBox2.Text))

The | operator should be a || as well. But this is only part of the problem.

Upvotes: 0

Steve
Steve

Reputation: 216293

This line is wrong for two reasons

if (textBox1.Text == null | textBox2.Text == null) 
  1. A textbox.Text is never null when you read it, it's an empty string
  2. You are using the bitwise OR operator | when you should use the logical ||

so the correct line is

if (textBox1.Text == string.Empty || textBox2.Text == string.Empty)
{
    MessageBox(......);

    // See the comment below
    textBox1.ResetText();    
    textBox2.ResetText();   
} 

From your question is not clear if you want to reset the textbox in case of error or if you want to reset always as you do now. If you want to reset only in case of error move the two ResetText inside the if block

Upvotes: 3

Ash Burlaczenko
Ash Burlaczenko

Reputation: 25445

Although I'm not conviced a textbox can have a null value I use String.IsNullOrEmpty

if(String.IsNullOrEmpty(textBox1.Text) || String.IsNullOrEmpty(textBox2.Text))
{
    //...
}

Upvotes: 0

RvdK
RvdK

Reputation: 19790

if ((textBox1.Text == String.Empty) || (textBox2.Text == String.Empty))

if Textbox1 is empty OR textbox2 is empty (note the || instead of |) Also the Text property is never null. It is always a string, but it could be empty (String.Empty or "")

Upvotes: 0

Romil Kumar Jain
Romil Kumar Jain

Reputation: 20745

Use

  if (textBox1.Text == null || textBox2.Text == null)

instead of

  if (textBox1.Text == null | textBox2.Text == null)

You are not using OR (||) operator correctly.

Use String.IsNullorEmpty(string) to check for NULL and blank values in string variable.

Upvotes: 0

C. Ross
C. Ross

Reputation: 31848

WinForms Texboxes never show null in my experience, instead returning String.Empty.

You can use String.IsNullOrEmpty(textBox1.Text) to check for either case. If you're using .Net 4, you can use String.IsNullOrWhiteSpace(textBox1.Text) which will return true for spaces as well.

if (String.IsNullOrWhiteSpace(textBox1.Text) || String.IsNullOrWhiteSpace(textBox2.Text))

Upvotes: 0

Related Questions