日期:2014-05-20 浏览次数:21152 次
public Form1() { InitializeComponent(); Label lbl = new Label(); lbl.Text = "123"; lbl.Name = "abc"; this.Controls.Add(lbl); lbl.ContextMenuStrip = this.contextMenuStrip1; lbl.MouseDown += new MouseEventHandler(p_MouseDown); } private void p_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right) { this.contextMenuStrip1.Items.Clear(); this.contextMenuStrip1.Items.Add(Control.MousePosition.ToString()); } } private void contextMenuStrip1_Opening(object sender, CancelEventArgs e) { MessageBox.Show((sender as ContextMenuStrip).SourceControl.Name); }
------解决方案--------------------
你右击的contextMenuStrip1控件有没有和那个控件绑定,控件的属性里面有个ContextMenuStrip,把它和ContextMenuStrip1控件绑定再试试,比如datagridview控件有个ContextMenuStrip属性,把它绑定到你的ContextMenuStrip1控件中