none
Show dialog when listbox is clicked or right clicked RRS feed

  • Question

  • I have a listbox.  I want to show a dialog box when the listbox is clicked (or right-clicked).  I'm trying to avoid using a separate button to launch the dialog.  When I place the code to show the dialog in the listbox click procedure, nothing happens.
    Friday, May 31, 2013 2:04 AM

Answers

  • this works

     Private Sub ListBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown
            If e.Button = Windows.Forms.MouseButtons.Right Then
                MsgBox("you clicked me")
            Else
                MsgBox(e.Button.ToString)
            End If
        End Sub

    • Marked as answer by Patrick D W Sunday, June 2, 2013 5:01 PM
    Friday, May 31, 2013 2:21 AM

All replies

  • See if this works for you

    Public Class Form1
    
        Private Sub ListBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseClick
            If e.Button = Windows.Forms.MouseButtons.Right Then
                MsgBox("you clicked me")
            End If
        End Sub
    
        Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    
        End Sub
    End Class

    • Proposed as answer by Mr. Monkeyboy Friday, May 31, 2013 2:19 AM
    Friday, May 31, 2013 2:14 AM
  • Hi,

    if you need to react on rightclick too use the mouseDown or MouseUp event...

        Private Sub ListBox1_MouseDown(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown
            MsgBox("listbox clicked mousedown")
        End Sub

    Regards,

      Thorsten

    • Proposed as answer by DMC_Daniel Friday, May 31, 2013 2:26 AM
    Friday, May 31, 2013 2:15 AM
  • Do you know how to launch the dialog from the ListBox.Click event?

    Private Sub ListBox1_Click(sender As Object, e As EventArgs) Handles ListBox1.Click
            MessageBox.Show("ListBox1 Clicked")
    End Sub


    You've taught me everything I know but not everything you know.

    Friday, May 31, 2013 2:17 AM
  • you will have to use the mouseDown event insted of the MouseClick event ,.
    Friday, May 31, 2013 2:20 AM
  • this works

     Private Sub ListBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown
            If e.Button = Windows.Forms.MouseButtons.Right Then
                MsgBox("you clicked me")
            Else
                MsgBox(e.Button.ToString)
            End If
        End Sub

    • Marked as answer by Patrick D W Sunday, June 2, 2013 5:01 PM
    Friday, May 31, 2013 2:21 AM
  • I've tried the following code, but cannot get the dialog to launch.  I know the dialog works because I've placed a temporary button on the form to launch it.

    Private Sub listMATTERLitigants__Click(sender As Object, e As EventArgs) Handles listMATTERLitigants.Click
                    
                formLITIGANTS.ShowDialog(Me)
            
    End Sub

    Friday, May 31, 2013 2:37 AM
  • That is if you want to fire the event with the right mouse button. Because it dosent fire in the Click event for the listbox control

    Friday, May 31, 2013 2:42 AM
  • Thanks.  That last MouseDown code worked, but I am getting a residual selection outline box in the listbox after the dialog is closed.  The bottom right corner of the selection outline follows the mouse movement.

    Friday, May 31, 2013 2:51 AM
  • Sorry I carnt help with that. Dont forget to mark your answer. And you could ask a new Question about the residual selection outline. Add some code, so if there are mistakes in the code someone might be able to pick up those mistakes for you.

    Good luck

    Friday, May 31, 2013 2:55 AM
  • Got it.  Thanks.
    Friday, May 31, 2013 2:56 AM