how to use if-else statement for multiple condition

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2009
Posts: 9
Reputation: Digvijaysinh is an unknown quantity at this point 
Solved Threads: 0
Digvijaysinh Digvijaysinh is offline Offline
Newbie Poster

how to use if-else statement for multiple condition

 
0
  #1
Feb 13th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,977
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 288
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

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

 
0
  #2
Feb 13th, 2009
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
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 436
Reputation: JerryShaw is on a distinguished road 
Solved Threads: 72
JerryShaw JerryShaw is offline Offline
Posting Pro in Training

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

 
0
  #3
Feb 13th, 2009
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;

  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:

  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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 33
Reputation: yollyP. is an unknown quantity at this point 
Solved Threads: 1
yollyP. yollyP. is offline Offline
Light Poster

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

 
0
  #4
Feb 13th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 276
Reputation: rapture has a spectacular aura about rapture has a spectacular aura about 
Solved Threads: 37
rapture rapture is offline Offline
Posting Whiz in Training

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

 
0
  #5
Feb 13th, 2009
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

  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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC