放一个Text1控件数组 Static aa As Long Private Sub CommandLeft_Click() Text1(aa Mod Text1.Count).SetFocus aa = aa - 1 End SubPrivate Sub CommandRight_Click() Text1(aa Mod Text1.Count).SetFocus aa = aa + 1 End Sub
先在需要控制的TEXT的tag属性 写上 方位,格式是: x,y x为横的方位,y为竖的方位 譬如 第一个TextBox 的tag为1,1 他右边的为 2,1 他下面的就为 1,2 Option ExplicitPrivate m_col As New Collection Private Const MAX_X As Integer = 3 '横最多3个 这里我测试 横竖都是3个 Private Const MAX_Y As Integer = 3 '竖最多3个 Private m_strCurFocus As String Private Sub cmdDown_Click() '向下 Call GoPos("DOWN") End Sub Private Sub cmdUP_Click() '向上 Call GoPos("UP") End Sub Private Sub cmdLeft_Click() '向左 Call GoPos("Left") End Sub Private Sub cmdRight_Click() '向右 Call GoPos("Right") End SubFunction GoPos(strDirect As String) Dim strTag As String, strPos() As String, x As Integer, y As Integer If m_strCurFocus = "" Then Exit Function strPos = Split(m_strCurFocus, ",") x = CInt(Trim(strPos(0))) y = CInt(Trim(strPos(1))) Select Case UCase(strDirect) Case "LEFT" If (x - 1) < 1 Then '如果是最左边一个的话 '回到最上一行最后一个 '注意下面RIGHT DOWN UP也要这样处理,由于是例子 没做处理了 x = MAX_X If y - 1 > 0 Then y = y - 1 End If Else x = x - 1 End If Case "RIGHT" x = x + 1 Case "DOWN" y = y + 1 Case "UP" y = y - 1 End Select Dim obj As Control On Error Resume Next Set obj = m_col.Item(x & "," & y) '这里要处理一下 是不是没有这个项目 On Error GoTo 0 If Not (obj Is Nothing) Then obj.SetFocus End If End Function Private Sub Form_Load() Dim obj As Control For Each obj In Controls If TypeName(obj) = "TextBox" Then If obj.Tag <> "" Then Debug.Print obj.Tag m_col.Add obj, obj.Tag '把所有的TextBox添加到集合里 End If End If Next End Sub '使用Timer来纪录 当前的焦点, 因为按了控制方向的按钮后 焦点会是那个按钮 而不是刚才的Text Private Sub Timer1_Timer() If Me.ActiveControl.Tag <> "" Then m_strCurFocus = Me.ActiveControl.Tag End If End Sub
用控件数组 dim i As integer //在通用声明里 Private Sub Text1_GotFocus(Index As Integer) i = Index End SubPrivate Sub CommandLeft_Click() if (i-1) < 0 then exit sub Text1(i -1).SetFocus End SubPrivate Sub CommandRight_Click() if (i+1) > text1.count then exit sub Text1(i +1).SetFocus End Sub
Text2.SetFocus
End Sub
Static aa As Long
Text1(aa Mod Text1.Count).SetFocus
aa = aa + 1
End Sub
Static aa As Long
Private Sub CommandLeft_Click()
Text1(aa Mod Text1.Count).SetFocus
aa = aa - 1
End SubPrivate Sub CommandRight_Click()
Text1(aa Mod Text1.Count).SetFocus
aa = aa + 1
End Sub
譬如 第一个TextBox 的tag为1,1 他右边的为 2,1 他下面的就为 1,2
Option ExplicitPrivate m_col As New Collection
Private Const MAX_X As Integer = 3 '横最多3个 这里我测试 横竖都是3个
Private Const MAX_Y As Integer = 3 '竖最多3个
Private m_strCurFocus As String
Private Sub cmdDown_Click() '向下
Call GoPos("DOWN")
End Sub
Private Sub cmdUP_Click() '向上
Call GoPos("UP")
End Sub
Private Sub cmdLeft_Click() '向左
Call GoPos("Left")
End Sub
Private Sub cmdRight_Click() '向右
Call GoPos("Right")
End SubFunction GoPos(strDirect As String)
Dim strTag As String, strPos() As String, x As Integer, y As Integer
If m_strCurFocus = "" Then Exit Function
strPos = Split(m_strCurFocus, ",")
x = CInt(Trim(strPos(0)))
y = CInt(Trim(strPos(1)))
Select Case UCase(strDirect)
Case "LEFT"
If (x - 1) < 1 Then '如果是最左边一个的话
'回到最上一行最后一个
'注意下面RIGHT DOWN UP也要这样处理,由于是例子 没做处理了
x = MAX_X
If y - 1 > 0 Then
y = y - 1
End If
Else
x = x - 1
End If
Case "RIGHT"
x = x + 1
Case "DOWN"
y = y + 1
Case "UP"
y = y - 1
End Select
Dim obj As Control
On Error Resume Next
Set obj = m_col.Item(x & "," & y) '这里要处理一下 是不是没有这个项目
On Error GoTo 0
If Not (obj Is Nothing) Then
obj.SetFocus
End If
End Function
Private Sub Form_Load()
Dim obj As Control
For Each obj In Controls
If TypeName(obj) = "TextBox" Then
If obj.Tag <> "" Then
Debug.Print obj.Tag
m_col.Add obj, obj.Tag '把所有的TextBox添加到集合里
End If
End If
Next
End Sub
'使用Timer来纪录 当前的焦点, 因为按了控制方向的按钮后 焦点会是那个按钮 而不是刚才的Text
Private Sub Timer1_Timer()
If Me.ActiveControl.Tag <> "" Then
m_strCurFocus = Me.ActiveControl.Tag
End If
End Sub
不过当你的TEXT控件非常多的时候,排版会比较麻烦,可以考虑实用GRID控件,比如MSFLEXGRID,顺便给出一个简单的编辑MSFLEXGRID控件内容的方法:http://community.csdn.net/Expert/topic/3190/3190090.xml?temp=7.798404E-02
dim i As integer //在通用声明里
Private Sub Text1_GotFocus(Index As Integer)
i = Index
End SubPrivate Sub CommandLeft_Click()
if (i-1) < 0 then exit sub
Text1(i -1).SetFocus
End SubPrivate Sub CommandRight_Click()
if (i+1) > text1.count then exit sub
Text1(i +1).SetFocus
End Sub