我怎样才能使用相同的代码而不必每次都在不同的潜艇中重新输入?
How can I use the same code without having to retype it every time in different subs?
所以我有一个文本框,我只想允许数字进入,我有代码可以做到这一点并且工作正常。但是我想在几个文本框中使用它,我怎样才能使用相同的代码而不必在每个文本框的 KeyDown 和 KeyPress Subs 中重新键入它?
我在 KeyDown Subs 中使用的代码是
If e.KeyCode = Keys.Back Then
BACKSPACE = True
Else
BACKSPACE = False
End If
并且在我使用的 KeyPress 潜艇中
If BACKSPACE = False Then
Dim allowedChars As String = "0123456789"
If allowedChars.IndexOf(e.KeyChar) = -1 Then
e.Handled = True
End If
End If
我将这段代码用于几个文本框,想知道如何稍微清理一下。我可以吗?感谢您的帮助和时间!
您可以创建自己的 TextBox 控件版本,它继承自基本 TextBox 并使用您的自定义代码对其进行扩展。
例如:
Public Class CleverTextBox
Inherits TextBox
Private BACKSPACE As Boolean = False
Private Sub CleverTextBox_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Back Then
BACKSPACE = True
Else
BACKSPACE = False
End If
End Sub
Private Sub CleverTextBox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
If BACKSPACE = False Then
Dim allowedChars As String = "0123456789"
If allowedChars.IndexOf(e.KeyChar) = -1 Then
e.Handled = True
End If
End If
End Sub
End Class
然后重新编译您的项目,您应该会看到新的 CleverTextBox
控件已添加到“工具箱”面板中,您可以将其拖放到窗体上。
您可以简单地设置事件的处理程序来处理其他对象,它们将共享该代码
Private Sub txtDim0_Validated(sender As Object, e As EventArgs) Handles txtDim0.Validated, txtDim1.Validated, txtDim2.Validated, txtDim3.Validated, txtDim4.Validated, txtDim5.Validated, txtDim7.Validated, txtDim7.Validated
Dim iNewIndex As Integer
If ErrorProvider1.Tag <> "" Then
For icount = 0 To Len(ErrorProvider1.Tag) - 1
iNewIndex = Val(ErrorProvider1.Tag.ToString.Substring(icount, 1))
ErrorProvider1.SetError(txtDims(iNewIndex), String.Empty)
Next
ErrorProvider1.Tag = ""
Else
' get the index based on the name
Dim index As Integer = Val(sender.name.ToString.Substring(Len("txtDim")))
ErrorProvider1.SetError(txtDims(index), String.Empty)
End If
End Sub
所以我有一个文本框,我只想允许数字进入,我有代码可以做到这一点并且工作正常。但是我想在几个文本框中使用它,我怎样才能使用相同的代码而不必在每个文本框的 KeyDown 和 KeyPress Subs 中重新键入它?
我在 KeyDown Subs 中使用的代码是
If e.KeyCode = Keys.Back Then
BACKSPACE = True
Else
BACKSPACE = False
End If
并且在我使用的 KeyPress 潜艇中
If BACKSPACE = False Then
Dim allowedChars As String = "0123456789"
If allowedChars.IndexOf(e.KeyChar) = -1 Then
e.Handled = True
End If
End If
我将这段代码用于几个文本框,想知道如何稍微清理一下。我可以吗?感谢您的帮助和时间!
您可以创建自己的 TextBox 控件版本,它继承自基本 TextBox 并使用您的自定义代码对其进行扩展。
例如:
Public Class CleverTextBox
Inherits TextBox
Private BACKSPACE As Boolean = False
Private Sub CleverTextBox_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Back Then
BACKSPACE = True
Else
BACKSPACE = False
End If
End Sub
Private Sub CleverTextBox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
If BACKSPACE = False Then
Dim allowedChars As String = "0123456789"
If allowedChars.IndexOf(e.KeyChar) = -1 Then
e.Handled = True
End If
End If
End Sub
End Class
然后重新编译您的项目,您应该会看到新的 CleverTextBox
控件已添加到“工具箱”面板中,您可以将其拖放到窗体上。
您可以简单地设置事件的处理程序来处理其他对象,它们将共享该代码
Private Sub txtDim0_Validated(sender As Object, e As EventArgs) Handles txtDim0.Validated, txtDim1.Validated, txtDim2.Validated, txtDim3.Validated, txtDim4.Validated, txtDim5.Validated, txtDim7.Validated, txtDim7.Validated
Dim iNewIndex As Integer
If ErrorProvider1.Tag <> "" Then
For icount = 0 To Len(ErrorProvider1.Tag) - 1
iNewIndex = Val(ErrorProvider1.Tag.ToString.Substring(icount, 1))
ErrorProvider1.SetError(txtDims(iNewIndex), String.Empty)
Next
ErrorProvider1.Tag = ""
Else
' get the index based on the name
Dim index As Integer = Val(sender.name.ToString.Substring(Len("txtDim")))
ErrorProvider1.SetError(txtDims(index), String.Empty)
End If
End Sub