We help IT Professionals succeed at work.

how to detect a click outside of my control

mahmuod
mahmuod asked
on
Well I need some help detecting mouse clicks outside or key press  on the form or any contrl in the form  
i am design my control he is  content some textbox and label fro sersh  of emp and returen emp name and emp id

for example
the DateTimePicker or combobox this control when click show item or date if press in any control the drop down list hide
i want seam thate for mycontrol

formmy control
Comment
Watch Question

Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
Here's one way to implement it via IMessageFilter():
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class UserControl1 : UserControl
    {

        private static MyFilter mf =  null;

        public UserControl1()
        {
            InitializeComponent();

            if (mf == null)
            {
                mf = new MyFilter();
                Application.AddMessageFilter(mf);
            }

            mf.MouseDown += new MyFilter.LeftButtonDown(mf_MouseDown);
            mf.KeyUp += new MyFilter.KeyPressUp(mf_KeyUp);
        }

        private void mf_KeyUp(IntPtr target)
        {
            if (!target.Equals(IntPtr.Zero))
            {
                if (PanelVisible)
                {
                    if (!IsTargetMine(target))
                    {
                        TogglePanel();
                    }
                }
            }
        }

        private bool IsTargetMine(IntPtr target)
        {
            return IsTargetMine(this, target);
        }

        private bool IsTargetMine(Control ctl, IntPtr target)
        {
            foreach (Control child in ctl.Controls)
            {
                if (child.Handle.Equals(target))
                {
                    return true;
                }
                else if (child.HasChildren)
                {
                    bool result = IsTargetMine(child, target);
                    if (result)
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        private bool PanelVisible
        {
            get { return panel1.Visible; }
        }

        private void mf_MouseDown()
        {
            if (PanelVisible)
            {
                if (!this.RectangleToScreen(this.ClientRectangle).Contains(Cursor.Position))
                {
                    TogglePanel();
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            TogglePanel();
        }

        private void TogglePanel()
        {
            if (PanelVisible)
            {
                panel1.Visible = false;
                this.Size = new Size(this.Size.Width, 50); // collasped height
            }
            else
            {
                this.Size = new Size(this.Size.Width, 350); // expanded height
                panel1.Visible = true;
            }
        }

        private class MyFilter : IMessageFilter
        {

            public delegate void LeftButtonDown();
            public event LeftButtonDown MouseDown;

            public delegate void KeyPressUp(IntPtr target);
            public event KeyPressUp KeyUp;

            private const int WM_KEYUP = 0x101;
            private const int WM_LBUTTONDOWN = 0x201;

            bool IMessageFilter.PreFilterMessage(ref Message m)
            {
                switch (m.Msg)
                {
                    // raise our KeyUp() event whenever a key is released in our app
                    case WM_KEYUP:
                        if (KeyUp != null)
                        {
                            KeyUp(m.HWnd);
                        }
                        break;

                    // raise our MouseDown() event whenever the mouse is left clicked somewhere in our app
                    case WM_LBUTTONDOWN:
                        if (MouseDown != null)
                        {
                            MouseDown(); 
                        }
                        break;
                }
                return false;
            }

        }

    }
}

Open in new window

Author

Commented:
thank you
is working
but i have problem the user control some part hidden in the form i want same DateTimePicker drop down list
image
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009
Commented:
Ah..that is more complicated...

You might want to start with something like this:
http://lukesw.net/articles/SimplePopup.aspx