当前位置:网站首页>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;
}
}边栏推荐
- QT笔记——在一个窗口上显示另外一个透明窗口
- Outsourced Student Management System Architecture Documentation
- LeetCode每日一练 —— 20. 有效的括号
- 开箱即用-使用异步加载布局来优化页面启动速度的几种方案
- SVN如何删除文件名包含空格的文件
- How to choose a truly "easy-to-use, high-performance" remote control software
- Event object, do you know it well?
- ssm web page access database data error
- 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...
- LeetCode每日一练 —— 225. 用队列实现栈
猜你喜欢

MySql模糊查询大全

Breaking the Boundary, Huawei's Storage Journey

详细总结SoC、DSP、MCU、GPU和FPGA等基础概念

突破边界,华为存储的破壁之旅

SQL 经典50题(题目+解答)(1)
![ASP.NET Core 6框架揭秘实例演示[31]:路由"高阶"用法](/img/57/821576ac28abc8d1c0d65df6a72fa3.png)
ASP.NET Core 6框架揭秘实例演示[31]:路由"高阶"用法

Challenge LeetCode1000 questions in 365 days - Day 047 Design Circular Queue Circular Queue

如何在技术上来保证LED显示屏质量?

ssm web page access database data error

多线程(基础) - 4万字总结
随机推荐
LeetCode每日一练 —— 20. 有效的括号
ssm web page access database data error
10份重磅报告 — 展望中国数字经济未来
太帅了!我用炫酷大屏展示爬虫数据!
org.apache.ibatis.binding.BindingException Invalidbound statement (not found)的解决方案和造成原因分析(超详细)
LayaBox---TypeScript---高级类型
ECCV22|PromptDet:无需手动标注,迈向开放词汇的目标检测
5G基础学习1、5G网络架构、网络接口及协议栈
Mysql事务隔离级别与MVCC(多版本并发控制)
LayaBox---TypeScript---三斜线指令
idea常用插件
LayaBox---TypeScript---Namespaces and modules
SQL 数据更新
WPF 截图控件之文字(七)「仿微信」
STM32+MPU6050设计便携式Mini桌面时钟(自动调整时间显示方向)
sva 断言资料
Hub and Spoke配置案例
21天学习挑战赛--第一天打卡(屏幕密度)
windbg分析进程卡死
Oracle超全SQL,细节狂魔