C # Right-click menu ContextMenuStrip

Source: Internet
Author: User

1. Add ContextMenuStrip control default naming: ContextMenuStrip1

2. On the control that you want to display, locate its ContextMenuStrip property and set it to CONTEXTMENUSTRIP1

For example, with my side on the DataGridView1 control, set the DataGridView1 ContextMenuStrip property to ContextMenuStrip1

3. Write the following code to the DataGridView1 Cellmousedown event

        //When you right-click, select the current row        Private voidDatagridview1_cellmousedown (Objectsender, DataGridViewCellMouseEventArgs e) {            if(E.button = = System.Windows.Forms.MouseButtons.Right && e.rowindex >=0)            {                if(!Datagridview1.rows[e.rowindex].                    Selected) {datagridview1.clearselection (); Datagridview1.rows[e.rowindex]. Selected=true;            } contextmenustrip1.show (mouseposition.x, MOUSEPOSITION.Y); }        }

4. Add a click event for the ToolStripMenuItem control

        Private void Delete Toolstripmenuitem_click (object  sender, EventArgs e)        {            //  Gets the index of the currently selected row t22            int selectrow = dataGridView1.CurrentRow.Index;             if 0 )                return;        }    

C # Right-click menu ContextMenuStrip

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.