發布時間:2017-08-29 17:53 作者:獨孤劍 閱讀:1464
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("發送文件"); } } }
微信打賞, 微信掃一掃
支付寶打賞, 支付寶掃一掃
如果文章對您有幫助,歡迎給作者打賞