Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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:

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.

 

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.

Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

-- Rick Cook, The Wizardry Compiled

Posted
Or would i be better, capturing the keypress before it leaves the last control?

Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

-- Rick Cook, The Wizardry Compiled

Posted

I have tried to capture the keypress event, but it somehow misses the tab keypress. Code is:

 

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

:

 

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?

Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

-- Rick Cook, The Wizardry Compiled

Posted

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:

 

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:

 

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.

Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

-- Rick Cook, The Wizardry Compiled

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...