943,692 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 5595
  • C# RSS
Feb 13th, 2009
0

how to use if-else statement for multiple condition

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Digvijaysinh is offline Offline
9 posts
since Feb 2009
Feb 13th, 2009
0

Re: how to use if-else statement for multiple condition

Pseudocode :
if textbox1 is blank then
do something
else if texbox2 is blank then
do something
else if textbox3 is blank then
do something
else
do something
Reputation Points: 2023
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,736 posts
since Oct 2008
Feb 13th, 2009
0

Re: how to use if-else statement for multiple condition

Typically this question comes up when you don't want the user to leave a dialog form until all of the required text boxes are populated.

If that is the case here, then do not enable the Ok button until the required textboxes are populated. To test when to enable the ok button, write a single event handler for the TextChanged event and assign it to all three textbox controls.

In the event handler;

C# Syntax (Toggle Plain Text)
  1. buttonOK.Enabled = !string.IsNullOrEmtpy(textbox1.Text)
  2. && !string.IsNullOrEmpty(textbox2.Text)
  3. && !string.IsNullOrEmpty(textbox3.Text);

This is much better than a bunch of if..else..else statements.

If this is not a modal dialog type form, and you really need to use if..else statements, then use the format:

C# Syntax (Toggle Plain Text)
  1.  
  2. if( condition )
  3. {
  4. ...
  5. }
  6. else if (condition2)
  7. {
  8. ...
  9. }
  10. else if( condition3)
  11. {
  12. ...
  13. }
Last edited by JerryShaw; Feb 13th, 2009 at 10:19 am.
Reputation Points: 69
Solved Threads: 75
Posting Pro in Training
JerryShaw is offline Offline
465 posts
since Nov 2006
Feb 13th, 2009
0

Re: how to use if-else statement for multiple condition

Did you get your answer or still you need some additional info? I think the answer was already been well-said here. I was supposed to add mine but I noticed the complete replies ... so I just want to ask if you are okay now or what.
Reputation Points: 10
Solved Threads: 1
Light Poster
yollyP. is offline Offline
34 posts
since Dec 2008
Feb 13th, 2009
0

Re: how to use if-else statement for multiple condition

Personally I like the required field validators for this sort of stuff, but if you wanted an if statement you can also combine them into one

C# Syntax (Toggle Plain Text)
  1. If(textbox1 == "" || textbox2 == "" || textbox3 == "")
  2. label = "Hey dude you left a textbox blank on us"

or however you want to handle it. You could use multiple if statements as well to set your message specifically as
Reputation Points: 155
Solved Threads: 41
Posting Whiz in Training
rapture is offline Offline
294 posts
since Jul 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Writing an alternate logon service for windows?
Next Thread in C# Forum Timeline: c# webBrowser - change text size of webpage displayed





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC