当前位置:网站首页>C#为listview选中的项添加右键菜单
C#为listview选中的项添加右键菜单
2022-08-02 11:04:00 【幕尘枫】
效果图:

实现步骤:
1、把ContextMenuStrip菜单控件拖放到设计界面上,Name属性设置为cmsListViewItem
2、在ContextMenuStrip控件的Items集合里添加想要的菜单

3、ListView控件添加MouseClick事件,代码如下:
private void listViewZone_MouseClick(object sender, MouseEventArgs e)
{
ListView listView = (ListView)sender;
ListViewItem item = listView.GetItemAt(e.X, e.Y);
if (item != null && e.Button == MouseButtons.Right)
{
//cmsListViewItem是我们添加的菜单控件
this.cmsListViewItem.Show(listView, e.X, e.Y);
}
}4、Listview控件的常规属性设置:
- FullRowSelect属性设置为true,这样item项选中会有背景色;
- GridLines属性设置为true,这样会显示表格线,比较美观;
5、ContextMenuStrip控件的子菜单添加点击事件,比如我选中ListView的一个项后,右键点击“修改”菜单按钮,代码如下:
private void tsmiListViewItemEdit_Click(object sender, EventArgs e)
{
ListView.SelectedIndexCollection indexes = this.listViewZone.SelectedIndices;
if (indexes.Count > 0){
int index = indexes[0];
//获取第一列的值
int id = Convert.ToInt32(this.listViewZone.Items[index].SubItems[0].Text);
//获取第二列的值
string name = this.listViewZone.Items[index].SubItems[1].Text;
}
}边栏推荐
- Event 对象,你很了解吗?
- mysql清除binlog日志文件
- 软件测试岗位巨坑?阿里在职7年测试人告诉你千万别上当
- Alibaba CTO Cheng Li: Alibaba Open Source History, Concept and Practice
- Failed to configure mysql, what's going on?
- Hongxing, donate another million
- 如何选择一块真正“好用的、性能高”的远程控制软件
- Geoffery Hinton:深度学习的下一个大事件
- 只问耕耘,不问收获,其实收获却在耕耘中
- How to encapsulate the wx.request() request of WeChat applet
猜你喜欢
随机推荐
LayaBox---TypeScript---模块
38岁女儿不恋爱没有稳定工作老母亲愁哭
LayaBox---TypeScript---Symbols
如何封装微信小程序的 wx.request() 请求
通过方法引用获取方法名
Excel dynamic chart production
How to choose a truly "easy-to-use, high-performance" remote control software
ssm网页访问数据库数据报错
Mysql事务隔离级别与MVCC(多版本并发控制)
10份重磅报告 — 展望中国数字经济未来
ansible模块--copy模块
MP的几种查询方式
The 38-year-old daughter is not in love and has no stable job, the old mother is crying
After 21 years of graduation, I switched to software testing. From 0 income to a monthly salary of over 10,000, I am really lucky...
365天挑战LeetCode1000题——Day 047 设计循环队列 循环队列
Oracle 单实例19.11升级到19.12
Kotlin的协程与生命周期
如何在技术上来保证LED显示屏质量?
C#/VB.NET 添加多行多列图片水印到Word文档
深度学习100例 —— 卷积神经网络(CNN)实现mnist手写数字识别









