[英]Iterate through a series of textboxes
我在使用 Active X 文本框控件的 MS-Word 文檔中有大約 13 個文本框。 我發現這段代碼在我的測試中使用單個文本框似乎效果很好。
如何按名稱循環瀏覽所有 13 個文本框,而不是將此代碼添加到所有 13 個文本框。
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii > Asc("9") Or KeyAscii < Asc("0") Then
If KeyAscii = Asc("-") Then
If InStr(1, Me.TextBox1.Text, "-") > 0 Or _
Me.TextBox1.SelStart > 0 Then KeyAscii = 0
ElseIf KeyAscii = Asc(".") Then
If InStr(1, Me.TextBox1.Text, ".") > 0 Then KeyAscii = 0
Else
KeyAscii = 0
End If
End If
End Sub
你可以這樣做:
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
HandleKeyPress Me.TextBox1, KeyAscii
End Sub
Private Sub TextBox2_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
HandleKeyPress Me.TextBox2, KeyAscii
End Sub
Private Sub HandleKeyPress(txtBox As Object, ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii > Asc("9") Or KeyAscii < Asc("0") Then
If KeyAscii = Asc("-") Then
If InStr(1, txtBox.Text, "-") > 0 Or _
txtBox.SelStart > 0 Then KeyAscii = 0
ElseIf KeyAscii = Asc(".") Then
If InStr(1, txtBox.Text, ".") > 0 Then KeyAscii = 0
Else
KeyAscii = 0
End If
End If
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.