1

我正在尝试触发 a 的鼠标右键单击事件Panel并调用一个函数,但它没有触发。这是我的代码:

private void viewscreen_MouseClick(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        generate_editpanel();
    }
}

“视屏”是一个Panel. 我的设计师代码是:

// viewscreen
// 
this.viewscreen.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.viewscreen.Location = new System.Drawing.Point(208, 16);
this.viewscreen.Name = "viewscreen";
this.viewscreen.Size = new System.Drawing.Size(370, 289);
this.viewscreen.TabIndex = 0;
this.viewscreen.MouseClick += 
    new System.Windows.Forms.MouseEventHandler(this.viewscreen_MouseClick);

谁能帮我?

4

3 回答 3

1

面板中的视频对象必须覆盖面板的整个区域(停靠)。在这种情况下,不会触发面板事件,因为所有点击都将发生在视频对象上。
但是如果您仍然希望触发面板事件,您可以像这样修改表单设计器代码:

this.pictureBox1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseClick);
this.pictureBox1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.viewscreen_MouseClick);  

在这里,我在面板(viewscreen)中停靠了一个 PictureBox(pictureBox1)。现在在表单代码中,您可以像这样进行测试:

private void viewscreen_MouseClick(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right) 
        {
            MessageBox.Show(this, "Right Clicked on Panel");
        }            
    }
 private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
    {
        MessageBox.Show(this, "Picture Clicked");
        //this.viewscreen_MouseClick(sender, e);
    }
于 2012-09-14T06:08:10.237 回答
0

在此处粘贴与 this.viewscreen 相关的完整代码 ....如果该面板上有任何其他控件,则这些控件将不允许鼠标单击面板。最顶层的控制事件将触发

于 2012-09-14T05:34:57.697 回答
0

代替 ...

private void viewscreen_MouseClick(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        generate_editpanel();
    }
}

使用这个...

private void viewscreen_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            generate_editpanel();
        }
    }

为我工作

于 2012-09-15T07:18:13.750 回答