Problem with GroupBox when calling member event vb.net 2005
I have this class:
Public Class common
Public Function NumbersOnlyEvent(ByVal CtrlName As String, ByVal type As String, ByVal formName As Object) As String
Dim ctrlType As String = "System.Windows.Forms." & type
For Each objcontrol As Control In formName.Controls
If objcontrol.GetType.ToString = ctrlType And objcontrol.Name.Contains(CtrlName) Then
AddHandler objcontrol.KeyPress, AddressOf Number开发者_如何学编程sOnlyHandler
End If
Next
Return True
End Function 'NumbersOnlyEvent
Private Sub NumbersOnlyHandler(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
If Char.IsDigit(e.KeyChar) Or e.KeyChar = Chr(8) Then
e.Handled = False
Else
e.Handled = True
End If
End Sub
End Class
And i have this Form Class:
Public Class addbet
Dim CommonFunc As common = New common
Private Sub addbet_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'AddHandler Events
CommonFunc.NumbersOnlyEvent("txtSbcNum", "TextBox", Me)
End Sub
End Class
And i have this textboxes:
txtSbcNum1
txtSbcNum2
.
.
.
txtSbcNum15
When I add an eventhandler by calling CommonFunc.NumbersOnlyEvent("txtSbcNum", "TextBox", Me) on Form_Load, it works fine. But when I place my textboxes on a GroupBox, it doesn't work but when I remove it again to GroupBox it works again. Did i missed something?
The problem is that the form controls don't include child controls. So, the group box is found on the form but not the text box, since it is a child of the group box.
Change this line:
CommonFunc.NumbersOnlyEvent("txtSbcNum", "TextBox", Me)
to this:
CommonFunc.NumbersOnlyEvent("txtSbcNum", "TextBox", Me.GroupBox1)
so that it will find the text box in the group box.
精彩评论