Results 1 to 10 of 10

Thread: Keyboard button that doesn't change value in ComboBox when pressed.

  1. #1

    Thread Starter
    Addicted Member kutlesh's Avatar
    Join Date
    Jun 2018
    Location
    Skopje, Macedonia
    Posts
    202

    Keyboard button that doesn't change value in ComboBox when pressed.

    I have a VB.NET application and it is for data entry. There are several ComboBox components and only recently I changed going backwards trough the form with Left arrow to PageUp, since pressing Left/Right arrows decreases/increases value in the ComboBox.

    I have this new code with PageUp for my components:

    Code:
        Private Sub cboPol_GotFocus(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cboPol.GotFocus
            cboPol.SelectAll()
        End Sub
    
        Private Sub cboPol_KeyDown(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyEventArgs) Handles cboPol.KeyDown
            Dim KeyCode As Short = eventArgs.KeyCode
            Dim Shift As Short = eventArgs.KeyData \ &H10000
    
            If KeyCode = Keys.PageUp Then
                meEmb.Focus()
            End If
        End Sub
    
        Private Sub cboPol_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles cboPol.KeyPress
            Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
    
            If KeyAscii = Keys.Enter Or KeyAscii = Keys.PageDown Then
    
                If cboPol.Text = "" Then
                    obrazecZaVnesRef.proverkaCelObrazec = True
                    cboPol.Focus()
                    MsgBox(SV20_MessagesModule.VNESETE_POL_MSG, MsgBoxStyle.Information)
                Else
                    obrazecZaVnesRef.pol = vbNetUtilsDZS.convertToPositiveInteger(cboPol.SelectedItem)
                    meRNasmes.Focus()
                End If
            End If
    
            eventArgs.KeyChar = Chr(KeyAscii)
    
            If KeyAscii = 0 Then
                eventArgs.Handled = True
            End If
        End Sub
    
        Private Sub cboPol_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboPol.SelectedIndexChanged
            obrazecZaVnesRef.pol = vbNetUtilsDZS.convertToPositiveInteger(cboPol.SelectedItem)
        End Sub
    Though it seems promising, PageUp changes the value in a ComboBox once you have selected with mouse different value then it was.

    Which keyboard button can be used to jump trough fields of a Form backwards in the field list without affecting the previously selected value in the ComboBox components?

    Or somehow I can prevent changing ComboBox components from changing its value when jumping to other field?

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Keyboard button that doesn't change value in ComboBox when pressed.

    Are you referring to doing "Tab" in the opposite direction?

    If so that is simply Shift+Tab, and I think it should work already without adding any code.

  3. #3

    Thread Starter
    Addicted Member kutlesh's Avatar
    Join Date
    Jun 2018
    Location
    Skopje, Macedonia
    Posts
    202

    Re: Keyboard button that doesn't change value in ComboBox when pressed.

    Yes, perhaps that should work. Ok will check the tab indexes.

    Should it be

    Code:
         Private Sub cboPol_KeyDown(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyEventArgs) Handles cboPol.KeyDown
            Dim KeyCode As Short = eventArgs.KeyCode
            Dim Shift As Short = eventArgs.KeyData \ &H10000
    
            If KeyCode = Keys.PageUp Then
                SendKeys.Send("+{TAB}")
                'meEmb.Focus()
            End If
        End Sub
    instead of just focusing the field directly?

  4. #4
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Keyboard button that doesn't change value in ComboBox when pressed.

    I don't think that's what he's looking for. What he's looking for is how to scroll through the list of items in a combo box, then move on to the next field, with out changing the value of the combobox. As it is, the default behavior is that as your scroll through a CBO with the keyboard, it will select the item you just scrolled to, so when you then tab out, that value is now selected.

    As far as I know, there isn't a way... the only key combinations that come to mind, and I'm not sure if it works would be ESC, Tab.... but I'm not sure if ESC simple closes the drop down, or if it will revert the original selected value.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Addicted Member kutlesh's Avatar
    Join Date
    Jun 2018
    Location
    Skopje, Macedonia
    Posts
    202

    Re: Keyboard button that doesn't change value in ComboBox when pressed.

    How to reprogram different button, like say, Space or Left arrow key or PageUp so that it works like Shift + Tab and go back one field in the list of form fields?

    Should it be on KeyDown?

    Code:
    Private Sub TextBox2_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox2.KeyDown
            If e.KeyCode = Keys.Space Then
                e.Handled = False
                SendKeys.Send("+{TAB}")
            End If
        End Sub
    Or maybe on KeyPress or KeyUp?
    Last edited by kutlesh; Dec 17th, 2019 at 01:46 PM.

  6. #6

    Thread Starter
    Addicted Member kutlesh's Avatar
    Join Date
    Jun 2018
    Location
    Skopje, Macedonia
    Posts
    202

    Re: Keyboard button that doesn't change value in ComboBox when pressed.

    I actually tried with Left arrow key which didn't change the value of the combobox while PageUp did jump to previous field but still changed the value of ComboBox.

    Hmm

    Code:
    Private Sub ComboBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyDown
            If e.KeyCode = Keys.Left Then
                SendKeys.Send("+{TAB}")
            End If
        End Sub
    What the hell! In my app, Left arrow key was decreasing the value of the combobox by one...

  7. #7

    Thread Starter
    Addicted Member kutlesh's Avatar
    Join Date
    Jun 2018
    Location
    Skopje, Macedonia
    Posts
    202

    Re: Keyboard button that doesn't change value in ComboBox when pressed.

    Hmm, is there a difference between Visual Studio 2019 and 2005?
    Because on my home PC I have VS 2019 and Left arrow key when using this code:

    Code:
    Private Sub ComboBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyDown
            If e.KeyCode = Keys.Left Then
                SendKeys.Send("+{TAB}")
            End If
        End Sub
    doesn't change the value of the combobox, while on my work PC where I have VS 2005 this code does change the value of the combo box.

  8. #8

    Thread Starter
    Addicted Member kutlesh's Avatar
    Join Date
    Jun 2018
    Location
    Skopje, Macedonia
    Posts
    202

    Re: Keyboard button that doesn't change value in ComboBox when pressed.

    Turns out, my combo boxes are of style "DropDownList" which doesn't allow typing in the combo box field and only items in the collection are allowed for selection.

    If I set it to style "DropDown", it works, meaning left arrow key doesn't change value, but how do I disable editing of the combo box in "DropDown" mode and allow only the items in the collection to be selected?

  9. #9

    Thread Starter
    Addicted Member kutlesh's Avatar
    Join Date
    Jun 2018
    Location
    Skopje, Macedonia
    Posts
    202

    Re: Keyboard button that doesn't change value in ComboBox when pressed.

    Not sure what it does but certainly works:

    Code:
    Private Sub ComboBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyDown
            If e.KeyCode = Keys.Left Then
                SendKeys.Send("+{TAB}")
                ' OR 
                myField.Focus()
            End If
        End Sub
    
        Private Sub ComboBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress
            e.Handled = True
        End Sub
    with ComboBox1 drop down style set as "DropDown".

    e.Handled = True in ComboBox1_KeyPress somehow prevents typing in the combo box.

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Keyboard button that doesn't change value in ComboBox when pressed.

    The KeyPress event allows you to add custom behaviour, and whatever you put in there happens before the default behaviour for the control.

    By setting e.Handled = True you are saying that the key press has already been dealt with appropriately, so no further actions should be taken based on that key press.

    It is probably a good idea to add an If (or similar) so that you only ignore the key(s) that you want to ignore.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width