Hi guys need help here.
Scenario:
I have a function in my textbox1 with the got focus event. I have a 40 textbox in my goupbox1. Here is my code so far.

Private Sub Textbox1_GotFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Textbox1_GotFocus
...
...

For Each x As TextBox In Me.GroupBox1.Controls.OfType(Of TextBox)()
     ' Im stuck here
Next
End Sub

As you can see guys, I want that when the textbox1 gotFocus, all my textbox in groupbox1 under the for loop event will declare that only numbers are allowed to be entered in all the textbox. I manage to work with it but only in a single textbox. Here is my code for it.

Private Sub Textbox1_keypress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Textbox1.KeyPress
        If e.KeyChar <> ChrW(Keys.Back) Then
            If Char.IsNumber(e.KeyChar) Then

            Else
                MessageBox.Show("Invalid Input! Numbers Only.", "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
                e.Handled = True
            End If
        End If
    End Sub

My question is, Is there a way or would it be posibble to put this code # 2 into the code # 1? So that I don't have to declare 40 TEXTBOX which is not good. Please advise me or help me with this code.

Private Sub Textbox1_TextChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Textbox1.TextChanged

if IsNumeric(TextBox1.Text) THen 
   'do nothing 
else 
   'display error message
   messagebox.show("Numbers Only", "Error")
   'clear textbox 
TextBox1.Text = ""
End If

hi see below code:

[B][U]Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress, TextBox4.KeyPress, TextBox3.KeyPress, TextBox2.KeyPress[/U][/B]
        If e.KeyChar <> ChrW(Keys.Back) Then
            If Char.IsNumber(e.KeyChar) Then

            Else
                e.Handled = True

            End If
        End If

    End Sub

Notice Handles .. this event handles textbox 1,2,3,4.
u can add how many u like

HI thanks for this rply. I forgot to post yesterday but a friend of mine offers me the same aproach. I think theres no other way, but its ok, i just declare textbox1 - 40 though. wooooww... hehehe.. Chao!

pls always remember to mark thread as solved when you've finally got a solution

See if this helps.

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For Each ctl As Control In Me.Controls '// Loop thru all controls.
            If TypeOf ctl Is TextBox Then '// Locate TextBoxes.
                AddHandler ctl.KeyPress, AddressOf txt_KeyPress '// Associate the KeyPress Event with the TextBox.
            End If
        Next
    End Sub
    '// Renamed from: Private Sub TextBox1_KeyPress
    Private Sub txt_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) ' Handles TextBox1.KeyPress
        If e.KeyChar <> ChrW(Keys.Back) Then
            If Char.IsNumber(e.KeyChar) Then

            Else
                MessageBox.Show("Invalid Input! Numbers Only.", "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
                e.Handled = True
            End If
        End If
    End Sub
End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.