Results 1 to 7 of 7

Thread: Check textbox if empty??

  1. #1
    gate7cy is offline VB.NET Forum Fanatic
    .NET Framework
    .NET 3.5
    Join Date
    May 2009
    Posts
    119
    Reputation
    71

    Check textbox if empty??

    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.

    Code:
       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

  2. #2
    JuggaloBrotha's Avatar
    JuggaloBrotha is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2004
    Location
    Lansing, MI; USA
    Posts
    4,364
    Reputation
    977
    Code:
    If techniciansTextBox.Text.Trim.Length = 0I Then
        'Has data in it
    Else
        'Is empty
    End If
    Currently using: VS 2010 Ultimate on Win7 Ultimate x64.


  3. #3
    gate7cy is offline VB.NET Forum Fanatic
    .NET Framework
    .NET 3.5
    Join Date
    May 2009
    Posts
    119
    Reputation
    71
    Did not work as the pop up shows up but still my data is saved to the db which I do not want.

  4. #4
    gate7cy is offline VB.NET Forum Fanatic
    .NET Framework
    .NET 3.5
    Join Date
    May 2009
    Posts
    119
    Reputation
    71
    I tried this :

    Code:
            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

  5. #5
    JuggaloBrotha's Avatar
    JuggaloBrotha is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2004
    Location
    Lansing, MI; USA
    Posts
    4,364
    Reputation
    977
    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.
    Currently using: VS 2010 Ultimate on Win7 Ultimate x64.


  6. #6
    gate7cy is offline VB.NET Forum Fanatic
    .NET Framework
    .NET 3.5
    Join Date
    May 2009
    Posts
    119
    Reputation
    71
    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

  7. #7
    InertiaM is offline VB.NET Forum Idol
    .NET Framework
    .NET 2.0
    Join Date
    Nov 2007
    Location
    Kent, UK
    Posts
    663
    Reputation
    250
    Another way to do this would be to perform some verification logic to everything you want checked:-

    Code:
        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 :-

    Code:
        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
    Always parameterize your queries - read more here

    "When people discover the center of the universe, a lot of them will be disappointed to find they are not it."

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking