Results 1 to 9 of 9

Thread: strange mouseclick event behaviour on combobox

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2016
    Posts
    255

    strange mouseclick event behaviour on combobox

    I have declared a mouse click event on a combo box and notice that the event routine is only entered after left click. On right click no event is executed, but in case of a left click after right click the e.button indicates a right click


    Code:
    Private WithEvents CB_Lieferanten As New ComboBox
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Controls.Add(CB_Lieferanten) 
    End Sub
    Private Sub CB_Lieferanten_MouseUp(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles CB_Lieferanten.MouseUp
            MsgBox(e.Button.ToString)
    End Sub
    I use the same declarations with DGVs and it works correctly.

  2. #2
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: strange mouseclick event behaviour on combobox

    The event is fired when you click the right button, but the context menu might be interfering and you only see the message box when you left click to get rid of it. same effect with a second right click, enter or other actions.

    Try this

    Code:
    Private WithEvents CB_Lieferanten As New ComboBox
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Controls.Add(CB_Lieferanten)
        CB_Lieferanten.ContextMenuStrip = New ContextMenuStrip
    End Sub
    Private Sub CB_Lieferanten_MouseUp(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles CB_Lieferanten.MouseUp
        MsgBox(e.Button.ToString)
    End Sub
    Last edited by kaliman79912; Jul 1st, 2016 at 10:58 AM.
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2016
    Posts
    255

    Re: strange mouseclick event behaviour on combobox

    I made some tests and it turns out that the mouse up event gives some problems. I have also changed the mouse in order to eliminate possible hard ware problems, but got the same results. When the right mouse goes up the regular menu (see first image) comes up, just as if no events have been programed. With the next lelf mouse down the program goes into the event procedure twice and shoes right button and after that left button. I tried to avoid the interference problem by writing text into a text field (see second image).
    The procedures are given here:
    Code:
        Private Sub CB_test_MouseDown(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles CB_test.MouseDown
            TextBox2.AppendText("down " + e.Button.ToString + vbCrLf)
        End Sub
        Private Sub CB_test_MouseUp(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles CB_test.MouseUp
            TextBox2.AppendText("up " + e.Button.ToString + vbCrLf)
        End Sub
    Name:  Clipboard01.jpg
Views: 669
Size:  14.3 KB
    Name:  Clipboard02.jpg
Views: 652
Size:  8.3 KB

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Mar 2016
    Posts
    255

    Re: strange mouseclick event behaviour on combobox

    I suspected that something in my program is corrupted. So, I created an entirely new project but found the same behaviour. The problem seems to be related with the mouse up event.

  5. #5
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: strange mouseclick event behaviour on combobox

    I feel like you did not read my post #2.

    There is nothing wrong, it is a mater of what are you trying to do. The progrm is doing exactly what is supposed to do. Here is what is happening:

    When you press the left mouse button (actually when you release it) then the event triggers and you get your expected result. But when you press the right button you are calling the context menu strip (that happens before you release it so your procedure is not yet activated). At that moment you get presented with such menu, since it is modal the message can't be shown. Once you clear it, your procedure shows the message.

    Currently I believe you are always pressing the left mouse button right after the right one and that is giving you a wrong perception of the cause, but the same effect would happen if you press the right button again, you press enter, press esc, press alt or maybe others.

    First of all, is there a specific reason you are handling the MouseUp? why not use the MouseClick (MouseClick will still show the MenuStrip) or even the MouseDown? (MouseDown will trigger before, handling the click and efectively preventing the ManuStrip from showing). But if you need to use the MouseUp event then assign an empty menuStrip to the combobox as I showed you in post #2

    CB_Lieferanten.ContextMenuStrip = New ContextMenuStrip
    Last edited by kaliman79912; Jul 1st, 2016 at 12:31 PM.
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

  6. #6
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: strange mouseclick event behaviour on combobox

    Quote Originally Posted by smktec View Post
    I have declared a mouse click event on a combo box and notice that the event routine is only entered after left click.
    All controls follow a specific order of raising events, see MSDN,
    Mouse Events in Windows Forms
    If you want to handle mouse click events in the proper order, you need to know the order in which click events are raised in Windows Forms controls. All Windows Forms controls raise click events in the same order when a mouse button is pressed and released (regardless of which mouse button), except where noted...
    https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx

    I've only ran into a couple situations where I had to override or subclass so mouse click events worked exactly the way I needed, its very rare, one thing that surprised me the most was after double mouse clicking the title bar of a form to maximize the form a mouse_up message was raised from the form/control that was then under the mouse pointer! Basically what goes "down" must come "up" !
    Last edited by Edgemeal; Jul 1st, 2016 at 01:55 PM.

  7. #7
    Hyperactive Member Vexslasher's Avatar
    Join Date
    Feb 2010
    Posts
    429

    Re: strange mouseclick event behaviour on combobox

    I just do this like this in a timer for detecting all 3 types of clicks.
    vb.net Code:
    1. Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    2.         'detects the mouse clicks
    3.         Select MouseButtons
    4.             Case Windows.Forms.MouseButtons.Left
    5.                 ListBox1.Items.Add("Left")
    6.             Case Windows.Forms.MouseButtons.Right
    7.                 ListBox1.Items.Add("Right")
    8.             Case Windows.Forms.MouseButtons.Middle
    9.                 ListBox1.Items.Add("Middle")
    10.         End Select
    11.     End Sub

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Mar 2016
    Posts
    255

    Re: strange mouseclick event behaviour on combobox

    I made some further tests and have the impression something is corrupted either in this project or in my vb installation:
    As I indicated earlier the mouse up event for the right button is not processed immediately, but later after hidding the left mouse button.
    I then took a earlier project which had comboboxes and uses the right mouse button up event to bring up a context menu strip. I noticed that any new combobox showed the problem but a copy the the existing correctly working cb also behaved correctly and did not show the error.
    I wonder now how this can be fixed. Any ideas?

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Mar 2016
    Posts
    255

    Re: strange mouseclick event behaviour on combobox

    I tested your program to detect the mouse clicks. The mouse clicks left and right are correctly shown in the list box. So, I suspect that the Combobox3.MouseUp event does not call the corresponding sub ComboBox3_MouseUp.

    Code:
     Private Sub ComboBox3_MouseUp(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles ComboBox3.MouseUp

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