Got Focus Tab event

SonicBoomAu

Centurion
Joined
Oct 30, 2003
Messages
179
Location
Australia
Hi All,

I hoping this can be done, and i sure someone here can tell me, so here goes.

I am using a Windows form and would like a combo box to drop down automatically but only if it has been Tab'd to.

At the moment my code is:
Visual Basic:
Private Sub cboWings_GotFocus(ByVal sender as Object, Byval e as System.eventArgs) Handles cboWings.GotFocus
' Automatically drop down the list for selection
me.cbowings.droppedDown = True
End Sub

What i would like to do is throw in a If statement, so the me.cboWings.droppedDown = True on fires when Tab was used to get to that control.

Visual Basic:
Private Sub cboWings_GotFocus(ByVal sender as Object, Byval e as System.EventArgs) Handles cboWings.GotFocus
' Automatically drop down the list for selection if key press was tab
If keypress = TAB then
me.cbowings.droppedDown = True
End If
End Sub

Is something like this possible?

At the moment with the current code someone has to click on the dropdown arrow to display the items in the control. The first click displays the items but automatically hides them again.

Thank you all in advance for your help.
 
I have tried to capture the keypress event, but it somehow misses the tab keypress. Code is:

Visual Basic:
Private Sub txtCourseDates_KeyPress(byVal sender as Object, ByVal e as System.Windows.Forms.KeyPressEventArgs) Handles txtCourseDates.KeyPress
     If e.KeyChar = Chr(keys.Tab) then
          blnTabPress = True
     End If
End Sub

Before you ask blnTabPress is declared earlier. I then tried to capture the Tab using the Lost Focus event. But this threw a NullException Error. Code is
:

Visual Basic:
Private Sub txtCourseDates_LostFocus(byVal sender as Object, ByVal e as System.EventArgs) Handles txtCourseDates.LostFocus
     Dim ek as System.Windows.Forms.KeyPressEventArgs
     If ek.KeyChar = Chr(keys.Tab) then
          blnTabPress = True
     End If
End Sub

Does anyone have any ideas?
 
With the help of some other websites. I have managed to capture and release the TAB key press event. The code used is below to help the next person.


Firstly create a Class and paste in the following code:

Visual Basic:
Option Strict On
Option Explicit On

Public Class TextBoxEx
    Inherits System.Windows.Forms.TextBox

    Private _TabRaisesEvents As Boolean = False

    Protected Overrides Function IsInputKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean
        If keyData = Keys.Tab Then
            Return _TabRaisesEvents
        Else
            Return MyBase.IsInputKey(keyData)
        End If
    End Function

    Public Property TabRaisesEvents() As Boolean
        Get
            Return _TabRaisesEvents
        End Get
        Set(ByVal value As Boolean)
            _TabRaisesEvents = value
        End Set
    End Property
End Class

"then compile your project, and replace your existing textbox control, with this one from your toolbox. This will allow tab to fire the keypress and keydown events." Code and quote from here

I just changed my textboxes in the Windows Form Designer generated code area from 'Friend WithEvents txtSigneeName as System.Windows.Forms.TextBox to Friend WithEvents txtSigneeName as TextBoxEx' and 'Me.txtCourseDates = New System.Windows.Forms.TextBox to Me.txtCourseDates = New TextBoxEx'

The keypress press Event is as follows:

Visual Basic:
Private Sub txtSigneeName_KeyPress (ByVal sender AS Object, ByVal e AS System.Windows.Forms.KeyPressEventArgs) Handles txtSigneeName.KeyPress
     ' Set .TabRaisedEvent to True
     Me.txtSigneeName.TabRaisesEvents = True
     ' Capture if the Tab key was used to navigate to next control
     If e.char = Chr(Keys.Tab) Then
          blnTabPress = True
          ' Set the TabRaisesEvents to False so that Tab can now act as normal
          Me.txtSigneeName.TabRaisesEvents = False
          ' Generate Tab key press so that focus goes to the next control
          SendKeys.Send ("{tab}")
     End If
End Sub

Hope this helps the next person.
 
Back
Top