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

Winform 自定义控件的右键菜单, 右键菜单ContextMenuStrip
using 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[0].Click += new EventHandler(SendMessage);
            Items[1].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("发送文件");
        }
    }
}

作者最新文章
Winform TextBox (文本框) 密码输入框, 输入内容显示*号 (星号)
Winform 启动前弹出登录界面, 登录成功之后再显示主界面
C# 字符串string和字符数组char[]的相互转换
C# 字符串倒序输出, 实现字符串倒序的方法
Winform 如何从 TextBox(文本框) 控件读取每一行数据到数组