Checking several textboxes to make sure none are blank?
-
I'm hoping there's a more streamlined approach to this. In a form, I have 7 textboxes and a button. When the button is clicked, each textbox needs to be looked at to ensure it contains something. If the textbox is blank, I have a label on the forum to alert the user of such. Then I direct focus to the blank textbox. So far I've been doing it like this:
if (txt1.text == "") { ErrorLabel.text = "Enter something in txt1"; txt1.focus(); } else if (txt2.text == "") { ErrorLabel.text = "Enter something in txt2"; txt2.focus(); } else if (txt3.text == "") { ErrorLabel.text = "Enter something in txt3"; txt3.focus(); } else if (... and so on, and so on for all 7 textboxes)
Is there a way to do it with less code? -
I'm hoping there's a more streamlined approach to this. In a form, I have 7 textboxes and a button. When the button is clicked, each textbox needs to be looked at to ensure it contains something. If the textbox is blank, I have a label on the forum to alert the user of such. Then I direct focus to the blank textbox. So far I've been doing it like this:
if (txt1.text == "") { ErrorLabel.text = "Enter something in txt1"; txt1.focus(); } else if (txt2.text == "") { ErrorLabel.text = "Enter something in txt2"; txt2.focus(); } else if (txt3.text == "") { ErrorLabel.text = "Enter something in txt3"; txt3.focus(); } else if (... and so on, and so on for all 7 textboxes)
Is there a way to do it with less code? -
I'm hoping there's a more streamlined approach to this. In a form, I have 7 textboxes and a button. When the button is clicked, each textbox needs to be looked at to ensure it contains something. If the textbox is blank, I have a label on the forum to alert the user of such. Then I direct focus to the blank textbox. So far I've been doing it like this:
if (txt1.text == "") { ErrorLabel.text = "Enter something in txt1"; txt1.focus(); } else if (txt2.text == "") { ErrorLabel.text = "Enter something in txt2"; txt2.focus(); } else if (txt3.text == "") { ErrorLabel.text = "Enter something in txt3"; txt3.focus(); } else if (... and so on, and so on for all 7 textboxes)
Is there a way to do it with less code?if you don't want to go with databinding which is a good option, you can combine all the if statements: if(txt1.Text=="" OR txt2.text=="" OR txt3.text="" OR ...) { ErrorLabel.text="All fields must be entered to continue"; } Their still may be better options though.
Regards, Thomas Stockwell Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Visit my homepage Oracle Studios[^]
-
if you don't want to go with databinding which is a good option, you can combine all the if statements: if(txt1.Text=="" OR txt2.text=="" OR txt3.text="" OR ...) { ErrorLabel.text="All fields must be entered to continue"; } Their still may be better options though.
Regards, Thomas Stockwell Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Visit my homepage Oracle Studios[^]
-
Pete O'Hanlon wrote:
With a bit of the magic listed here[^] you can save yourself a lot of work keeping validation and items in sync.
That's a great article. Do you know any similar approach available for web application ?
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
-
Pete O'Hanlon wrote:
With a bit of the magic listed here[^] you can save yourself a lot of work keeping validation and items in sync.
That's a great article. Do you know any similar approach available for web application ?
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
-
I'm hoping there's a more streamlined approach to this. In a form, I have 7 textboxes and a button. When the button is clicked, each textbox needs to be looked at to ensure it contains something. If the textbox is blank, I have a label on the forum to alert the user of such. Then I direct focus to the blank textbox. So far I've been doing it like this:
if (txt1.text == "") { ErrorLabel.text = "Enter something in txt1"; txt1.focus(); } else if (txt2.text == "") { ErrorLabel.text = "Enter something in txt2"; txt2.focus(); } else if (txt3.text == "") { ErrorLabel.text = "Enter something in txt3"; txt3.focus(); } else if (... and so on, and so on for all 7 textboxes)
Is there a way to do it with less code?How about this:
foreach (var tb in new TextBox[] { txt1, txt2, txt3, txt4, txt5, txt6, txt7 }) { if (tb.Text == "") { ErrorLabel.text = string.format("Enter something in {0}", tb.Name); tb.Focus(); break; } }
It aint pretty, and I'd prefer data binding, but it'd work? -
How about this:
foreach (var tb in new TextBox[] { txt1, txt2, txt3, txt4, txt5, txt6, txt7 }) { if (tb.Text == "") { ErrorLabel.text = string.format("Enter something in {0}", tb.Name); tb.Focus(); break; } }
It aint pretty, and I'd prefer data binding, but it'd work?If you set required field validators when tabbing to each textbox it instantly does validation i dont see the issue perhaps ive misinterperted your query
A pessimist sees the difficulty in every opportunity; an optimist sees the opportunity in every difficulty I am a Optimist
-
How about this:
foreach (var tb in new TextBox[] { txt1, txt2, txt3, txt4, txt5, txt6, txt7 }) { if (tb.Text == "") { ErrorLabel.text = string.format("Enter something in {0}", tb.Name); tb.Focus(); break; } }
It aint pretty, and I'd prefer data binding, but it'd work?That's what I would do too... so it must be wrong somehow. :laugh:
-
I'm hoping there's a more streamlined approach to this. In a form, I have 7 textboxes and a button. When the button is clicked, each textbox needs to be looked at to ensure it contains something. If the textbox is blank, I have a label on the forum to alert the user of such. Then I direct focus to the blank textbox. So far I've been doing it like this:
if (txt1.text == "") { ErrorLabel.text = "Enter something in txt1"; txt1.focus(); } else if (txt2.text == "") { ErrorLabel.text = "Enter something in txt2"; txt2.focus(); } else if (txt3.text == "") { ErrorLabel.text = "Enter something in txt3"; txt3.focus(); } else if (... and so on, and so on for all 7 textboxes)
Is there a way to do it with less code?Or set the Button's Enabled property
btn.Enabled = ( txt1.Text.Length > 0 ) && ... ;
-
How about this:
foreach (var tb in new TextBox[] { txt1, txt2, txt3, txt4, txt5, txt6, txt7 }) { if (tb.Text == "") { ErrorLabel.text = string.format("Enter something in {0}", tb.Name); tb.Focus(); break; } }
It aint pretty, and I'd prefer data binding, but it'd work?I'll give this a shot .. looks interesting I'd do databinding, but not sure how. I'm still new at this. If I created the DB within Visual Studio I'd be able to do it, but it's created at runtime (SQL CE) EDIT: I just tried it, and it worked great. Thanks!!!
modified on Friday, April 4, 2008 1:36 PM