Home > Backend Development > C#.Net Tutorial > C# Detailed explanation of how to get the content of the cell that the mouse right-clicks on in the listview

C# Detailed explanation of how to get the content of the cell that the mouse right-clicks on in the listview

黄舟
Release: 2017-03-27 11:38:10
Original
2749 people have browsed it

The following editor will bring you an articleC#How to get the content of a cell when the mouse right-clicks on the listview. The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. Let’s follow the editor to take a look.

When we right-click the listview control, we can get the text content of the selected item.

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

The method is as follows:

1. Define the global mouse state

Point m_MBRpt;//Point when the mouse right button is clicked

2. Process the message in listView when the mouse is pressed

private void listView1_MouseDown(object sender, MouseEventArgs e)
    {
      //
      if (e.Button==MouseButtons.Right)
      {
        // 得到屏幕鼠标的坐标,转换为列表控件的坐标标
        m_MBRpt = listView1.PointToClient(Control.MousePosition);
      }

    }
Copy after login

3. Right-click menu--Message of copied item

private void COPYITEM_Click(object sender, EventArgs e)
    {
      //复制指定表格单元格的内容
      if (listView1.SelectedItems.Count <= 0)
      {
        MessageBox.Show("没有选中交易信息!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        return;
      }

      // 得到屏幕鼠标的坐标,转换为列表控件的坐标标(在鼠标按下时就去处理并保存)
      //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;
      //设置到粘贴板
      SetClipboardText(strText);

    }
Copy after login

4. Set pasteboard content

 public void SetClipboardText(string strText)
    {
      try
      {
        Clipboard.SetDataObject(strText);
      }
      catch (System.Exception ex)
      {
        MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);      
      }
      
    }
Copy after login

The above is the detailed content of C# Detailed explanation of how to get the content of the cell that the mouse right-clicks on in the listview. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template