Reputation: 3689
i'd like to hide item (panel) when textbox value is 0 or null.
if (Wier_r1.Text == null | Wier_r1.Text=="0") panelR1.Visible = false;
else panelR1.Visible = true;
Why it doesn't work when in texbox is nothing - null. With 0 is working.
Upvotes: 3
Views: 5506
Reputation: 216273
Because TextBox.Text is never null. Is an empty string.
Your test should be:
if (Wier_r1.Text == string.Empty || Wier_r1.Text=="0")
panelR1.Visible = false;
else
panelR1.Visible = true;
and the correct OR opertator in this case is || not |.
Upvotes: 5
Reputation: 629
Treat as object and use .equal for it..
if (Wier_r1.Text.equals("") || Wier_r1.equals(0))
panelR1.Visible = false;
else
panelR1.Visible = true;
Upvotes: 0
Reputation: 3334
I don't think a text box will ever return null for the Text value. I believe it returns an empty string.
You'd want to do something like this I think:
if( string.IsNullOrEmpty(Wier_r1.Text) || Wier_r1.Text == "0"){
// Do something
}
Upvotes: 7
Reputation: 1996
I would probably do it like this
panelR1.Visible = !(string.IsNullOrEmpty(Wier_r1.Tex) || Wier_r1.Text=="0");
And as others have stated, the logical or operator in C# is ||, a single | is a bitwise or.
Upvotes: 0
Reputation: 14611
panelR1.Visible = !string.IsNullOrEmpty(Wier_r1.Text) && Wier_r1.Text != "0";
Upvotes: 4
Reputation: 865
I'm not sure the Text property of a textfield is ever null. In doubt, use this method:
if (System.String.IsNullOrEmpty(Wier_r1.Text) || Wier_r1.Text=="0") panelR1.Visible = false;
I'm assuming you want to hide the panel when there is no text, right? That would be an empty string.
Upvotes: 0
Reputation: 1062600
.Text
on a TextBox
is not going to be null
. If you mean a blank string, then test against ""
, or use:
if(string.IsNullOrEmpty(Wier_r1.Text) || Wier_r1.Text == "0") {...}
Note also the difference between |
and ||
which changes it to a short-circuiting "or" (i.e. if the first is true it won't evaluate the second) - which is usually prefreable, but which shouldn't change the logic in this particular scenario,
Upvotes: 4
Reputation: 38179
Try (|| operator)
if (string.IsNullOrEmpty(Wier_r1.Text) || Wier_r1.Text=="0")
Or short form:
panelR1.Visible = (!string.IsNullOrEmpty(Wier_r1.Text)) && (Wier_r1.Text != "0");
Upvotes: 1
Reputation: 28625
Textboxes dont have nulls stored in them; they are always a string so you need to check for "".
if (Wier_r1.Text == "" || Wier_r1.Text=="0")
panelR1.Visible = false;
else
panelR1.Visible = true;
Upvotes: 1
Reputation: 22323
try:You are missing to use or ||
properly.
if (Wier_r1.Text == null || Wier_r1.Text=="0") panelR1.Visible = false;
else panelR1.Visible = true;
Upvotes: 1