שתף באמצעות


How to implement context menu in ListBox

Question

Friday, October 14, 2016 10:21 AM

Hi everybody,

just wanna know how can I implement a context menu in ListBox with option to delete the line (data) in focus.

little bit Similar to windows utility "Task Scheduler", please see image below.

I guess that is a ListBox, if not, please correct me and show me how to implement a context menu on/in that control.

All replies (10)

Saturday, October 15, 2016 11:04 AM ✅Answered

 If you used leshay`s code exactly then that would be the way it should work.  With his code,  the menu is canceled from opening if there is no items added to the ListView.  I can see that you have items added to your listview in that image.

 Perhaps you want to cancel the menu from opening if there is not an item selected like shown in the example below.  You can test it in a new Form project with 1 ListView and 1 ContextMenuStrip added to the form.  Add 1 item to the ContextMenuStrip with its text set to "Delete".

Imports System.ComponentModel

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        With ListView1
            .View = View.Details
            .FullRowSelect = True
            .ContextMenuStrip = ContextMenuStrip1
            .Columns.Add("Name", 120, HorizontalAlignment.Left)
            .Columns.Add("Status", 70, HorizontalAlignment.Left)
        End With

        Dim names() As String = {"Jack", "Angel", "Don"}
        Dim status() As String = {"Ready", "Busy", "Ready"}
        For i As Integer = 0 To names.Length - 1
            Dim lvi As ListViewItem = ListView1.Items.Add(names(i))
            lvi.SubItems.Add(status(i))
        Next
    End Sub

    Private Sub ContextMenuStrip1_Opening(sender As Object, e As CancelEventArgs) Handles ContextMenuStrip1.Opening
        e.Cancel = (ListView1.SelectedItems.Count = 0) 'cancel the menu from opening if there is not an item selected 
    End Sub

    Private Sub DeleteToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles DeleteToolStripMenuItem.Click
        ListView1.Items.Remove(ListView1.SelectedItems(0))
    End Sub
End Class

 

 Notice,  the menu only pops up if i right click on an item in the ListView.

If you say it can`t be done then i`ll try it


Friday, October 14, 2016 10:27 AM

 Use the ListBox.Items.Remove OR ListBox.Items.RemoveAt method in your Delete click event.

    Private Sub DeleteToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles DeleteToolStripMenuItem.Click
        If ListBox1.SelectedIndex <> -1 Then
            ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
        End If
    End Sub

 

    Private Sub DeleteToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles DeleteToolStripMenuItem.Click
        If ListBox1.SelectedItem IsNot Nothing Then
            ListBox1.Items.Remove(ListBox1.SelectedItem)
        End If
    End Sub

If you say it can`t be done then i`ll try it


Friday, October 14, 2016 10:29 AM

Hi

Yes you can implement a ContextMenuStrip on a ListBox.

1. Drag a ContextMenuStrip control onto the Form in the Designer.

2. Add menu items to the ContextMenuStrip

3. Associate the ContextMenuStrip. Edit  the properties of the ListBox and choose the ContextMenu drop down where you select the ContextMenuStrip you have just made (the default will be ContextMenuStrip1)

4. Double click on each of the ContextMenuStrip items which will create the click event handlers for the item - you need to code for the functions you want that menu item to perform.

Regards Les, Livingston, Scotland


Friday, October 14, 2016 11:26 AM

 Now that i look again,  that looks to be a ListView,  not a ListBox.  There is a big difference between these two controls.  However,  you can still do about the same thing as my first example.

    Private Sub DeleteToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles DeleteToolStripMenuItem.Click
        If ListView1.SelectedItems.Count > 0 Then
            ListView1.Items.Remove(ListView1.SelectedItems(0))
        End If
    End Sub
    Private Sub DeleteToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles DeleteToolStripMenuItem.Click
        If ListView1.SelectedIndices.Count > 0 Then
            ListView1.Items.RemoveAt(ListView1.SelectedIndices(0))
        End If
    End Sub

If you say it can`t be done then i`ll try it


Friday, October 14, 2016 12:50 PM

Is that a listview or ListBox???

maybe it's some kind of datagrid...  something.

I don't think it's a listbox, it might be a listview.

Please can you help ??


Friday, October 14, 2016 1:01 PM

Is that a listview of ListBox???

maybe it's some kind of datagrid...  something.

Please can you help ??

 Sure looks like a ListView to me but,  maybe it is a DataGridView.  You should know what kind of control it is if you have made this app.  I don`t understand how you would not know what kind of control it is.  Look at its Properties and see if they line up with the properties of a ListView or DataGridView.  It is surely not a ListBox,  a ListBox does not have Column Headers as it shows in your image.

If you say it can`t be done then i`ll try it


Friday, October 14, 2016 2:05 PM

Is that a listview of ListBox???

maybe it's some kind of datagrid...  something.

Please can you help ??

 Sure looks like a ListView to me but,  maybe it is a DataGridView.  You should know what kind of control it is if you have made this app.  I don`t understand how you would not know what kind of control it is.  Look at its Properties and see if they line up with the properties of a ListView or DataGridView.  It is surely not a ListBox,  a ListBox does not have Column Headers as it shows in your image.

If you say it can`t be done then i`ll try it

It's not my app. 

I said it's MS windows utility called "Task Scheduler" that you see in the picture;

Anyway, I picked the ListView and work perfect. Thanks for your help.

Besides, I also wanted :

I created the Context Menu as user leshay showed me, but what to do if I want not to display the context Menu when the Listview has nothing (empty) as items. 


Friday, October 14, 2016 2:28 PM | 1 vote

Is that a listview of ListBox???

maybe it's some kind of datagrid...  something.

Please can you help ??

 Sure looks like a ListView to me but,  maybe it is a DataGridView.  You should know what kind of control it is if you have made this app.  I don`t understand how you would not know what kind of control it is.  Look at its Properties and see if they line up with the properties of a ListView or DataGridView.  It is surely not a ListBox,  a ListBox does not have Column Headers as it shows in your image.

If you say it can`t be done then i`ll try it

It's not my app. 

I said it's MS windows utility called "Task Scheduler" that you see in the picture;

Anyway, I picked the ListView and work perfect. Thanks for your help.

Besides, I also wanted :

I created the Context Menu as user leshay showed me, but what to do if I want not to display the context Menu when the Listview has nothing (empty) as items. 

Hi

If you double click on the ContextMenuStrip in the designer, you will get the Opening event handler for the menu.

If you check the count property of the items in the listview then you can cancel the menu if less than 1

See:

    Private Sub ContextMenuStrip1_Opening(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles ContextMenuStrip1.Opening
        If ListView1.Items.Count < 1 Then
            e.Cancel = True
            Exit Sub
        End If

        ' rest of your code here (for contectmenu opening)

    End Sub

Regards Les, Livingston, Scotland


Saturday, October 15, 2016 8:36 AM

Thanks for the code. it works perfect.

I still have an issue : When I right-click on an area that is not filled I still have the context menu appearing.

How do i handle that empty area??

or

How do I disable the menus when I click on an area that doesn't have data. 

The area I circled in blue in the below picture.


Sunday, October 16, 2016 7:05 AM

Thanks user IronRazerz. It now works perfectly well.

leshay code wasn't doing it.

He used ListView1.Items.Count instead of ListView1.SelectedItems.Count.

I changed his code to the bold below and works.

If ListView1.SelectedItems.Count < 1 Then
            e.Cancel = True
            Exit Sub
End If

by the way, what software did u use to do the small video (gif) you put in your previous post, and how did you do that?

Thanks.