1,451
社区成员
发帖
与我相关
我的任务
分享
Dim txtBox As TextBox
If TypeOf Screen.ActiveControl Is TextBox Then
Set txtBox = Screen.ActiveControl
MsgBox "当前活动的textbox是" & txtBox.Name
End If
If TypeOf Me.ActiveControl Is TextBox Then
Dim activeText As TextBox = Me.ActiveControl
Select Case activeText.Name
Case "textbox1"
Case "textbox2"
Case Else
End Select
End If
Private Sub Command2_Click(Index As Integer)‘小键盘’
If Not (WhoGetFocuse Is Nothing) Then
If Index = 10 Then
WhoGetFocuse.Text = WhoGetFocuse.Text & "."
Else
WhoGetFocuse.Text = WhoGetFocuse.Text & (Index + 1)
End If
WhoGetFocuse.SetFocus
End If
End Sub
Private Sub Command3_Click()‘删除键’
If Not (WhoGetFocuse Is Nothing) Then
If Len(WhoGetFocuse.Text) <> 0 Then
WhoGetFocuse.Text = Left(WhoGetFocuse.Text, Len(WhoGetFocuse.Text) - 1)
End If
WhoGetFocuse.SetFocus
End If
End Sub
Private Sub Text1_GotFocus(Index As Integer)文本框
Set WhoGetFocuse = Text1(Index)
Text1(Index).SelStart = Len(Text1(Index).Text)‘光标始终保持在最右边
end sub
Option Explicit
Dim WhoGetFocuse As TextBox '定义模块级变量,存储哪个文本框获取了焦点
Private Sub Command1_Click()
'按钮点击的时候只要模块变量不为空,那么取到谁就往谁那里送文本
If Not (WhoGetFocuse Is Nothing) Then
WhoGetFocuse.Text = WhoGetFocuse.Text & "1"
WhoGetFocuse.SetFocus '完事以后再把焦点交还给文本框
End If
End Sub
Private Sub Command2_Click()
'按钮点击的时候只要模块变量不为空,那么取到谁就往谁那里送文本
If Not (WhoGetFocuse Is Nothing) Then
WhoGetFocuse.Text = WhoGetFocuse.Text & "2"
WhoGetFocuse.SetFocus '完事以后再把焦点交还给文本框
End If
End Sub
Private Sub Text1_GotFocus()
Set WhoGetFocuse = Text1 '两个文本框谁获得了焦点就把自己塞给前面定义的模块级变量
End Sub
Private Sub Text2_GotFocus()
Set WhoGetFocuse = Text2
End Sub
用控件数组版:
Option Explicit
Dim WhoGetFocuse As TextBox '定义模块级变量,存储哪个文本框获取了焦点
Private Sub Command1_Click(Index As Integer)
'按钮点击的时候只要模块变量不为空,那么取到谁就往谁那里送文本
If Not (WhoGetFocuse Is Nothing) Then
WhoGetFocuse.Text = WhoGetFocuse.Text & (Index + 1)
WhoGetFocuse.SetFocus '完事以后再把焦点交还给文本框
End If
End Sub
Private Sub Text1_GotFocus(Index As Integer)
Set WhoGetFocuse = Text1(Index) '两个文本框谁获得了焦点就把自己塞给前面定义的模块级变量
End Sub
Dim n As Integer
Private Sub Command1_Click(Index As Integer)
If Index = 10 Then
Text1(n) = Replace(Text1(n), ".", "") & "."
Else
Text1(n) = Text1(n) & Index
End If
Text1(n).SelStart = Len(Text1(n))
Text1(n).SetFocus
End Sub
Private Sub Command2_Click()
Text1(n) = Left(Text1(n), Len(Text1(n)) - 1)
Text1(n).SelStart = Len(Text1(n))
Text1(n).SetFocus
End Sub
Private Sub Text1_GotFocus(Index As Integer)
n = Index
End Sub
Text1和 Command1 都是控件数组,其中 Command1(10) 是小数点;Command2 是退格键。Private Sub Text1_KeyPress(Index As Integer, KeyAscii As Integer)
If Index = 0 Then
'......
Else
'......
End If
End Sub
如果不是数组,你就在各个 Text 的 KeyPress 事件中处理。