locked
how to check if the text box is empty. RRS feed

  • Question

  • Hi.

    How can i check the value of text box if it empty or not.

    i have 1 combo box and button. the combobox will display project name from data table. and 4 text box txt1, txt2, txt3, and txt4.

    i want to do then when i click the button it will pass the value of combo box to the text box. if the txt1 is emty then pass it to txt1, if txt1 not empty pass it to txt2 and so on. can i do that.?

    then when selected the item in the combo box and select another it change to "System.Data.DataRowView" and not display the data anymore how can i make it to display the data.

     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            Dim project As String
            project = CStr(ComboBox2.SelectedText)
    
            If TextBox1.Text = "" Then
                TextBox1.Text = project
            ElseIf TextBox1.Text.Trim <> "" Then
                TextBox2.Text = project
            ElseIf TextBox1.Text.Trim <> "" And TextBox2.Text.Trim <> "" Then
                TextBox3.Text = project
            ElseIf TextBox1.Text.Trim <> "" And TextBox2.Text.Trim <> "" And TextBox3.Text.Trim <> "" Then
                TextBox4.Text = project
            Else
            End If
    
        End Sub

    i make this code. but it does not work.


    ~SS~

    Tuesday, December 27, 2016 2:14 AM

Answers

  • How can i check the value of text box if it empty or not.

    i have 1 combo box and button. the combobox will display project name from data table. and 4 text box txt1, txt2, txt3, and txt4.

    i want to do then when i click the button it will pass the value of combo box to the text box. if the txt1 is emty then pass it to txt1, if txt1 not empty pass it to txt2 and so on. can i do that.?

     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            Dim project As String
            project = CStr(ComboBox2.SelectedText)
    
            If TextBox1.Text = "" Then
                TextBox1.Text = project
            ElseIf TextBox1.Text.Trim <> "" Then
                TextBox2.Text = project
            ElseIf TextBox1.Text.Trim <> "" And TextBox2.Text.Trim <> "" Then
                TextBox3.Text = project
            ElseIf TextBox1.Text.Trim <> "" And TextBox2.Text.Trim <> "" And TextBox3.Text.Trim <> "" Then
                TextBox4.Text = project
            Else
            End If
    
        End Sub

    i make this code. but it does not work.

    Rethink your logic. It doesn't appear to match your description.

    For example, why do you set TextBox2.Text to project after only
    checking if TextBox1.Text is empty? And so on down the line.

    According to your description you should only be setting a TextBox to
    project if it's empty and no prior TextBoxes are empty. More like this:

    If TextBox1.Text = "" Then    
        TextBox1.Text = project   
    ElseIf TextBox2.Text = "" Then
        TextBox2.Text = project   
    ElseIf TextBox3.Text = "" Then
        TextBox3.Text = project   
    ElseIf TextBox4.Text = "" Then
        TextBox4.Text = project   
        ' Else no place to put it!     
    End If                        
    

    - Wayne

    • Proposed as answer by Ashidacchi Tuesday, December 27, 2016 4:18 AM
    • Marked as answer by Syafiq Syahman Wednesday, December 28, 2016 12:28 AM
    Tuesday, December 27, 2016 3:49 AM
  • Try this solution too:

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim project = ComboBox2.Text
        Dim textboxes = {TextBox1, TextBox2, TextBox3, TextBox4}
        Dim first = textboxes.FirstOrDefault(Function(tb) String.IsNullOrWhiteSpace(tb.Text))
        If first IsNot Nothing Then first.Text = project
    End Sub
    

    • Proposed as answer by Frank L. Smith Tuesday, December 27, 2016 2:00 PM
    • Marked as answer by Syafiq Syahman Wednesday, December 28, 2016 12:28 AM
    Tuesday, December 27, 2016 10:01 AM

All replies

  • How can i check the value of text box if it empty or not.

    i have 1 combo box and button. the combobox will display project name from data table. and 4 text box txt1, txt2, txt3, and txt4.

    i want to do then when i click the button it will pass the value of combo box to the text box. if the txt1 is emty then pass it to txt1, if txt1 not empty pass it to txt2 and so on. can i do that.?

     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            Dim project As String
            project = CStr(ComboBox2.SelectedText)
    
            If TextBox1.Text = "" Then
                TextBox1.Text = project
            ElseIf TextBox1.Text.Trim <> "" Then
                TextBox2.Text = project
            ElseIf TextBox1.Text.Trim <> "" And TextBox2.Text.Trim <> "" Then
                TextBox3.Text = project
            ElseIf TextBox1.Text.Trim <> "" And TextBox2.Text.Trim <> "" And TextBox3.Text.Trim <> "" Then
                TextBox4.Text = project
            Else
            End If
    
        End Sub

    i make this code. but it does not work.

    Rethink your logic. It doesn't appear to match your description.

    For example, why do you set TextBox2.Text to project after only
    checking if TextBox1.Text is empty? And so on down the line.

    According to your description you should only be setting a TextBox to
    project if it's empty and no prior TextBoxes are empty. More like this:

    If TextBox1.Text = "" Then    
        TextBox1.Text = project   
    ElseIf TextBox2.Text = "" Then
        TextBox2.Text = project   
    ElseIf TextBox3.Text = "" Then
        TextBox3.Text = project   
    ElseIf TextBox4.Text = "" Then
        TextBox4.Text = project   
        ' Else no place to put it!     
    End If                        
    

    - Wayne

    • Proposed as answer by Ashidacchi Tuesday, December 27, 2016 4:18 AM
    • Marked as answer by Syafiq Syahman Wednesday, December 28, 2016 12:28 AM
    Tuesday, December 27, 2016 3:49 AM
  • I think WayneAKing's code is the simplest.

    And I'd like to add some.
    Dim project As String
    project = ComboBox2.Text.Trim	' --<< trim here
    Regards,
    Ashidacchi
    Tuesday, December 27, 2016 4:22 AM
  • Try this solution too:

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim project = ComboBox2.Text
        Dim textboxes = {TextBox1, TextBox2, TextBox3, TextBox4}
        Dim first = textboxes.FirstOrDefault(Function(tb) String.IsNullOrWhiteSpace(tb.Text))
        If first IsNot Nothing Then first.Text = project
    End Sub
    

    • Proposed as answer by Frank L. Smith Tuesday, December 27, 2016 2:00 PM
    • Marked as answer by Syafiq Syahman Wednesday, December 28, 2016 12:28 AM
    Tuesday, December 27, 2016 10:01 AM
  • Thanks... Both code works...Thank You guys...

    ~SS~

    Wednesday, December 28, 2016 12:29 AM