Avatar of TSFLLC
TSFLLC

asked on 

Windows combobox - setting selected value with AutoComplete using TAB key

I have created a textbox with an AutoComplete with SuggestAppend where my datasource and autocomplete source is a dataview.

I want the following to happen:

1) I begin typing text.  The SuggestAppend begins filling the combobox while the AutoComplete dropdown displays appropriately.
2) When I TAB out of the combobox and a valid value is displayed in the combobox , I want to set the SelectedValue of that entry.  By default you have to press ENTER or click on a row from the dropdown with my mouse.  I want a TAB to do what an enter or mouseclick does.
3) If my SelectedValue is -1 or nothing (whichever is appropriate when a valid value is not in the combobox ) I prompt to add new one with messagebox.  

The issue is that the selectedvalue is always nothing when using TAB.
Visual Basic.NET

Avatar of undefined
Last Comment
TSFLLC
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

The simplist option is to use SendKeys function to send an Enter when a Tab key is pressed in KeyPress of combobox.
Avatar of TSFLLC
TSFLLC

ASKER

Have tried this...not in the combobox keypress though because I dont' see how you're able to get the "TAB" from KeyPressEventArgs.

I use SendKey with other things, however I can't acquire the ValueMember value of the combobox using it.  It's as if the only way the ValueMember gets set is when the true combobox dropdown is selected upon and not the AutoComplete dropdown list.

How can something so simple in theory be so damn hard in reality.  Oh yeah, we're talking Microsoft.  Frustration run amuk.
Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
        Select Case msg.WParam.ToInt32()
            Case CInt(Keys.Up)
                SendKeys.Send("{UP}")
                keybd_event(Keys.Up, MapVirtualKey(Keys.Up, 0), 2, 0) ' Down Arrow Up
                Return True
            Case CInt(Keys.Tab)
                If Me.ActiveControl.GetType.ToString = "System.Windows.Forms.ComboBox" Then
                    SendKeys.Send("{ENTER}")
                    keybd_event(Keys.Enter, MapVirtualKey(Keys.Enter, 0), 2, 0) ' Down Arrow Up
                    Return True
                End If
        End Select
        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function 'ProcessCmdKey

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of TSFLLC
TSFLLC

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

How does the above code deal with Tab?
Avatar of TSFLLC
TSFLLC

ASKER

It may not SPECIFICALLY deal with the Tab key itself so much as the Tab key initiates a Leave from the combobox.  My issue was more about the solution to my problem.  Setting a SelectedValue on a valid combobox entry.  A click with a mouse or pressing enter on a valid entry of the dropdown worked properly, yet a Tab of and by itself didn't.  I could probably just as easily used SendKey to solve the problem....but you gave me a general thought towards a solution.  I spent over an hour trying to get it to work.  To no avail.

I'm all for getting SendKey to work too.  I'm sure the cmbo_Leave(sender) can be implemented with SendKey just as well.  
Avatar of TSFLLC
TSFLLC

ASKER

Found solution through additional research.  Link and code included for others to use.
Visual Basic.NET
Visual Basic.NET

Visual Basic .NET (VB.NET) is an object-oriented programming language implemented on the .NET framework, but also supported on other platforms such as Mono and Silverlight. Microsoft launched VB.NET as the successor to the Visual Basic language. Though it is similar in syntax to Visual Basic pre-2002, it is not the same technology,

96K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo