我一直在使用以下Subs来检查并确保输入到TextBox中的数据是数字,然后在具有单个CurrTextBox的表单中转换为货币小数,但是我现在的任务是创建一个包含多个CurrTextBox的表单#39; S
如何转换以下代码以处理多个调用。我想我可以创建一个Public Sub,输入特定CurrTextBox的数据调用... Ideas?
Private Sub CurrBox2_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
If (e.KeyCode >= Keys.D0 And e.KeyCode <= Keys.D9) OrElse (e.KeyCode >= Keys.NumPad0 And e.KeyCode <= Keys.NumPad9) OrElse e.KeyCode = Keys.Back Then
acceptableKey = True
Else
acceptableKey = False
End If
End Sub
' This sub handles the keypress inputs and ensures values are numeric only
Private Sub CurrBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
' Check for the flag being set in the KeyDown event.
If acceptableKey = False Then
' Stop the character from being entered into the control since it is non-numerical.
e.Handled = True
Return
Else
If e.KeyChar = Convert.ToChar(Keys.Back) Then
If strCurrency.Length > 0 Then
strCurrency = strCurrency.Substring(0, strCurrency.Length - 1)
End If
Else
strCurrency = strCurrency & e.KeyChar
End If
If strCurrency.Length = 0 Then
CurrBox2.Text = ""
ElseIf strCurrency.Length = 1 Then
CurrBox2.Text = "0.0" & strCurrency
ElseIf strCurrency.Length = 2 Then
CurrBox2.Text = "0." & strCurrency
ElseIf strCurrency.Length > 2 Then
CurrBox2.Text = strCurrency.Substring(0, strCurrency.Length - 2) & "." & strCurrency.Substring(strCurrency.Length - 2)
End If
CurrBox2.Select(CurrBox2.Text.Length, 0)
End If
e.Handled = True
End Sub
答案 0 :(得分:0)
一个事件可以处理多个控件。
只需在事件中的Handles子句之后添加更多控件,并处理所有这些控件。
Private Sub CurrBox2_KeyDown(ByVal sender As Object, ByVal e System.Windows.Forms.KeyEventArgs) Handles currbox1.keydown, currbox2.keydown, currbox3.keydown
If (e.KeyCode >= Keys.D0 And e.KeyCode <= Keys.D9) OrElse (e.KeyCode >= Keys.NumPad0 And e.KeyCode <= Keys.NumPad9) OrElse e.KeyCode = Keys.Back Then
acceptableKey = True
Else
acceptableKey = False
End If
End Sub
这样您只需要编写一次这些事件。 我希望我的问题是正确的。