none
Show Windows context Menu in listbox RRS feed

  • Question

  • I have a listbox Of files and I want to show "windows conntext menu" when I press right button of mouse.
    Tuesday, May 19, 2015 5:35 PM

Answers

  • Below is an image with three pics. Pic 1 displays a "windows context menu" when the right mouse button is selected on my desktop. Pic 2 does the same except in Internet Explorer. Pic 3 does the same except in a folder displayed in Windows Explorer.

    Note that each "windows context menu" provides programmed capabilities for use with whatever they appear for when the right mouse button is selected. A different "windows context menu" would be displayed in Windows Explorer if drives were displayed or other items other than drives or folders or files were displayed.

    Since you don't provide anything about what you want the "windows context menu" to be used for which would require code to provide functionality based on what it would be supposed to do with regard to a list of files in a ListBox then it may be difficult to assist you.

    Here's some links though. Although there is a standard context menu that is available as a control for windows.forms which can be programmed for whatever you may want to do possibly.

    http://www.codeproject.com/Articles/512956/NET-Shell-Extensions-Shell-Context-Menus

    http://geekswithblogs.net/Jialiang/archive/2010/09/16/writing-windows-shell-extension-with-.net-4---part-1.aspx


    La vida loca

    Wednesday, May 20, 2015 4:50 AM
  • This may help or not.

    https://msdn.microsoft.com/en-us/library/system.windows.forms.contextmenustrip(v=vs.110).aspx

    How to: Associate a ContextMenuStrip with a Control

    Option Strict On
    
    Public Class Form1
    
        WithEvents ContextMenuStrip1 As New ContextMenuStrip
        Dim ADialog As New Windows.Forms.Form
        WithEvents RTB1 As New RichTextBox
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.Location = New Point(CInt((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2)), CInt((Screen.PrimaryScreen.WorkingArea.Height / 2) - (Me.Height / 2)))
    
            ListBox1.Items.AddRange(My.Computer.FileSystem.GetFiles("C:\Users\John\Desktop\ListBox Test").ToArray)
            With ContextMenuStrip1
                .Items.Add("Get selected file size")
                .Items.Add("Get selected file creation date")
            End With
            AddHandler ContextMenuStrip1.ItemClicked, AddressOf ContextMenuStrip1Items_Click
    
            With ADialog
                .FormBorderStyle = Windows.Forms.FormBorderStyle.FixedDialog
                .ControlBox = True
                .MaximizeBox = False
                .MinimizeBox = False
            End With
    
            With RTB1
                .Font = New Font("Book Antiqua", 11)
                .Width = 250
                .Height = 220
                .Location = New Point(CInt((ADialog.Width / 2) - (RTB1.Width / 2)), CInt((ADialog.Height / 2) - (RTB1.Height / 2)))
            End With
    
            ADialog.Controls.Add(RTB1)
        End Sub
    
    
        Private Sub ContextMenuStrip1Items_Click(sender As Object, e As ToolStripItemClickedEventArgs)
            If e.ClickedItem.Text = "Get selected file size" Then
                RTB1.Text = ListBox1.SelectedItem.ToString & " size is " & My.Computer.FileSystem.GetFileInfo(ListBox1.SelectedItem.ToString).Length.ToString & "."
                ContextMenuStrip1.Close()
                ADialog.Show(Me)
            ElseIf e.ClickedItem.Text = "Get selected file creation date" Then
                RTB1.Text = ListBox1.SelectedItem.ToString & " creation time was " & My.Computer.FileSystem.GetFileInfo(ListBox1.SelectedItem.ToString).CreationTime.ToString & "."
                ContextMenuStrip1.Close()
                ADialog.Show(Me)
            End If
        End Sub
    
        Private Sub ListBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles ListBox1.MouseDown
            If e.Button = Windows.Forms.MouseButtons.Right Then
                ContextMenuStrip1.Show(ListBox1, New Point(ListBox1.Location.X + CInt(ListBox1.Width / 2), ListBox1.Location.Y + CInt(ListBox1.Height / 2)))
            End If
        End Sub
    
    End Class
    


    La vida loca

    Wednesday, May 20, 2015 5:35 AM

