Another ComboBox Quandry!

I posted this on another forum, but I'd like some more feedback, so I'm posting it here as well. :D

I've got a combobox that I can either type into (I have an autocomplete function implemented, see below), or select a value with my mouse. How can I catch and run a sub on pressing the enter key, tab key, or clicking on a row, but no other keypress

Keypress sub of combobox(es):
Code:
   Private Sub ComboBox_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles CboLookupByAsset.KeyPress, CboServerStatus.KeyPress, CboLookupByName.KeyPress
       formBusy = True
       OOP.AutoComplete(sender, e)
   End Sub

AutoComplete Sub:
Code:

   Friend Shared Sub AutoComplete(ByVal Sender As ComboBox, ByVal e As KeyPressEventArgs)
       Dim myComboBox As ComboBox = Sender
       myComboBox.DroppedDown = True
       If Char.IsControl(e.KeyChar) Then
           If String.Compare(e.KeyChar, Keys.Enter) = -1 Then
               If String.Compare(myComboBox.Name, "cboServerStatus") <> 0 Then
                   'MessageBox.Show("AutoComplete - Enter pressed")
                   ServerInformation(myComboBox)
                   Server_Details.formbusy = False
                   Return
               End If
               'MessageBox.Show(String.Compare(myComboBox.Name, "CboServerStatus"))

           End If
       End If

       With myComboBox

           Dim ToFind As String = .Text.Substring(0, .SelectionStart) & e.KeyChar
           Dim Index As Integer = .FindStringExact(ToFind)

           If Index = -1 Then Index = .FindString(ToFind)
           If Index = -1 Then Return

           .SelectedIndex = Index
           .SelectionStart = ToFind.Length
           .SelectionLength = .Text.Length - .SelectionStart

           e.Handled = True

       End With

   End Sub

And lastly, the code from the selectedvaluechanged in my form:
Code:
Private Sub ComboBox_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CboLookupByAsset.SelectedIndexChanged, CboLookupByName.SelectedIndexChanged
       If Not formBusy Then
           ' In this function, find the appropriate server/assetid and show that in the appropriate combobox (also status )
           Dim myComboBox As ComboBox = sender
           Dim columnName As String
           columnName = IIf(String.Compare(myComboBox.Name.ToLower, "cbolookupbyname") = 0, "AssetID", "ServerName")
           MessageBox.Show(myComboBox, columnName)
           'OOP.DisplayServerComboBox(columnName)
       End If
   End Sub



Autocomplete was scoured from the web and with some help on forums... everything else is mine.

I basically want the same thing to happen if somebody types in the value and hits enter, types in part of the value and hits enter, or selects the item with the mouse. Help is greatly appreciated, as always.


It was suggested that I can use the selectedvaluechanged for the mouse clicks, which I understand... but I'm not 100% sure where to put the catch for the enter key. Should I put it into the autocomplete sub, or should I put it in a keypress event for hte controls on the form

I hope this makes sense the way I've explained it, and I hope some advice can be given on the subject.

Thank you in advance!

Kaleb


Answer this question

Another ComboBox Quandry!

  • Stephan Hofmeister

    I understand the implementation of handlers for the events (Private sub combobox_keypress(byval sender as system.object, byval e as KeyPressEventArgs) handles combobox.keypress). If I handle the lostfocus event, how would I determine if focus was lost on pressing the tab key or clicking with the mouse

    As far as I know, pressing the tab key raises the keyup on the control that receives focus, but raises no events on the control that loses focus. Is there a way around that


  • Karulont

    You need to sub-class the ComboBox in order to get access (override) this method.  See http://msdn.microsoft.com/library/default.asp url=/library/en-us/vbcn7/html/vaconInheritanceForBasicLanguage.asp for details on sub-classing using Visual Basic.

    Joe Stegman
    The Windows Forms Team
    Microsoft Corp.

    This posting is provided "AS IS" with no warranties, and confers no rights


  • ReddGreen

    > I've got a combobox that I can either type into (I have an autocomplete function implemented, see below), or select a value with my mouse. How can I catch and run a sub on pressing the enter key

    Depending on when you want to handle the key and what you need to do, you can override either "KeyDown" or "KeyPress".

    > tab key

    Do you want "Tab" or logical lost focus - if you really want lost focus, then you'll need to override "OnLostFocus".

    > or clicking on a row, but no other keypress

    You'll need to handle SelectedIndexChanged.

    Joe Stegman
    The Windows Forms Team
    Microsoft Corp.

    This posting is provided "AS IS" with no warranties, and confers no rights.


  • Joe Lynn

    Sorry, I responded before thinking.  Tab is special and will require special handling.  From experience (we implemented this in V 2.0) this is going to take some time and effort to get right.  For general information on how keyboard handling works in Windows Forms as well as information on how to trap the Tab key see http://blogs.msdn.com/jfoscoding/archive/2005/01/24/359334.aspx.

    Also note that in general, you'll want to handle both Tab and Shift-Tab. 

    Joe Stegman
    The Windows Forms Team
    Microsoft Corp.

    This posting is provided "AS IS" with no warranties, and confers no rights.


  • David Yue

    I've attempted to implement a keydown event, but it's not catching the tab key being pressed. Is there another way to catch the tab key
  • James Matthews 11

    I wasn't sure if you need to handle "lost focus" or do Tab key specific processing - if you need to do Tab key specific processing then you can do that in KeyDown as well.

    Joe Stegman
    The Windows Forms Team
    Microsoft Corp.

    This posting is provided "AS IS" with no warranties, and confers no rights.


  • IAmBrady

    I was referring to overriding them in a derived class - but handling the event should work as well.

    Joe Stegman
    The Windows Forms Team
    Microsoft Corp.

    This posting is provided "AS IS" with no warranties, and confers no rights.


  • Dhileep

    Thank you for the blog link, Joe, it was extremely informative!! Quick question about it, though, I can't seem to find any implementation of the ProcessDialogKey Method in my installation of VS.Net ... as far as I know, I should have all the components, but I can't seem to implement this protected function, though I found it on the msdn via the log as well.

    Is it something I simply want to implement Or should I figure out why it's missing :S

    Your help is always appreciated,

    Kaleb

  • Tensor

    When you say override these events, do you mean write my own event handlers Or add new event handlers I'm sorry to ask such a simple question, but I'm not sure what you mean.

    I want the selectedindex to be determined if the user presses tab, enter, or clicks with the mouse. If they type in anything else, I want it to continue the auto-complete sub that I have in place. (I don't think that came across quite as clearly as I wanted it to the first time)

  • Another ComboBox Quandry!