• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2251
  • Last Modified:

how to detect a click outside of my control

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
0
mahmuod
Asked:
mahmuod
  • 2
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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

0
 
mahmuodAuthor 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
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Ah..that is more complicated...

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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now