gkket 发表于 2021-9-12 16:22:32

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

Winform 自定义控件的右键菜单, 右键菜单ContextMenuStripusing System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApp12
{
    public partial class Form1 : Form
    {
      private TextBox textBox1;
      private CustomContextMenuStrip customContextMenuStrip1;
      public Form1()
      {
            InitializeComponent();

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

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

            this.Controls.Add(this.textBox1);
      }
    }

    /// <summary>
    /// 自定义控件的右键菜单
    /// </summary>
    public class CustomContextMenuStrip : ContextMenuStrip
    {
      /// <summary>
      /// 构造函数
      /// </summary>
      public CustomContextMenuStrip()
      {
            // 添加菜单项
            Items.Add("发送消息");
            Items.Add("发送文件");

            // 定义菜单项上的Click事件处理函数
            Items.Click += new EventHandler(SendMessage);
            Items.Click += new EventHandler(SendFile);
      }

      /// <summary>
      /// 发送消息
      /// </summary>
      /// <param name="sender"></param>
      /// <param name="e"></param>
      private void SendMessage(object sender, EventArgs e)
      {
            MessageBox.Show("发送消息");
      }

      /// <summary>
      /// 发送文件
      /// </summary>
      /// <param name="sender"></param>
      /// <param name="e"></param>
      private void SendFile(object sender, EventArgs e)
      {
            MessageBox.Show("发送文件");
      }
    }
}




不要说话 发表于 2021-9-12 16:59:12

淡定,淡定,淡定……

yyl1203 发表于 2021-10-3 23:55:30

好东西一定要看看!

李冬123 发表于 2025-11-14 21:06:11

赞同 + 10086,完全说出了我的想法!

Piano、 发表于 2025-11-14 21:17:32

已转发给朋友,一起感受这份快乐~

chenxinian 发表于 2025-11-14 22:09:57

理性围观,感觉大家说得都有道理

wx_JXB61X2S 发表于 2025-11-15 00:46:50

原来还有这种操作,长见识了!

@me€巧_j8tDx 发表于 2025-11-15 01:01:18

哈哈哈哈笑不活,楼主这脑洞绝了!

MagnoliaZ 发表于 2025-11-15 01:03:04

同款经历!我当初也这么过来的😂

离魅魍魉 发表于 2025-11-15 01:06:24

赞同 + 10086,没毛病,完全没毛病
页: [1] 2
查看完整版本: Winform 自定义控件的右键菜单, 右键菜单ContextMenuStrip