HelloWorld1
HelloWorld1

Reputation: 14108

Right click menu and menu line

Goal:
Right clicking in the listview and choose different option.

Problem:
There are two problem:
*When I'm right clicking, the left corner of the menu is not exactly located in the arrow's spot location.
*How do I create a line in the menu?

The main problem about the menu

The menu is not located in the right spot once you are right clicking

Need support to create these two redmark.

Request to fulfill these two red marked

private void lstV_Stock_MouseUp(object sender, MouseEventArgs e)
{
    switch (e.Button)
    {

        // Right mouse click
            case MouseButtons.Right:


                ContextMenu myContextMenu = new ContextMenu();

                MenuItem menuItem1 = new MenuItem("New product");
                MenuItem menuItem2 = new MenuItem("Delete");
                MenuItem menuItem3 = new MenuItem("Add quantity");


                // Clear all previously added MenuItems.
                myContextMenu.MenuItems.Clear();

                myContextMenu.MenuItems.Add(menuItem1);
                myContextMenu.MenuItems.Add(menuItem2);
                myContextMenu.MenuItems.Add(menuItem3);

                if (lstV_Stock.SelectedItems.Count > 0) 
                {

                    foreach (ListViewItem item in lstV_Stock.SelectedItems)
                    {
                        myContextMenu.MenuItems[1].Visible = true;
                        myContextMenu.MenuItems[2].Visible = true;
                        myContextMenu.MenuItems[0].Visible = false;
                    }

                }
                else
                {
                    myContextMenu.MenuItems[1].Visible = false;
                    myContextMenu.MenuItems[2].Visible = false;
                    myContextMenu.MenuItems[0].Visible = true;
                }

                myContextMenu.Show(lstV_Stock, this.PointToClient(Cursor.Position), LeftRightAlignment.Right);



                menuItem1.Click += new System.EventHandler(this.menuItem1_Click);


                break;

        }

Upvotes: 3

Views: 7576

Answers (4)

dieend
dieend

Reputation: 2299

For the positioning, you can replace your

myContextMenu.Show(lstV_Stock, this.PointToClient(Cursor.Position), LeftRightAlignment.Right);

to

myContextMenu.Show(lstV_Stock, e.Location(), LeftRightAlignment.Right);

or the point e.X,e.Y. Not from this.PointToClient, but from the MouseEventArgs generating the event. You can check wahat MouseEvent have here.

Upvotes: 3

βӔḺṪẶⱫŌŔ
βӔḺṪẶⱫŌŔ

Reputation: 1296

So, using a ContextMenu is the way to go here. Those "Lines" you're referring to are called Separaters.

If you're creating the COntext Menu in Design View, then click the Context Menu, then right-click inside the menu, and click Insert > Separater.

You can then drag it up or down, or into a sub-menu if you wish.

Upvotes: 0

Henk Holterman
Henk Holterman

Reputation: 273199

Problem

  1. If you just set the ListView.ContextMenu property and remove all your own right-click code, the menu should show up correctly.
  2. For the line you need a ToolStripSeparator item. The designer will create one when you type '-' as the Text. You can drag them in the designer.

Upvotes: 1

Marco
Marco

Reputation: 57573

To create a "line" you have to create a MenuItem with text "-"

Upvotes: 3

Related Questions