-
IF sectence
I have two textboxes.
txtbox1
txtbox2
I want to make a IF sectence which does this:
When i do NOT write txt in txtbox1, the other textbox, txtbox2, will be set to visibility = false
This require that the IF sectence can notice if there is any text in txtbox1.
How can i do this?
-
Re: IF sectence
Just call this method to wherever you want to do the checking. You could also make a string variable and check its length, but for a basic solution this should work.
private void checkIfText()
{
if(textbox1.Text != "")
{
textbox2.visible=true;
textbox2.enabled=true;
}
else
{
textbox2.visible=false;
textbox2.enabled=false;
}
}
-
Re: IF sectence
There's really no reason to make that so verbose. You are already evaluating a boolean in the if statement, so you can accomplish the same thing with a single line.
Code:
textbox2.Visible = (textbox1.Text != String.Empty);
You don't need to set Enabled since an invisible textbox cannot be interacted with anyway.
I would handle the TextChanged event of textbox1 and place that code there.
-
Re: IF sectence
I would use the LostFocus or the Leave event of the first textbox and then do what the above post says. If you are populating the textbox as it loads, you may want to check if the form is visible first and then make this check.