Question Check textbox if empty??

gate7cy

Well-known member
Joined
May 11, 2009
Messages
119
Programming Experience
3-5
I am using VB.net to create a simple winform. I have the option to save the data I input on that form. I am trying to prevent the user not to save the data if textbox10 is empty. I tried the while loop but I go into an infinite loop. The code is below. The if statement gives the error but allows the user to save.

VB.NET:
   While Len(Me.techniciansTextBox.Text) = 0

            MessageBox.Show("Please Enter a Technician To continue")


        End While

Any way how to do this??Thanks for the replies
 
I tried this :

VB.NET:
        If techniciansTextBox.TextLength = 0 Then

            MessageBox.Show("Please enter a Technician for the service!")


        Else
Now the data is not saved but the window closes without allowing me to finish entering all the data and saving it
 
All I provided was a way to detect if the data in the textbox was empty or not, I have no idea where you're needing this code, actually I have no idea what you're code even looks like so I can't tell you why the form closes.
 
I go it resolved. I had a wrong order on the conditional statements. After re-arranging them all is working fine. Thanks for the replies
 
Another way to do this would be to perform some verification logic to everything you want checked:-

VB.NET:
    Private Sub techniciansTextBox_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles techniciansTextBox.KeyUp
        VerifyTextboxes()
    End Sub

    Private Sub operatorsTextBox_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles operatorsTextBox.KeyUp
        VerifyTextboxes()
    End Sub

    Private Sub VerifyTextboxes()
        btnSave.enabled = (techniciansTextBox.Text.Trim.Length > 10) andalso (operatorsTextBox.Text.Trim.Length > 5) andalso .......
    End Sub

If you only want to verify textboxes, you can use multiple handles and shorten the code into something like :-

VB.NET:
    Private Sub VerifyTextboxes(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles techniciansTextBox.KeyUp, operatorsTextBox.KeyUp
        btnSave.enabled = (techniciansTextBox.Text.Trim.Length > 10) andalso (operatorsTextBox.Text.Trim.Length > 5) andalso .......
    End Sub
 
Back
Top