Reputation: 25
I am trying to create an if else statement to show a message box if no input.
If(textbox1.text==false)
{
messagebox.show("please fill in the boxes")
}
I have 16 different text box currently out, do i need to use an if else statement for each?
Upvotes: 0
Views: 2683
Reputation: 32445
Pass all TextBoxes
in the list and loop it
//Create list once in the constructor of main form or window
List<TextBox> list = new List<TextBox>()
//...
list.Add(textbox1);
list.Add(textbox2);'
//...
Then loop it
foreach(TextBox txt in list)
{
if(String.IsNullOrWhiteSpace(txt.Text))
{
messagebox.Show("please fill in the boxes");
break;
}
}
Update
If all textboxes expecting only number/double input then use TryParse
for checking if value is valid
foreach(TextBox txt in list)
{
Double temp;
if(Double.TryParse(txt.Text, temp) == true)
{
//save or use valid value
Debug.Print(temp.ToString());
}
else
{
messagebox.Show("please fill in the boxes");
break;
}
}
Upvotes: 3
Reputation: 7022
String and Boolean aren't comparable, also you can check if all the textfields are empty like described in this post
if(this.Controls.OfType<TextBox>().Any(t => string.IsNullOrEmpty(t.Text)) {
//Textfield is empty
}
Upvotes: 2
Reputation: 1464
You can not compare string with Boolean. textbox.text is a string data type. try this, if you want to show different message for different textbox, you must use if-else statement for all texboxes.
If(textbox1.text=="")
{
messagebox.show("please fill in the boxes")
}
or
If(string.IsNullOrEmpty(textbox1.text) == true)
{
messagebox.show("please fill in the boxes")
}
for multiple textbox
validation
Adding the handler to the textboxes is easily done with a foreach loop in the form constructor:
foreach(TextBox tb in this.Controls.OfType<TextBox>().Where(x => x.CausesValidation == true))
{
tb.Validating += textBox_Validating;
}
use validating
event to handle it
private void textBox_Validating(object sender, CancelEventArgs e)
{
TextBox currenttb = (TextBox)sender;
if(currenttb.Text == "")
MessageBox.Show(string.Format("Empty field {0 }",currenttb.Name.Substring(3)));
e.Cancel = true;
else
{
e.Cancel = false;
}
}
Upvotes: 2
Reputation: 1243
First of all, you have a type mismatch error in your question. The Text
property of a TextBox
is of string
type, while the keyword false
is of bool
type. You can read more on types in here.
The fix to this issue would be:
If (!string.IsNullOrEmpty(textbox1.Text))
{
Messagebox.Show("please fill in the boxes")
}
Secondly, modern programming is all about DRY principle. So, the answer is no, you do not need to write the same piece of code for each of them.
You could actually do it at least two ways. The first way would be to create some sort of collection of your textboxes (an array, for example). Then you would create a method to iterate over this collection like this:
private bool AllTextboxesAreFilled()
{
var textboxes = new TextBox[] { textBox1, textBox2, textBox3 };
return textboxes.All(textbox => !string.IsNullOrEmpty(textbox.Text));
}
And then call it like:
if (!AllTextboxesAreFilled())
{
MessageBox.Show("please fill in the boxes");
}
The second way would be to make these textboxes children of some control (a Panel
, for example) and then iterate over these children. This way you don't need to create an additional collection (and to remember to add elements in it in case you need more textboxes):
private bool AllTextboxesAreFilled()
{
return holderPanel.Controls.OfType<TextBox>().All(textbox => !string.IsNullOrEmpty(textbox.Text));
}
The usage is the same as in the previous example.
Upvotes: 1