Windows combobox - setting selected value with AutoComplete using TAB key

TSFLLC
TSFLLC used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2014

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

Author

Commented:
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

Commented:
Fortunately with a little persistence.

http://www.codeproject.com/vb/net/autocomplete_combobox.asp

The code from this link allowed me to extract a couple of lines for my use.


Public Sub cmbo_Leave(ByVal cbo As ComboBox)
        Dim iFoundIndex As Integer
        iFoundIndex = cbo.FindStringExact(cbo.Text)
        cbo.SelectedIndex = iFoundIndex
    End Sub

    Private Sub cmboLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmboACHBank.Leave, cmboACHBankABANum.Leave, cmboACHTransactionCode.Leave
        If sender.Text.Length > 0 Then
            cmbo_Leave(sender)  ' *** THIS IS THE TICKET ***
            If sender.SelectedValue = Nothing Then
                Beep()
                Dim msg As String = ""
                Select Case sender.name
                    Case "cmboACHBank"
                        msg = "Current value not found in 'ACH BANK' table." & vbCrLf & "Press 'OK' to add this value into the 'ACH BANK' table...'Cancel' to re-enter different value."
                    Case "cmboACHBankABANum"
                        msg = "Current value not found in 'ACH ROUTING #' table." & vbCrLf & "Press 'OK' to add this value into the 'ACH ROUTING #' table...'Cancel' to re-enter different value."
                    Case "cmboACHTransactionCode"
                        msg = "Current value not found in 'ACH TRANSACTION CODE' table." & vbCrLf & "Press 'OK' to add this value into the 'ACH TRANSACTION CODE' table...'Cancel' to re-enter different value."
                End Select
                If MessageBox.Show(Me, msg, "Add New Record?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Cancel Then
                    sender.SelectedValue = 0
                    sender.Focus()
                    Exit Sub
                Else
                    AddTableRec(sender, sender.Text)
                End If
            End If
        End If
    End Sub

Open in new window

CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

Most Valuable Expert 2012
Top Expert 2014

Commented:
How does the above code deal with Tab?

Author

Commented:
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.  

Author

Commented:
Found solution through additional research.  Link and code included for others to use.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial