How do I prompt the user if a textbox is enabled but empty?

I'm using VB.NET 2003. The form allows the user to activate various textboxes by checking a checkbox if they want to enter text in the textbox. When the user presses "Submit," I want a Msgbox to appear warning users if they have enabled a textbox but haven't entered any text. However, there are multiple opportunities on the form for this to have occurred (i.e. several sections to activate textboxes and enter text). Is there a simple way to have VB.NET check the entire form for instances of enabled textboxes with no text entered?

Thanks.
[565 byte] By [irishman] at [2007-11-20 10:07:33]
# 1 Re: How do I prompt the user if a textbox is enabled but empty?
For Each o As Control In Me.Controls
If TypeOf o Is TextBox AndAlso o.Enabled AndAlso o.Text.Trim().Length = 0 Then
MsgBox(String.Format("{0} textbox is empty.", o.Name), MsgBoxStyle.Information)
End If
Next
Thread1 at 2007-11-10 3:08:44 >
# 2 Re: How do I prompt the user if a textbox is enabled but empty?
A nice touch would be to set off the error provider control when you spin those text boxes
reeper at 2007-11-10 3:09:44 >
# 3 Re: How do I prompt the user if a textbox is enabled but empty?
The thing to do would be to not enable the Submit button until all required fields have been populated. As reeper suggests, use an ErrorProvider set an error message on all empty fileds to begin with and disable the Submit button. As each field is validated only enable the Submit button if all required fields have been set.
jmcilhinney at 2007-11-10 3:10:43 >
# 4 Re: How do I prompt the user if a textbox is enabled but empty?
The thing to do would be to not enable the Submit button until all required fields have been populated. As reeper suggests, use an ErrorProvider set an error message on all empty fileds to begin with and disable the Submit button. As each field is validated only enable the Submit button if all required fields have been set.

This is a good idea only if your creating a project that you know you'll never update ever again. (Like a school project or a learning tutorial).

If this is a real peice of software, I would go with the above example that loops through all of the controls when the user hits submit.

The reason? If you loop through the controls in the submit button, then you can add new fields to this form in the future without having to change or add any code. If you have Submit disabled until all fields have a falue, adding a new field to the form requires you to change all of the other fields code to include the new field, as well as copying that code into the field you just added. It's not a hard update but you still have to do it... and forgetting to do so will cause a logic error in your program that you might not find for sometime causing the risk of invalid data being written to the database.
MeatLander at 2007-11-10 3:11:51 >
# 5 Re: How do I prompt the user if a textbox is enabled but empty?
The reason? If you loop through the controls in the submit button, then you can add new fields to this form in the future without having to change or add any code. If you have Submit disabled until all fields have a falue, adding a new field to the form requires you to change all of the other fields code to include the new field, as well as copying that code into the field you just added. It's not a hard update but you still have to do it... and forgetting to do so will cause a logic error in your program that you might not find for sometime causing the risk of invalid data being written to the database.That's completely untrue if you've written your code correctly. If you were to validate your controls when you press submit then you have to go through each control. If you add a new control then you have to add it to the list that requires checking.

If you were to disable the the Submit button then you still only have to add any new controls in one place because you do all the checking in one method and you call that method from the validated event of each control. No code written for those existing controls has to change if and when you add new controls.

Good design is good design whether a project is for school or commercial sale. You wouldn't distribute an inferior interface in commercial software compare to a school project. Well written code is well written code and poorly written code isn't.
jmcilhinney at 2007-11-10 3:12:50 >
# 6 Re: How do I prompt the user if a textbox is enabled but empty?
In theory, if the submit button is only usable when all fields are valid, then it should indeed be disabled until all fields are valid. Think of a EULA agreement dialog.

With that being said; while some of us know what you guys are debating about, code samples for your ideas would surely clear this up for someone that isn't as familiar.
Craig Gemmill at 2007-11-10 3:13:49 >
# 7 Re: How do I prompt the user if a textbox is enabled but empty?
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Button1.Enabled = False
End Sub

Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
Me.SetButton1State()
End Sub

Private Sub TextBox2_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox2.Leave
Me.SetButton1State()
End Sub

Private Sub SetButton1State()
Me.Button1.Enabled = Me.TextBox1.Text.Trim() <> String.Empty AndAlso _
Me.TextBox2.Text.Trim() <> String.Empty
End Sub
jmcilhinney at 2007-11-10 3:14:47 >