This article is a mirror article of machine translation, please click here to jump to the original article.

View: 20353|Reply: 0

[WinForm] C# gets the contents of a cell by right-clicking on the mouse in the listview

[Copy link]
Posted on 1/29/2015 3:15:34 PM | | |

When we right-click on the ListView control, we can get the individual text content of the selected item.
Now we ask to get only the text content of the cell when right-clicked.
Here's how:
1. Define the global mouse state
Point m_MBRpt; When you right-click Point
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 the coordinates of the screen mouse and convert them into the coordinates of the list controls
                m_MBRpt = listView1.PointToClient(Control.MousePosition);
            }

        }
3. Right-click menu - the message of the copy item
        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!") , "Prompt", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            Get the coordinates of the screen mouse and convert them into the coordinates of the list control (process and save 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 the paste board
            SetClipboardText(strText);

        }
4. Set the content of the paste board
        public void SetClipboardText(string strText)
        {
            try
            {
                Clipboard.SetDataObject(strText);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex. Message, "Prompt", MessageBoxButtons.OK, MessageBoxIcon.Error);            
            }
            
        }




Previous:Dell Server Error Codes and Solutions 2013 Edition
Next:DIV+CSS rounded borders
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com