VB6捕获Tab键
我正在vb6中制作一个exe,其中包含供用户填写的表单。我正在尝试这样做,如果您的选项卡超过表单的可见区域,那么如果您的ActiveControl不在表单的可见部分,它将自动向下滚动。我唯一的问题是,我找不到一种方法来捕获Tab键。在谷歌搜索了很多次之后,我了解到,捕获tab键的一种方法是为表单上的每个控件将表单的KeyPreview属性设置为True,并将TabStop属性设置为falseVB6捕获Tab键,vb6,Vb6,我正在vb6中制作一个exe,其中包含供用户填写的表单。我正在尝试这样做,如果您的选项卡超过表单的可见区域,那么如果您的ActiveControl不在表单的可见部分,它将自动向下滚动。我唯一的问题是,我找不到一种方法来捕获Tab键。在谷歌搜索了很多次之后,我了解到,捕获tab键的一种方法是为表单上的每个控件将表单的KeyPreview属性设置为True,并将TabStop属性设置为false 我的问题是,是否有任何方法可以在vb6中捕获Tab键,而不必禁用所有TabStop。尝试查看其他属性如何
我的问题是,是否有任何方法可以在vb6中捕获Tab键,而不必禁用所有TabStop。尝试查看其他属性如何 控件“enter”事件触发时的读数是否有助于您决定向下滚动?
当用户点击每个项目时,应该会出现这种情况,并允许您确定在何处滚动视图。我想我有一个答案给您,但尚未测试 使用计时器控件,获取活动控件。然后查看表单的Top&Height属性,与活动控件的Top&Height属性进行比较,可以确定活动控件是否在表单的可视区域内,如果需要,可以滚动表单
希望能有所帮助。这可能不是您问题的完整答案,但在VBF论坛上有几个解决此问题的示例: 如果有可以获得焦点的控件,则无法以VB6格式(使用本机方法)捕获Tab press
但是,您可以使用其他方法,如或。这些还可以处理其他获取焦点键的方法,或者只需单击鼠标。通过编程方式扫描表单上的控件 注意那些TabStop=True的 将它们放入按TabIndex排序的列表中 清除所有控件的TabStop=True属性 启用窗体的KeyPreview选项 捕获表单的_KeyDown事件 如果该键是tab键,请在控制列表中单步查找下一个或上一个并将焦点设置为它 如果焦点位于网格上,则可以选择向前或向后移动选定的行/列 我在石器时代写过这门课。可耻的丑陋代码,但它是有效的 具有多个控件和网格的窗体:
Private WithEvents TabStop As cTabStop
Private Sub Form_Load()
Me.MSFlexGrid1.Rows = 3
Me.MSFlexGrid1.Cols = 3
Me.MSFlexGrid1.FixedRows = 0
Me.MSFlexGrid1.FixedCols = 0
Set TabStop = New cTabStop
TabStop.Setup Me
End Sub
Private Sub TabStop_TabPressed(ByVal Shift As Integer, Cancel As Integer)
If Me.ActiveControl.Name = Me.MSFlexGrid1.Name Then
If Shift = 0 Then
If Me.MSFlexGrid1.Col < Me.MSFlexGrid1.Cols - 1 Then
Me.MSFlexGrid1.Col = Me.MSFlexGrid1.Col + 1
Cancel = 1
ElseIf Me.MSFlexGrid1.Row < Me.MSFlexGrid1.Rows - 1 Then
Me.MSFlexGrid1.Col = 0
Me.MSFlexGrid1.Row = Me.MSFlexGrid1.Row + 1
Cancel = 1
End If
Else
If Me.MSFlexGrid1.Col > 0 Then
Me.MSFlexGrid1.Col = Me.MSFlexGrid1.Col - 1
Cancel = 1
ElseIf Me.MSFlexGrid1.Row > 0 Then
Me.MSFlexGrid1.Col = Me.MSFlexGrid1.Cols - 1
Me.MSFlexGrid1.Row = Me.MSFlexGrid1.Row - 1
Cancel = 1
End If
End If
End If
End Sub
Private with events选项卡stop As cTabStop
专用子表单_加载()
Me.MSFlexGrid1.Rows=3
Me.MSFlexGrid1.Cols=3
Me.MSFlexGrid1.FixedRows=0
Me.MSFlexGrid1.FixedCols=0
设置TabStop=New cTabStop
停,陷害我
端接头
私有子选项卡stop\u TabPressed(ByVal Shift为整数,Cancel为整数)
如果Me.ActiveControl.Name=Me.MSFlexGrid1.Name,则
如果Shift=0,则
如果Me.MSFlexGrid1.Col0,则
Me.MSFlexGrid1.Col=Me.MSFlexGrid1.Col-1
取消=1
ElseIf Me.MSFlexGrid1.Row>0则
Me.MSFlexGrid1.Col=Me.MSFlexGrid1.Cols-1
Me.MSFlexGrid1.Row=Me.MSFlexGrid1.Row-1
取消=1
如果结束
如果结束
如果结束
端接头
类别:
Option Explicit
' ------------------------------------------------------------------------------
' This class ATTEMPTS to manage Tab Keypress events
' The specific advantage is the ability capture and cancel a Tab Keypress
'
' This is a horribly inelegant way to get the job done
'
' Bug:
' If a control should be next to receive focus, but is sitting on a control
' which is hidden, the tab order will not be able to proceed past that control.
' i.e. Command1 on Frame1 where Frame1.Visible = False
' ------------------------------------------------------------------------------
Private Type typeControl
Control As Control
TabIndex As Long
End Type
Private Type typeSettings
Controls() As typeControl
ControlCount As Long
End Type
Public Event TabPressed(ByVal Shift As Integer, ByRef Cancel As Integer)
Private Settings As typeSettings
Private WithEvents Form As Form
Public Sub Setup(Form_ As Form)
Call FunctionTagger("cTabStop", "Setup")
On Error GoTo E
Dim Control As Control
' Get the Form and enable its KeyPreview ability
Set Form = Form_
Form.KeyPreview = True
' Get the Tab-Able controls from the form
Settings.ControlCount = 0
ReDim Settings.Controls(0 To 0)
For Each Control In Form.Controls
Call AddControl(Control)
Next Control
Exit Sub
E:
Debug.Print " " & Err.Description
End Sub
Public Sub Clear()
Call FunctionTagger("cTabStop", "Clear")
Set Form = Nothing
Settings.ControlCount = 0
ReDim Settings.Controls(0 To 0)
End Sub
Private Sub AddControl(ByRef Control As Control)
Call FunctionTagger("cTabStop", "AddControl")
On Error GoTo E
Dim TabIndex As Long
Dim TabStop As Boolean
Dim Visible As Boolean
Dim Enabled As Boolean
' Only accept controls with these four properties
TabStop = Control.TabStop
TabIndex = Control.TabIndex
Visible = Control.Visible
Enabled = Control.Enabled
' Diable the control's TabStop property
Control.TabStop = False
' Add the control to our list
Settings.ControlCount = Settings.ControlCount + 1
ReDim Preserve Settings.Controls(0 To Settings.ControlCount - 1)
Set Settings.Controls(Settings.ControlCount - 1).Control = Control
Settings.Controls(Settings.ControlCount - 1).TabIndex = TabIndex
Exit Sub
E:
End Sub
Private Sub Class_Initialize()
Call FunctionTagger("cTabStop", "Class_Initialize")
Clear
End Sub
Private Sub Class_Terminate()
Call FunctionTagger("cTabStop", "Class_Terminate")
Clear
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Call FunctionTagger("cTabStop", "Form_KeyDown")
Dim Cancel As Integer
If KeyCode = 9 Then
KeyCode = 0
RaiseEvent TabPressed(Shift, Cancel)
Debug.Print " Tab Pressed"
If Cancel <> 0 Then ' Tab Keypress Cancelled...
KeyCode = 0
ElseIf Shift = 0 Then ' Tab Pressed...
TabRight
ElseIf Shift = 1 Then ' Shift-Tab Pressed...
TabLeft
End If
End If
End Sub
Public Sub TabLeft()
Call FunctionTagger("cTabStop", "TabLeft")
On Error GoTo E
Dim CurTabIndex As Long
Dim NextControl As Long
Dim NextTabIndex As Long
Dim c As Long
' Get the Tab Index of the currently active control
If Not Form.ActiveControl Is Nothing Then ' 2012-09-25 - Jaron
CurTabIndex = Form.ActiveControl.TabIndex
End If
' Find the control with the next smallest Tab Index
NextControl = -1
NextTabIndex = -1
For c = 0 To Settings.ControlCount - 1
' With Settings.Controls(c) ' 2007-05-07
If Settings.Controls(c).TabIndex >= NextTabIndex And Settings.Controls(c).TabIndex < CurTabIndex Then
If Settings.Controls(c).Control.Visible And Settings.Controls(c).Control.Enabled Then
NextControl = c
NextTabIndex = Settings.Controls(c).TabIndex
End If
End If
' End With
Next c
' Set focus to the next control
If NextControl >= 0 Then
' With Settings.Controls(NextControl).Control ' 2007-05-07
'Debug.Print " Set Focus to " & Settings.Controls(NextControl).Control.Name
SetFocusSafe Settings.Controls(NextControl).Control ' 2007-06-05
DoEvents
' End With
End If
Exit Sub
E:
Debug.Print " " & Err.Description
End Sub
Public Sub TabRight()
Call FunctionTagger("cTabStop", "TabRight")
On Error GoTo E
Dim CurTabIndex As Long
Dim NextControl As Long
Dim NextTabIndex As Long
Dim c As Long
' Get the Tab Index of the currently active control
If Not Form.ActiveControl Is Nothing Then ' 2012-09-25 - Jaron
CurTabIndex = Form.ActiveControl.TabIndex
End If
' Find the control with the next largest Tab Index
NextControl = -1
NextTabIndex = 999999999
For c = 0 To Settings.ControlCount - 1
' With Settings.Controls(c) ' 2007-05-07
If Settings.Controls(c).TabIndex <= NextTabIndex And Settings.Controls(c).TabIndex > CurTabIndex Then
If Settings.Controls(c).Control.Visible And Settings.Controls(c).Control.Enabled Then
NextControl = c
NextTabIndex = Settings.Controls(c).TabIndex
End If
End If
' End With
Next c
' Set focus to the next control
If NextControl >= 0 Then
' With Settings.Controls(NextControl).Control ' 2007-05-07
'Debug.Print " Set Focus to " & Settings.Controls(NextControl).Control.Name
SetFocusSafe Settings.Controls(NextControl).Control ' 2007-06-05
DoEvents
' End With
End If
Exit Sub
E:
Debug.Print " " & Err.Description
End Sub
' ------------------------------------------------------------------------------
Private Sub SetFocusSafe(ByRef Control As Control)
On Error GoTo E
Control.SetFocus
Exit Sub
E:
End Sub
Private Sub FunctionTagger(ByVal sModule As String, ByVal sFunction As String)
Debug.Print " " & Time$ & " " & sModule & "." & sFunction
End Sub
' ------------------------------------------------------------------------------
' ------------------------------------------------------------------------------
选项显式
' ------------------------------------------------------------------------------
'此类尝试管理Tab按键事件
'特别的优势是能够捕获和取消Tab键
'
这是一种极不雅观的完成工作的方式
'
“错误:
'如果某个控件应该位于接收焦点的旁边,但却位于某个控件上
'隐藏,选项卡顺序将无法通过该控件继续。
'即Frame1上的Command1,其中Frame1.Visible=False
' ------------------------------------------------------------------------------
专用类型字体控制
控制作为控制
TabIndex尽可能长
端型
专用类型类型设置
控件()作为类型控件
控制计数为长
端型
已按下公共事件选项卡(ByVal Shift为整数,ByRef Cancel为整数)
私人设置作为类型设置
以事件形式作为形式的私有
公共子设置(形式为形式)
调用FunctionTagger(“cTabStop”、“设置”)
关于错误转到E
作为控制的调光控制
'获取表单并启用其KeyPreview功能
设置形式=形式_
Form.KeyPreview=True
'从窗体中获取可用于制表符的控件
Settings.ControlCount=0
重拨设置。控件(0到0)
对于Form.Controls中的每个控件
调用AddControl(控制)
下一个控件
出口接头
E:
Debug.Print“”&错误说明
端接头
公共分区清除()
调用FunctionTagger(“cTabStop”、“Clear”)
设置形式=无
Settings.ControlCount=0
重拨设置。控件(0到0)
端接头
私有子AddControl(ByRef控件作为控件)
调用FunctionTagger(“cTabStop”、“AddControl”)
关于错误转到E
如长
Dim TabStop作为布尔值
以布尔值形式显示
将Dim启用为布尔值
'仅接受具有这四个属性的控件
TabStop=Control.TabStop
TabIndex=Control.TabIndex
可见=控件。可见
已启用=控制。已启用
'禁用控件的TabStop属性
Control.TabStop=False
'将控件添加到我们的列表中
Settings.ControlCount=Settings.ControlCount+1
重拨保留设置。控件(0到设置。控件计数-1)
设置Settings.Controls(Settings.ControlCount-1)。Control=Control
Settings.Controls(Settings.ControlCount-1).TabIndex=TabIndex
出口接头
E:
端接头
私有子类_Initialize()
调用FunctionTagger(“cTabStop”,“Class_Initialize”)
清楚的
端接头
私有子类