mousedown event does not trigger for dockpanel suite in C#

940 views Asked by At

I have a Form MainForm which inherits DockContent and even registered the mousedown and keypress event in the initialization of form. But none of these events get triggered and really don't know what could be the reason.

Below is the code :

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);
}

On the right or left click on form i want to show context menu with item "Copy". But mousedown event or even the keypress event does not trigger.

1

There are 1 answers

3
giammin On

The mouse events only work for the top control so they won't fire for the form controls when there is another control on top.

there you can find some workaround:

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

anyway I created a simple winform app with your code and everything works fine so you have definitely something that is swallowing all your events

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";
        }
    }
}