| | |
how to use if-else statement for multiple condition
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2006
Posts: 436
Reputation:
Solved Threads: 72
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;
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:
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)
buttonOK.Enabled = !string.IsNullOrEmtpy(textbox1.Text) && !string.IsNullOrEmpty(textbox2.Text) && !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)
if( condition ) { ... } else if (condition2) { ... } else if( condition3) { ... }
Last edited by JerryShaw; Feb 13th, 2009 at 10:19 am.
•
•
Join Date: Jul 2007
Posts: 276
Reputation:
Solved Threads: 37
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
or however you want to handle it. You could use multiple if statements as well to set your message specifically as
C# Syntax (Toggle Plain Text)
If(textbox1 == "" || textbox2 == "" || textbox3 == "") 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
![]() |
Similar Threads
- multiple condition update (Visual Basic 4 / 5 / 6)
- Multiple conditions in one IF statement? (C#)
- breaking out of multiple loops (C)
- Boolean Algebra (C++)
- Waiting Room Access DB (Visual Basic 4 / 5 / 6)
- switch/case statement (C++)
Other Threads in the C# Forum
- Previous Thread: Writing an alternate logon service for windows?
- Next Thread: c# webBrowser - change text size of webpage displayed
| Thread Tools | Search this Thread |
.net access ado.net algorithm array barchart bitmap box broadcast buttons c# chat check checkbox client color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development draganddrop drawing encryption enum event excel file files form format formatting forms function gdi+ httpwebrequest image index input install java label list listbox listener mandelbrot math mouseclick mysql networking object operator oracle path photoshop picturebox pixelinversion post prime programming radians regex remote remoting richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation view visualstudio webbrowser windows winforms wpf xml






