Mhike24
Mhike24

Reputation: 1

How to put many Textbox in one If else statement

This is the question =If the FN button will be click, then it will display a message base on the status of the textbox1 control. For the MN button, if the event is click then it will display a message base on the status of the textbox2 control. The LN button was clicked then it will display a message base on the status of the textbox3 control. Lastly, if the Clear button control will be click and any of the textbox control is not empty then it will clear all the textbox controls, but if all textbox controls are empty then it will show a message that all textbox controls are empty.

this is my code

Public Class Form1

    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
        Label1.Text = "First Name"
    End Sub

    Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click
        Label2.Text = "Middle Name"
    End Sub

    Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click
        Label3.Text = "Last Name"
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Button1.Text = "FN"
        If TextBox1.Text = vbNullString Then
            MessageBox.Show("The First name is empty")
        Else MessageBox.Show("The First name is " & TextBox1.Text)

        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Button2.Text = "MN"
        If TextBox2.Text = vbNullString Then
            MessageBox.Show("The Middle name is empty")
        Else : MessageBox.Show("The Middle name is " & TextBox2.Text)
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Button3.Text = "LN"
        If TextBox3.Text = vbNullString Then
            MessageBox.Show("The Last name is empty")
        Else : MessageBox.Show("The Last name is " & TextBox3.Text)
        End If
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Button4.Text = "Clear"
        If (TextBox1.Text) And (TextBox2.Text) = vbNullString Then
            MessageBox.Show("Textbox control is empty")
        Else : TextBox1.Clear()
        End If
        For Each txt As Control In Me.Controls.OfType(Of TextBox)()
            txt.Text = ""
        Next
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    End Sub

    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged

    End Sub
End Class

My question is , how to work on clear button because i cant do it in Textbox2 and Textbox3 thankyou

Upvotes: 0

Views: 498

Answers (4)

Idle_Mind
Idle_Mind

Reputation: 39132

I'd use a List(Of TextBox) allowing you to leverage All and ForEach:

Public Class Form1

    Private TBs As New List(Of TextBox)

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        TBs.AddRange({TextBox1, TextBox2, TextBox3})
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        If TBs.All(Function(tb) tb.Text = "") Then
            MessageBox.Show("All TextBoxes are empty!")
        Else
            TBs.ForEach(Sub(tb) tb.Clear())
        End If
    End Sub

End Class

Upvotes: 0

Mhike24
Mhike24

Reputation: 1

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click ' Button4.Text = "Clear"

If (TextBox1.Text = vbNullString) And (TextBox2.Text = vbNullString) And (TextBox3.Text = vbNullString) Then
    MessageBox.Show("Textbox controls are empty")
Else
    TextBox1.Text = ""
    TextBox2.Text = ""
    TextBox3.Text = ""
End If

thanks sir Tom Brunberg, this code works for me :)

Upvotes: -1

Tom Brunberg
Tom Brunberg

Reputation: 21033

So, you are asking about Button4_Click.

This is what I was about to suggest first, because clearing all text boxes whether they are empty or not, is faster than first checking and then clearing.

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
'    Button4.Text = "Clear"
'   If (TextBox1.Text) And (TextBox2.Text) = vbNullString Then
'       MessageBox.Show("Textbox control is empty")
'    Else : TextBox1.Clear()
'    End If
    For Each txt As Control In Me.Controls.OfType(Of TextBox)()
        txt.Text = ""
    Next
    MessageBox.Show("Textbox controls are empty")
End Sub

But then, if the intention is to a) just clear all textboxes, without any message if any one has some text. b) show the message only if all are empty.

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
  '  Button4.Text = "Clear"

    If (TextBox1.Text = vbNullString) And (TextBox2.Text = vbNullString) And (TextBox3.Text = vbNullString) Then
        MessageBox.Show("Textbox controls are empty")
    Else
        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
    End If
    
    or (if this works, VB6 is alien for me)
    
    ....
    Else
        For Each txt As Control In Me.Controls.OfType(Of TextBox)()
            txt.Text = ""
        Next
    End If

End Sub

Upvotes: 1

Mang Kepweng
Mang Kepweng

Reputation: 9

Try something like this

For Each ctrl As Control In Me.Controls
    If TypeOf ctrl Is TextBox Then
        CType(ctrl, TextBox).Text = String.Empty
    End If
Next ctrl

Try to add Next txt to your For Each

Upvotes: 1

Related Questions