All replies

  • Below is an image with three pics. Pic 1 displays a "windows context menu" when the right mouse button is selected on my desktop. Pic 2 does the same except in Internet Explorer. Pic 3 does the same except in a folder displayed in Windows Explorer.

    Note that each "windows context menu" provides programmed capabilities for use with whatever they appear for when the right mouse button is selected. A different "windows context menu" would be displayed in Windows Explorer if drives were displayed or other items other than drives or folders or files were displayed.

    Since you don't provide anything about what you want the "windows context menu" to be used for which would require code to provide functionality based on what it would be supposed to do with regard to a list of files in a ListBox then it may be difficult to assist you.

    Here's some links though. Although there is a standard context menu that is available as a control for windows.forms which can be programmed for whatever you may want to do possibly.

    http://www.codeproject.com/Articles/512956/NET-Shell-Extensions-Shell-Context-Menus

    http://geekswithblogs.net/Jialiang/archive/2010/09/16/writing-windows-shell-extension-with-.net-4---part-1.aspx


    La vida loca

    Wednesday, May 20, 2015 4:50 AM
  • This may help or not.

    https://msdn.microsoft.com/en-us/library/system.windows.forms.contextmenustrip(v=vs.110).aspx

    How to: Associate a ContextMenuStrip with a Control

    Option Strict On
    
    Public Class Form1
    
        WithEvents ContextMenuStrip1 As New ContextMenuStrip
        Dim ADialog As New Windows.Forms.Form
        WithEvents RTB1 As New RichTextBox
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.Location = New Point(CInt((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2)), CInt((Screen.PrimaryScreen.WorkingArea.Height / 2) - (Me.Height / 2)))
    
            ListBox1.Items.AddRange(My.Computer.FileSystem.GetFiles("C:\Users\John\Desktop\ListBox Test").ToArray)
            With ContextMenuStrip1
                .Items.Add("Get selected file size")
                .Items.Add("Get selected file creation date")
            End With
            AddHandler ContextMenuStrip1.ItemClicked, AddressOf ContextMenuStrip1Items_Click
    
            With ADialog
                .FormBorderStyle = Windows.Forms.FormBorderStyle.FixedDialog
                .ControlBox = True
                .MaximizeBox = False
                .MinimizeBox = False
            End With
    
            With RTB1
                .Font = New Font("Book Antiqua", 11)
                .Width = 250
                .Height = 220
                .Location = New Point(CInt((ADialog.Width / 2) - (RTB1.Width / 2)), CInt((ADialog.Height / 2) - (RTB1.Height / 2)))
            End With
    
            ADialog.Controls.Add(RTB1)
        End Sub
    
    
        Private Sub ContextMenuStrip1Items_Click(sender As Object, e As ToolStripItemClickedEventArgs)
            If e.ClickedItem.Text = "Get selected file size" Then
                RTB1.Text = ListBox1.SelectedItem.ToString & " size is " & My.Computer.FileSystem.GetFileInfo(ListBox1.SelectedItem.ToString).Length.ToString & "."
                ContextMenuStrip1.Close()
                ADialog.Show(Me)
            ElseIf e.ClickedItem.Text = "Get selected file creation date" Then
                RTB1.Text = ListBox1.SelectedItem.ToString & " creation time was " & My.Computer.FileSystem.GetFileInfo(ListBox1.SelectedItem.ToString).CreationTime.ToString & "."
                ContextMenuStrip1.Close()
                ADialog.Show(Me)
            End If
        End Sub
    
        Private Sub ListBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles ListBox1.MouseDown
            If e.Button = Windows.Forms.MouseButtons.Right Then
                ContextMenuStrip1.Show(ListBox1, New Point(ListBox1.Location.X + CInt(ListBox1.Width / 2), ListBox1.Location.Y + CInt(ListBox1.Height / 2)))
            End If
        End Sub
    
    End Class
    


    La vida loca

    Wednesday, May 20, 2015 5:35 AM