C#获取鼠标在listview右键点击单元格的内容方法
作者:jingxian 时间:2023-10-26 12:40:47
当我们右键点击listview控件时,可以得到选择的项的各个文本内容。
现在我们要求只获取右键点击时的单元格的文本内容。
方法如下:
1、定义全局的鼠标状态
Point m_MBRpt;//鼠标右键点击时Point
2、处理鼠标按下时在listView的消息
private void listView1_MouseDown(object sender, MouseEventArgs e)
{
//
if (e.Button==MouseButtons.Right)
{
// 得到屏幕鼠标的坐标,转换为列表控件的坐标标
m_MBRpt = listView1.PointToClient(Control.MousePosition);
}
}
3、右键菜单--复制项的消息
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);
}
4、设置粘贴板内容
public void SetClipboardText(string strText)
{
try
{
Clipboard.SetDataObject(strText);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
标签:鼠标,listview,右键
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java多线程之读写锁分离设计模式
2021-06-08 07:20:48
详解android 中animation-list 动画的应用
2022-09-13 18:28:31
idea的使用之关于tomcat热部署的教程
2022-12-02 20:16:46
![](https://img.aspxhome.com/file/2023/3/129953_0s.png)
SpringBoot接口如何统一异常处理
2023-08-10 15:06:20
![](https://img.aspxhome.com/file/2023/9/84309_0s.png)
使用spring容器在初始化Bean时前和后的操作
2021-07-01 05:49:24
maven的生命周期及常用命令介绍
2022-03-10 17:21:16
![](https://img.aspxhome.com/file/2023/3/75443_0s.png)
sin(x)如何求解的java代码实现方法
2022-04-17 16:22:07
![](https://img.aspxhome.com/file/2023/3/64793_0s.jpg)
Android自带emoji表情的使用方法详解
2021-11-25 09:28:22
![](https://img.aspxhome.com/file/2023/7/94967_0s.png)
spring多数据源配置实现方法实例分析
2023-06-06 22:15:56
C#中动态数组用法实例
2021-11-30 16:42:23
Android Loop机制中Looper与handler详细分析
2023-01-13 04:40:56
![](https://img.aspxhome.com/file/2023/3/90183_0s.png)
Eclipse 2020-06 汉化包安装步骤详解(附汉化包+安装教程)
2021-05-31 09:26:37
![](https://img.aspxhome.com/file/2023/8/112048_0s.jpg)
java中switch选择语句代码详解
2023-09-28 19:34:29
Android之线程池ThreadPoolExecutor的简介
2021-06-27 02:41:25
SpringBoot整合POI导出通用Excel的方法示例
2021-12-30 21:13:12
![](https://img.aspxhome.com/file/2023/5/97825_0s.png)
SpringBoot 整合mybatis+mybatis-plus的详细步骤
2022-09-29 21:06:10
![](https://img.aspxhome.com/file/2023/5/72915_0s.png)
C语言实现模拟银行系统
2022-01-17 08:40:27
浅析C#中的Main(String[] args)参数输入问题
2023-08-13 01:45:58
![](https://img.aspxhome.com/file/2023/3/74813_0s.jpg)
详解Java如何改变字符串中的字符
2022-06-13 14:29:48
![](https://img.aspxhome.com/file/2023/2/121952_0s.png)
Java png图片修改像素rgba值的操作
2022-08-11 06:57:21