none
How can i create a "right-click menu" i C#

    Question

  • Hi all.

    How can i create a "right-click menu" in some datagridviews? and how can i avoid a "right-click menu" over other parts of my program?

    I am developing a mdi-application, and my default "right-click menu" is all open forms. I want to create my own menu and not show the default one.

    Does anyone have a url where i can read/get som info on how to create this.?

     

    Thanks.

     

     

    Tuesday, September 12, 2006 7:54 AM

Answers

  • Simply drag a ContextMenu or ContextMenuStrip (VS2005) component into your form and visually design it, then assign it to the ContextMenu or ContextMenuStrip property of your desired control.

    To do this programatically, suppose your datagridview is dataGridView1 and you want to a add three items right-click menu, copy, cut, paste. Here are the code to do this:

    ContextMenuStrip mnu = new ContextMenuStrip();
    ToolStripMenuItem mnuCopy = new ToolStripMenuItem("Copy");
    ToolStripMenuItem mnuCut = new ToolStripMenuItem("Cut");
    ToolStripMenuItem mnuPaste = new ToolStripMenuItem("Paste");
    //Assign event handlers
    mnuCopy.Click += new EventHandler(mnuCopy_Click);
    mnuCut.Click +=
    new EventHandler(mnuCut_Click);
    mnuPaste.Click +=
    new EventHandler(mnuPaste_Click);
    //Add to main context menu
    mnu.Items.AddRange(new ToolStripItem[] { mnuCopy, mnuCut, mnuPaste});
    //Assign to datagridview
    dataGridView1.ContextMenuStrip = mnu;

    If you want standard menu and menu items, or if you are working on VS .NET 2002/2003 then use ContextMenu instead of ContextMenuStrip, MenuItem instead of ToolStripMenuItem, MenuItems instead of Items

    The code will be like this:

    ContextMenu mnu = new ContextMenu();
    MenuItem mnuCopy = new MenuItem("Copy");
    MenuItem mnuCut = new MenuItem("Cut");
    MenuItem mnuPaste = new MenuItem("Paste");
    mnuCopy.Click +=
    new EventHandler(mnuCopy_Click);
    mnuCut.Click +=
    new EventHandler(mnuCut_Click);
    mnuPaste.Click +=
    new EventHandler(mnuPaste_Click);
    mnu.MenuItems.AddRange(
    new MenuItem[] { mnuCopy, mnuCut, mnuPaste});
    dataGridView1.ContextMenu = mnu;

    In both cases, you will have to either define the three methods mnuCopy_Click, mnuCut_Click, mnuPaste_Click or remove the three lines of attaching events!

    Hope this helped!

    Tuesday, September 12, 2006 8:08 AM
  • Usually, this is done in the CellMouseDown event of the DataGridView control (I'm assuming you are using a DataGridView in .NET framework v2 (Visual Studio 2005)).

    When handling the CellMouseDownEvent, the event handler method is usually of the form:

    dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)

    wih the e argument containing information on the current location and mouse buttons. Now, if you want the data grid view to have a context menu that includes a menu item and that menu item's action depends on the cell being right-clicked (which i expect to be your case), you might store this information for later reference and use it in the event handler for the context menu's menu item. As an example, let's suppose you want the mnuCopy menu item we created earlier in the previous example to copy the contents of the right-clicked cell to the clipboard, you will need to do the following:

    1. Define two integer fields in your form class to store the location of the mouse right-click, your code should be something like:

    public partial class Form1 : Form {

    private int col = -1;
    private int row = -1;

    2. In the designer view, select the datagridview and go to the events tab in the properties window, scroll to the CellMouseDown event and attach a new event handler (simply by double clicking or pressing enter), within the code view, make the event handler store the location if the mouse was right clicked. Your code should look like:

    private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) {

    if (e.Button == MouseButtons.Right) {

    row = e.RowIndex;
    col = e.ColumnIndex;

    }

    }

    3. Now, in the mnuCopy event handler put the code to copy the contents of the cell. Your code should look like:

    private void mnuCopy_Click(object sender, EventArgs e) {

    if (row >= 0 && col >= 0) {

    Clipboard.SetData(DataFormats.Text, dataGridView1.Rows[row].Cells[col].Value.ToString());

    }

    }

    //Here is a bonus: the paste event handler.
    //Yuo should attach this method to the mnuPaste.Click event just like you did with the above one!
    private void mnuPaste_Click(object sender, EventArgs e) {

    //This assumes all your datagrid values are strings, so use wisely
    //If you want to support other formats like images and the like, check
    //The DataFormats class!
    //You might handle the DataError event to create a custom message box when data mismatches
    dataGridView1.Rows[row].Cells[col].Value = Clipboard.GetData(DataFormats.Text);

    }

    That's it. Hope i helped!

    Tuesday, September 12, 2006 10:15 AM

