在一个用户窗体上,我有4列x 10行的文本框。当第二个文本框中的第一个字符位于“%”行中时,我需要将焦点设置在下一行的第二个文本框中,否则,我需要将注意力集中在下一个文本框(同一行的第三个文本)上。>
这是我的代码:
Const ST_VPISOV = 4
Public Set_fokus As Boolean
Private Sub tb1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Vnos Me.tb1
End Sub
Private Sub tb2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Vnos Me.tb2
End Sub
...
Private Sub tb40_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Vnos Me.tb40
End Sub
Sub Vnos(tb As Object)
Dim Vpis As String
If Set_fokus Then Exit Sub
stKrog = Me.MultiPage1.value
Vpis = tb.Text
If tb.Text = "" Then
Exit Sub
End If
tb_column = CInt(Mid(tb.Name, 3)) Mod ST_VPISOV
If tb_column = 0 Then tb_column = ST_VPISOV
If tb_column = 2 Then
If Left(Vpis, 1) = "%" Then ' Set focus on 2nd textbox in next row
Set_fokus = True
Me.MultiPage1.Pages(stKrog).Controls("tb" & CInt(Mid(tb.Name, 3)) + 4).SetFocus
Else ' set focus on 3rd (next) textbox in same row
Set_fokus = True
Me.MultiPage1.Pages(stKrog).Controls("tb" & CInt(Mid(tb.Name, 3)) + 1).SetFocus
End If
ElseIf tb_column = 4 Then ' set focus on 4th textbox in next row
Set_fokus = True
Me.MultiPage1.Pages(stKrog).Controls("tb" & CInt(Mid(tb.Name, 3)) + 5).SetFocus
End If
Debug.Print Me.MultiPage1.Pages(stKrog).ActiveControl.Name
Set_fokus = False
End Sub
当我运行此代码并将值“%12345”放入tb2并按Enter键时,焦点实际上放在tb6上,但随后跳到tb7并在tb5中完成。
我做错了什么?
谢谢您的帮助。
我尝试过@AcsEmo建议:
Sub Vnos(tb As Object)
Dim Vpis As String
stKrog = Me.MultiPage1.value
Vpis = tb.Text
If tb.Text = "" Or Izhod Then
Exit Sub
End If
tb_column = CInt(Mid(tb.Name, 3)) Mod ST_VPISOV
If tb_column = 0 Then tb_column = ST_VPISOV
Debug.Print "1st debug in Vnos: " & Me.MultiPage1.Pages(stKrog).ActiveControl.Name
itb = CInt(Mid(tb.Name, 3))
If itb >= 40 Then itb = 0
With Me.MultiPage1.Pages(stKrog)
If tb_column = 2 And Left(Vpis, 1) = "%" Or tb_column = 4 Then ' Set focus on 2nd textbox in next row
.Controls("tb" & itb + 4).SetFocus
Else ' set focus on next textbox
.Controls("tb" & itb + 1).SetFocus
End If
End With
Debug.Print "2nd debug in Vnos: " & Me.MultiPage1.Pages(stKrog).ActiveControl.Name
End Sub
现在将焦点从tb2到tb6结束于tb4。 我还为所有文本框添加textbox_enter事件:
Private Sub tb1_Enter()
Debug.Print "Enter: " & Me.MultiPage1.Pages(stKrog).ActiveControl.Name
End Sub
这是我得到的照片: Debugging 01
答案 0 :(得分:0)
如果tb2以%开头,则您的代码可以正常工作。如果tb6不是以%开头,则可以。该代码不处理tb7(您仅检查第2列和第4列),因此从tb7跳转到tb5可能是由于制表符顺序所致。因此,我会像这样重构您的代码(假设在第4列中按Enter键应始终将焦点移至第4列中的下一行,否则焦点应跳至下一个文本框):
iTB = CInt(Mid(tb.Name, 3))
if iTB >= 40 Then iTB = 0
With Me.MultiPage1.Pages(stKrog)
If tb_column = 2 And Left(Vpis, 1) = "%" or tb_column = 4 Then ' Set focus on 2nd textbox in next row
.Controls("tb" & iTB + 4).SetFocus
Else ' set focus on next textbox
.Controls("tb" & iTB + 1).SetFocus
End If
End With
答案 1 :(得分:0)
我更改了以下代码:
Private Sub tb1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Vnos Me.tb1
End Sub
收件人:
Private Sub tb2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Vnos Me.tb2
Cancel = CancelFokus
CancelFokus = False
End Sub
和
Private Sub tb2_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 13 Then
NastaviFokus Me.tb2
End If
End Sub
Sub NastaviFokus(tb As Object)
Dim stVnosGosta As Integer, StGost As Integer
Dim Vpis As String, ImeGosta As String
Dim PrestaviFokus As Integer
CancelFokus = False
If tb.Text = "" Or Izhod Then
Exit Sub
End If
Vpis = tb.Text
PrestaviFokus = 0
StGost = Int(Split(Gost_Krog2(tb.Name, ST_VPISOV), ";")(0))
stVnosGosta = Int(Split(Gost_Krog2(tb.Name, ST_VPISOV), ";")(1))
With Worksheets(VP_LIST)
If stVnosGosta = 2 Then
If Left(Vpis, 1) = "%" Then
Vpis = DobiVpis(Vpis)
tb.Text = Vpis
If Len(Vpis) > 2 Then
If Len(ImeGosta) > 2 Then
Controls("tb" & CInt(Mid(tb.Name, 3)) + 1).Text = ImeGosta
.Cells(StGost + 1, 3).Value2 = ImeGosta
.Cells(StGost + 1, stVnosGosta).Value2 = Vpis
.Cells(StGost + 1, 5).Value2 = Me.MultiPage1.value + 1
PrestaviFokus = 4
End If
Else
CancelFokus = True
End If
End If
ElseIf stVnosGosta = 4 And IsNumeric(Vpis) Then
PrestaviFokus = 4
End If
End With
' Me.MultiPage1.Pages(stKrog).Controls("tb" & CInt(Mid(tb.Name, 3)) + 8).SetFocus
If Not PrestaviFokus = 0 And _
(PrestaviFokus + (CInt(Mid(tb.Name, 3)) - 1)) \ 40 = Me.MultiPage1.value Then
Me.Controls("tb" & CInt(Mid(tb.Name, 3)) + PrestaviFokus).SetFocus
End If
End Sub
现在,.SetFocus可以像我想要的那样工作。
我猜VBA excel无法将焦点放在textbox_Exit(ByVal取消为MSForms.ReturnBoolean)事件上,而您必须使用textbox_KeyDown(ByVal关键码为MSForms.ReturnInteger,ByVal Shift为整数)事件。
谢谢大家的帮助。
最好的问候, Matjaž