找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2015|回复: 12

Winform 自定义控件的右键菜单, 右键菜单ContextMenuStrip

 火.. [复制链接]
  • 打卡等级:常驻代表
  • 打卡总天数:34
  • 打卡月天数:6
  • 打卡总奖励:9027
  • 最近打卡:2025-12-17 23:15:51

2823

主题

541

回帖

2万

积分

管理员

积分
22569
发表于 2021-9-12 16:22:32 | 显示全部楼层 |阅读模式
Winform 自定义控件的右键菜单, 右键菜单ContextMenuStrip
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;

  4. namespace WindowsFormsApp12
  5. {
  6.     public partial class Form1 : Form
  7.     {
  8.         private TextBox textBox1;
  9.         private CustomContextMenuStrip customContextMenuStrip1;
  10.         public Form1()
  11.         {
  12.             InitializeComponent();

  13.             this.customContextMenuStrip1 = new CustomContextMenuStrip();
  14.             this.textBox1 = new TextBox();

  15.             this.textBox1.ContextMenuStrip = this.customContextMenuStrip1;
  16.             this.textBox1.Location = new Point(200, 200);
  17.             this.textBox1.Text = "右键文本框";

  18.             this.Controls.Add(this.textBox1);
  19.         }
  20.     }

  21.     /// <summary>
  22.     /// 自定义控件的右键菜单
  23.     /// </summary>
  24.     public class CustomContextMenuStrip : ContextMenuStrip
  25.     {
  26.         /// <summary>
  27.         /// 构造函数
  28.         /// </summary>
  29.         public CustomContextMenuStrip()
  30.         {
  31.             // 添加菜单项
  32.             Items.Add("发送消息");
  33.             Items.Add("发送文件");

  34.             // 定义菜单项上的Click事件处理函数
  35.             Items[0].Click += new EventHandler(SendMessage);
  36.             Items[1].Click += new EventHandler(SendFile);
  37.         }

  38.         /// <summary>
  39.         /// 发送消息
  40.         /// </summary>
  41.         /// <param name="sender"></param>
  42.         /// <param name="e"></param>
  43.         private void SendMessage(object sender, EventArgs e)
  44.         {
  45.             MessageBox.Show("发送消息");
  46.         }

  47.         /// <summary>
  48.         /// 发送文件
  49.         /// </summary>
  50.         /// <param name="sender"></param>
  51.         /// <param name="e"></param>
  52.         private void SendFile(object sender, EventArgs e)
  53.         {
  54.             MessageBox.Show("发送文件");
  55.         }
  56.     }
  57. }
复制代码




工控课堂 www.gkket.com

0

主题

132

回帖

280

积分

注册会员

积分
280
发表于 2021-9-12 16:59:12 | 显示全部楼层
淡定,淡定,淡定……
工控课堂 www.gkket.com

0

主题

323

回帖

920

积分

中级会员

积分
920
发表于 2021-10-3 23:55:30 | 显示全部楼层
好东西一定要看看!
工控课堂 www.gkket.com

0

主题

110

回帖

328

积分

注册会员

积分
328
发表于 2025-11-14 21:06:11 | 显示全部楼层
赞同 + 10086,完全说出了我的想法!
工控课堂 www.gkket.com

0

主题

93

回帖

145

积分

新手上路

积分
145
发表于 2025-11-14 21:17:32 | 显示全部楼层
已转发给朋友,一起感受这份快乐~
工控课堂 www.gkket.com

0

主题

85

回帖

124

积分

新手上路

积分
124
发表于 2025-11-14 22:09:57 | 显示全部楼层
理性围观,感觉大家说得都有道理
工控课堂 www.gkket.com

0

主题

131

回帖

385

积分

注册会员

积分
385
发表于 2025-11-15 00:46:50 | 显示全部楼层
原来还有这种操作,长见识了!
工控课堂 www.gkket.com

0

主题

78

回帖

120

积分

新手上路

积分
120
发表于 2025-11-15 01:01:18 | 显示全部楼层
哈哈哈哈笑不活,楼主这脑洞绝了!
工控课堂 www.gkket.com

0

主题

84

回帖

124

积分

新手上路

积分
124
发表于 2025-11-15 01:03:04 | 显示全部楼层
同款经历!我当初也这么过来的😂
工控课堂 www.gkket.com

0

主题

53

回帖

75

积分

新手上路

积分
75
发表于 2025-11-15 01:06:24 | 显示全部楼层
赞同 + 10086,没毛病,完全没毛病
工控课堂 www.gkket.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

QQ|手机版|免责声明|本站介绍|工控课堂 ( 沪ICP备20008691号-1 )

GMT+8, 2025-12-22 16:09 , Processed in 0.417497 second(s), 27 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表