2

I already can make the login for user, so if user type their username or password wrongly in textbox, the message label that says "Invalid username or password" appear. But, i want to when user type a single character or number in textbox when the message label is appear, the message label will not visible to user (visible = false) as user already type a single character or number in textbox. But the message label didn't disappear when user type a single character or number.

This is the code:

private void CheckTextBox(object sender, EventArgs e) { if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text)) { label5.Visible = true; } else { label5.Visible = false; } } 

And here is the image:

Below image is when user type wrongly (the username or password), the message label appear:

enter image description here

Below image is when user type a single character or number, but the message label still at there

enter image description here

My question is: How do i set the message label to not show when user type a single character or number in the textbox?

Any help?

Your answer will be great appreciated!

Thank you!

1
  • 1
    You need to make sure this CheckTextBox method is wired up to the TextChanged event of the checkbox. Commented Feb 22, 2014 at 6:33

5 Answers 5

2

Problem : You have not wiredup the CheckTextBox() method for both TextBox1 and TextBox2 TextChanged Event.

Solution : in your Form_Load WireUp the CheckTextBox() method for the Textbox1 and TextBox2 TextChanged Event as below:

 private void Form1_Load(object sender, EventArgs e) { textBox1.TextChanged += new System.EventHandler(this.CheckTextBox); textBox2.TextChanged += new System.EventHandler(this.CheckTextBox); } 

Suggestion : i think string.IsNullOrWhiteSpace() is more appropriate as it would also check for Whitespace in addition to null and Empty strings.

Try This:

 private void CheckTextBox(object sender, EventArgs e) { if (string.IsNullOrWhiteSpace(textBox1.Text) || string.IsNullOrWhiteSpace(textBox2.Text)) { label5.Visible = true; } else { label5.Visible = false; } } 
Sign up to request clarification or add additional context in comments.

Comments

1

This line is checking if either textbox has information in it.

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

Change the || to && and then the label will only be shown when BOTH textboxes do not have any data.

Comments

1

If I understand you correctly, try this:

private void CheckTextBox(object sender, EventArgs e) { if (string.IsNullOrEmpty(textBox1.Text) && string.IsNullOrEmpty(textBox2.Text)) { label5.Visible = true; } else { label5.Visible = false; } } 

If you change || to && then label5 will be visible only if both textboxes are empty.

Comments

1
if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text)) 

Try this..use && instead of ||

private void CheckTextBox(object sender, EventArgs e) { if (string.IsNullOrEmpty(textBox1.Text) && string.IsNullOrEmpty(textBox2.Text)) { label5.Visible = true; } else { label5.Visible = false; } } 

1 Comment

@Kaoru sir Glad i cound help
1

Check this code, Should be call this OnTextChanged="CheckTextBox" on your textbox

protected void CheckTextBox(object sender, EventArgs e) { if (string.IsNullOrEmpty(textBox1.Text) && string.IsNullOrEmpty(textBox2.Text)) { label5.Visible = true; } else { label5.Visible = false; } } 

<asp:TextBox ID="TextBox1" runat="server" OnTextChanged="CheckTextBox"></asp:TextBox> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.