I have two text boxes and I want skip a block of code only when both are empty:
if (txtBox1.Text.Trim() != string.Empty && txtBox2.Text.Trim() != string.Empty )
{
// Do something
}
If either of the text boxes has something, I want the //Do something part to execute. Only when both are empty do I want to skip.
However the above code fragment doesn't work. Why?
Re: if statement returning only false condition value Then, it evaluates if TRUE (or FALSE) is less than C16. That's where your formula breaks as it will always return FALSE. Now, you could turn a TRUE or a FALSE into a 1 or a 0, but then the outcome would most likely always be TRUE.
FALSE Function in Excel. The FALSE function in Excel is a logical function that returns “FALSE” as an output when used in a blank cell. This function also does not take any arguments similar to the TRUE function in Excel.
You can prevent these indicators from being displayed by using the following procedure. In Excel 2016, Excel 2013, and Excel 2010: Click File > Options >Formulas. > Excel Options > Formulas. Under Error Checking, clear the Enable background error checking check box.
The reason for this is that the 'words' TRUE and FALSE are actually the numbers 1 and 0 in Excel (in most cases). So when you are creating the logical test in the IF function, you must treat it like a number i.e. no inverted commas.
I believe you have your logical operators mixed up. What you're looking for is
if (txtBox1.Text.Trim() != string.Empty || txtBox2.Text.Trim() != string.Empty )
{
// Do something
}
You should replace your &&
with ||
. Currently, the code in the if
block will only be executed if both text fields have text in them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With