mousedown事件不会触发C#中的dockpanel套件

时间:2014-11-24 08:38:55

标签: c# .net mouseevent mousedown dockpanel-suite

我有一个Form MainForm ,它继承了DockContent,甚至在表单初始化中注册了mousedown和keypress事件。 但是没有一个事件被触发,真的不知道可能是什么原因。

以下是代码:

using WeifenLuo.WinFormsUI.Docking;
public partial class MainForm : DockContent
{

     InitializeComponent();         
}

 private void InitializeComponent()
 {    
    this.Load += new System.EventHandler(this.MainForm_Load);
    this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.MainForm_KeyPress);
    this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.MainForm_KeyUp);
    this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MainForm_MouseDown);
 }


}

private void MainForm_MouseDown(object sender, MouseEventArgs e)
{
    Copy.Show(Cursor.Position);
}

在右侧或左侧单击表单我想显示带有“复制”项的上下文菜单。但是mousedown事件甚至是按键事件都没有触发。

1 个答案:

答案 0 :(得分:0)

鼠标事件仅适用于顶级控件,因此当顶部有另一个控件时,它们不会触发表单控件。

你可以找到一些解决方法:

How do I grab events from sub-controls on a user-control in a WinForms App?

无论如何我用你的代码创建了一个简单的winform应用程序,一切正常,所以你肯定会吞下你所有的活动

using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace WindowsFormsApplication2
{
    public partial class Form1 : DockContent
    {
        public Form1()
        {
            InitializeComponent();
            label1.Text = "init";
            KeyPress += MainForm_KeyPress;
            KeyUp += MainForm_KeyUp;
            MouseDown += MainForm_MouseDown;
        }
        private void MainForm_MouseDown(object sender, MouseEventArgs e)
        {
            label1.Text = "MainForm_MouseDown";
        }
        private void MainForm_KeyUp(object sender, KeyEventArgs e)
        {
            label1.Text = "MainForm_KeyUp";
        }
        private void MainForm_KeyPress(object sender, KeyPressEventArgs e)
        {
            label1.Text = "MainForm_KeyUp";
        }
    }
}