C to get the method of right clicking the cell with the mouse on listview
- 2021-12-04 10:58:23
- OfStack
When we right-click on the listview control, we can get the text content of the selected item.
Now we want to get only the text content of the cell at the time of right-clicking.
The method is as follows:
1. Define the global mouse state
Point m_MBRpt; //Point when right-clicking
2. Handle the message in listView when the mouse is pressed
private void listView1_MouseDown(object sender, MouseEventArgs e)
{
//
if (e.Button==MouseButtons.Right)
{
// Get the coordinates of the screen mouse and convert it into the coordinates of the list control
m_MBRpt = listView1.PointToClient(Control.MousePosition);
}
}
3. Right-click menu-copy item message
private void COPYITEM_Click(object sender, EventArgs e)
{
// Copies the contents of the specified table cell
if (listView1.SelectedItems.Count <= 0)
{
MessageBox.Show(" No transaction information selected! ", " Prompt ", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
// Get the coordinates of the screen mouse and convert it into the coordinates of the list control ( When the mouse is pressed, it will be processed and saved )
//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 pasting board
SetClipboardText(strText);
}
4. Set the contents of the sticker board
public void SetClipboardText(string strText)
{
try
{
Clipboard.SetDataObject(strText);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message, " Prompt ", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}