All replies

  • Simply drag a ContextMenu or ContextMenuStrip (VS2005) component into your form and visually design it, then assign it to the ContextMenu or ContextMenuStrip property of your desired control.

    To do this programatically, suppose your datagridview is dataGridView1 and you want to a add three items right-click menu, copy, cut, paste. Here are the code to do this:

    ContextMenuStrip mnu = new ContextMenuStrip();
    ToolStripMenuItem mnuCopy = new ToolStripMenuItem("Copy");
    ToolStripMenuItem mnuCut = new ToolStripMenuItem("Cut");
    ToolStripMenuItem mnuPaste = new ToolStripMenuItem("Paste");
    //Assign event handlers
    mnuCopy.Click += new EventHandler(mnuCopy_Click);
    mnuCut.Click +=
    new EventHandler(mnuCut_Click);
    mnuPaste.Click +=
    new EventHandler(mnuPaste_Click);
    //Add to main context menu
    mnu.Items.AddRange(new ToolStripItem[] { mnuCopy, mnuCut, mnuPaste});
    //Assign to datagridview
    dataGridView1.ContextMenuStrip = mnu;

    If you want standard menu and menu items, or if you are working on VS .NET 2002/2003 then use ContextMenu instead of ContextMenuStrip, MenuItem instead of ToolStripMenuItem, MenuItems instead of Items

    The code will be like this:

    ContextMenu mnu = new ContextMenu();
    MenuItem mnuCopy = new MenuItem("Copy");
    MenuItem mnuCut = new MenuItem("Cut");
    MenuItem mnuPaste = new MenuItem("Paste");
    mnuCopy.Click +=
    new EventHandler(mnuCopy_Click);
    mnuCut.Click +=
    new EventHandler(mnuCut_Click);
    mnuPaste.Click +=
    new EventHandler(mnuPaste_Click);
    mnu.MenuItems.AddRange(
    new MenuItem[] { mnuCopy, mnuCut, mnuPaste});
    dataGridView1.ContextMenu = mnu;

    In both cases, you will have to either define the three methods mnuCopy_Click, mnuCut_Click, mnuPaste_Click or remove the three lines of attaching events!

    Hope this helped!

    Tuesday, September 12, 2006 8:08 AM
  • Thats was very informative. Thank you.

    Just one small question. If i right click on one row in the datagrid which is not selected. How can i get a value from that row?

     

    Tuesday, September 12, 2006 9:41 AM
  • Usually, this is done in the CellMouseDown event of the DataGridView control (I'm assuming you are using a DataGridView in .NET framework v2 (Visual Studio 2005)).

    When handling the CellMouseDownEvent, the event handler method is usually of the form:

    dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)

    wih the e argument containing information on the current location and mouse buttons. Now, if you want the data grid view to have a context menu that includes a menu item and that menu item's action depends on the cell being right-clicked (which i expect to be your case), you might store this information for later reference and use it in the event handler for the context menu's menu item. As an example, let's suppose you want the mnuCopy menu item we created earlier in the previous example to copy the contents of the right-clicked cell to the clipboard, you will need to do the following:

    1. Define two integer fields in your form class to store the location of the mouse right-click, your code should be something like:

    public partial class Form1 : Form {

    private int col = -1;
    private int row = -1;

    2. In the designer view, select the datagridview and go to the events tab in the properties window, scroll to the CellMouseDown event and attach a new event handler (simply by double clicking or pressing enter), within the code view, make the event handler store the location if the mouse was right clicked. Your code should look like:

    private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) {

    if (e.Button == MouseButtons.Right) {

    row = e.RowIndex;
    col = e.ColumnIndex;

    }

    }

    3. Now, in the mnuCopy event handler put the code to copy the contents of the cell. Your code should look like:

    private void mnuCopy_Click(object sender, EventArgs e) {

    if (row >= 0 && col >= 0) {

    Clipboard.SetData(DataFormats.Text, dataGridView1.Rows[row].Cells[col].Value.ToString());

    }

    }

    //Here is a bonus: the paste event handler.
    //Yuo should attach this method to the mnuPaste.Click event just like you did with the above one!
    private void mnuPaste_Click(object sender, EventArgs e) {

    //This assumes all your datagrid values are strings, so use wisely
    //If you want to support other formats like images and the like, check
    //The DataFormats class!
    //You might handle the DataError event to create a custom message box when data mismatches
    dataGridView1.Rows[row].Cells[col].Value = Clipboard.GetData(DataFormats.Text);

    }

    That's it. Hope i helped!

    Tuesday, September 12, 2006 10:15 AM
  • Thank you.

    In my form i have 4 datagrids and all 4 have different contex menu. So naturally i create the differents context menus on run-time. It works perfectly.

    BUT: how can i assign ikons to menu item on run time?

     

    Lars

    Thursday, September 14, 2006 8:57 PM
  • you may see the auto genegate code in this context menu to find more info

    hope that help you.

    Friday, September 15, 2006 5:49 AM
  •  Lars E.Nes wrote:

    BUT: how can i assign ikons to menu item on run time?

    Simply use the following code:


    this.helloToolStripMenuItem.Image = Image.FromFile("d:\\test.jpg");
     
    .

    Hope this helps.

    Friday, September 15, 2006 6:03 AM