C # Get the mouse in the ListView right click on the contents method of the cell

Source: Internet
Author: User
The following small series for everyone to bring a C # get the mouse in the ListView right-click the content method of the cell. Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.

When we right-click the ListView control, we can get the individual text contents of the selected item.

Now we want to get only the text content of the cell when right-clicked.

Here's how:

1, define the global mouse state

Point m_mbrpt;//the right mouse button click

2. Handle the message in the ListView when the mouse is pressed

private void Listview1_mousedown (object sender, MouseEventArgs e)    {      //      if (e.button==mousebuttons.right)      {        //get coordinates of the screen mouse, convert to list control coordinates        M_MBRPT = listview1.pointtoclient (control.mouseposition);}    }

3. Right-click menu--Copy item's message

private void Copyitem_click (object sender, EventArgs e)    {      //Copy the contents of the specified table cell      if ( ListView1.SelectedItems.Count <= 0)      {        MessageBox.Show ("No transaction information selected! "," hint ", MessageBoxButtons.OK, messageboxicon.information);        return;      }      Get the coordinates of the mouse on the screen, convert to the coordinates of the list control (to be processed and saved when the mouse is pressed)      //point pt = listview1.pointtoclient (m_mbrpt);      ListViewItem Lstrow = Listview1.getitemat (M_mbrpt.x, m_mbrpt.y);      System.Windows.Forms.ListViewItem.ListViewSubItem Lstcol = Lstrow. Getsubitemat (M_mbrpt.x, m_mbrpt.y);      String strText = Lstcol. Text;      Set to Pasteboard      Setclipboardtext (strText);    }

4. Set the contents of the Pasteboard

 public void Setclipboardtext (string strText) {try {Clipboard.setdataobje      CT (strText); } catch (System.Exception ex) {MessageBox.Show (ex.            Message, "Hint", MessageBoxButtons.OK, Messageboxicon.error); }          }
Related Article

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.