If-Else statement in C#

Solved/Closed
Digvijaysinh Posts 10 Registration date Tuesday January 27, 2009 Status Member Last seen July 6, 2009 - Feb 13, 2009 at 05:06 AM
 jes_mar - Jul 8, 2011 at 11:51 AM
Hello,
hii
friends, I just want to know how to create if-else statement
where we dont allow user to left any textbox blank in our window application , suppose we have 3 textbox then please suggest me how to create if - else statement for this in c#.
thanx in advance.

2 responses

Using the if Statement


The if statement is perhaps the most basic of flow control options available to the C# programmer. Programmers who are familiar with C, C++ or Java will immediately be comfortable using C# if statements.
The basic syntax of C# if statement is as follows:
if (boolean expression) {
// C# code to be performed when expression evaluates to true here
}
Essentially if the boolean expression evaluates to true (see C# Operators and Expressions for more details of this type of logic) then the code in the body of the statement is executed. The body of the statement is enclosed in braces ({}). If, on the other hand, the expression evaluates to false the code in the body of the statement is skipped.
For example, if a decision needs to be made depending on whether one value is greater than another:
int x = 10;

if ( x > 9 )
{
System.Console.WriteLine ("x is greater than 9!");
}
Clearly, x is indeed greater than 9 causing the message to appear in the console window.
Using if ... else .. Statements

The next variation of the if statement allows us to also specify some code to perform if the expression in the if statement evaluates to false. The syntax for this construct is as follows:
if (boolean expression) {
// Code to be executed if expression is true
} else {
// Code to be executed if expression is false
}
Using the above syntax, we can now extend our previous example to display a different message if the comparison expression evaluates to be false:

int x = 10;

if ( x > 9 )
{
System.Console.WriteLine ("x is greater than 9!");
}
else
{
System.Console.WriteLine ("x is less than 9!");
}

In this case, the second WriteLine statement would execute if the value of x was less than 9.
Using if ... else if .. Statements

So far we have looked at if statements which make decisions based on the result of a single logical expression. Sometimes it becomes necessary to make decisions based on a number of different criteria. For this purpose we can use the if ... else if ... construct, the syntax for which is as follows:
int x = 9;

if (x == 10)
{
System.Console.WriteLine ("x is 10");
}
else if (x == 9)
{
System.Console.WriteLine ("x is 9");
}
else if (x == 8)
{
System.Console.WriteLine ("x is 8");
}
This approach works well for a moderate number of comparisons, but can become cumbersome for a larger volume of expression evaluations. For such situations, the C# switch statement provides a more flexible and efficient solution. For more details on using the switch statement read the chapter entitled The C# switch Statement.
Summary

In this chapter we looked at the use of C# if statements including if, if ... else and if ... else if ... constructs. In the next chapter of this book we will look at using the C# switch statement as an alternative to more complex if statements.
4
thank you very much
0
Thank you very much
0
thank'z you so much...huhu :)
0
fogsurfer Posts 1 Registration date Wednesday February 18, 2009 Status Member Last seen February 19, 2009
Feb 19, 2009 at 05:27 AM
//suppose you named the textbox textBox1


//This is should be the Leave event handler for the textbox
private void textBox1_Leave(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("Tell them that they should not leave the textbox emty");
textBox1.Focus(); //this will prenvet them from leaving the textbox as long as it es empty
}
}
-2