2013-06-21 75 views
1

我在vb6中製作了一個exe文件,其中有用於填寫的表單。我試圖做到這一點,以便如果您在窗體的可見區域中查看Tab鍵,如果您的ActiveControl不在窗體的可見部分,它會自動向下滾動。我唯一的問題是,我找不到捕獲Tab鍵的方法。據我瞭解,經過大量搜索後,捕獲Tab鍵的一種方法是將窗體的KeyPreview屬性設置爲True,並將TabStop屬性設置爲false,以便窗體上的每個控件都可以。VB6捕獲Tab鍵

我的問題是,如果有什麼方法可以在vb6中捕獲Tab鍵而不必禁用所有的TabStops。

回答

1

如何試圖看待不同的財產?

當控件「enter」事件被觸發時可以閱讀幫助您確定向下滾動? 當用戶選中每個項目並可以允許您確定滾動視圖的位置時,就會發生這種情況。

+0

這不是一個答案,因爲它並沒有回答這個問題。如果你有足夠的代表發佈一個,這將是一個很好的評論。答案空間不應該用於繞過您此時的有限特權。任何發佈爲答案的東西都需要實際上是一個答案。 [幫助]頁面有更多信息。 –

+2

這種方法(_GotFocus事件)可能比對標籤鍵 –

+1

@AlexK行爲更好。借調,尤其是人們可能使用訪問密鑰或任何其他方法來設置焦點。 – Deanna

1

我想我有一個答案給你,但它沒有經過測試。

使用計時器控件,獲取主動控件。然後查看窗體頂部的&高度屬性,與活動控件的高度屬性相比,您可以確定活動控件是否位於窗體的可視區域內,如果需要,可以滾動窗體。

希望它有幫助。

1

不能捕捉標籤按在VB6的形式(使用本地方法),當有控件可以獲得焦點。

但是,您可以使用其他方法,如a timer checking the current controlcapturing the GotFocus events。這些還將處理獲得焦點的其他方式,或者直接點擊鼠標。

+0

我嘗試了gotfocus和timer方法,並決定使用timer方法,因爲它不需要隨我添加的每個動態控件一起更改。謝謝! –

0

以編程方式掃描窗體上的控件。

請注意那些具有TabStop = True的人。

將它們放入按TabIndex排序的列表中。

清除所有控件的TabStop = True屬性。

打開窗體的KeyPreview選項。

捕獲表單的_KeyDown事件。

如果鍵是選項卡鍵,請逐步通過控制列表查找下一個或上一個鍵並將焦點設置爲該鍵。

如果焦點位於網格上,您可以選擇性地向前或向後移動所選的Row/Col。

我在石器時代寫了這個課。可恥的醜陋代碼,但它的工作原理。

形成多個控件加了網格:

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 

類:

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 

' ------------------------------------------------------------------------------ 
' ------------------------------------------------------------------------------