leinadw2
leinadw2

Reputation: 65

C# Mouse Hover Event for Background Panel

I am making a Windows Form and I am having trouble getting a Mouse Hover Event to work the way I want to. I have 50 little panels grouped together inside 1 big panel. I want to make info popup whenever the user hovers over a little panel.

Rather than making a mouse hover event for each little panel, I am trying to make just one mouse hover event for the big panel. Whenever the user hovers their mouse in anywhere inside the big panel, my code will determine whether or not they are hovering inside a little panel and if they are it will identify the little panel and display the pertinent info.

However, I am finding that the big panel mouse hover event does not trigger if I hover over a little panel since it is technically in the little panel boundaries and outside of the big panel boundaries (sort of like how Lesotho is it's own country even though it is inside South Africa's borders).

Is there some sort of way around this?

enter image description here

Upvotes: 3

Views: 7698

Answers (4)

Xtremexploit
Xtremexploit

Reputation: 329

If you want to make the top control to be transparent to Mouse Events you can use this code:

protected override void WndProc(ref Message m)
{
    const int WM_NCHITTEST = 0x0084;
    const int HTTRANSPARENT = -1;

    switch(m.Msg)
    {
        case WM_NCHITTEST:
            m.Result = (IntPtr)HTTRANSPARENT;
            break;
        default:
            base.WndProc(ref m);
    }
}

By example, you have an image and there is a button behind it that can pass the mouse over and the button will execute the MouseHover event.

Upvotes: 0

Lemonseed
Lemonseed

Reputation: 1732

Cleanest solution would be to implement your own LittlePanel control:

public class LittlePanel : Panel
{
    public class HoverEventArgs
    {
        public bool Active { get; set; }

        public HoverEventArgs(bool active)
        {
            Active = active;
        }
    }

    public event EventHandler<HoverEventArgs> Hover;

    protected override void OnMouseEnter(EventArgs e)
    {
        base.OnMouseEnter(e);
        OnHover(true);
    }

    protected override void OnMouseLeave(EventArgs e)
    {
        base.OnMouseLeave(e);
        OnHover(false);
    }

    protected void OnHover(bool active)
    {
        EventHandler<HoverEventArgs> hover = Hover;
        if (hover != null) hover(this, new HoverEventArgs(active));
    }
}

Then subscribe to the Hover events raised by all LittlePanels within the "big" Panel:

foreach (Control control in bigPanel.Controls)
{
    if (control is LittlePanel)
    {
        ((LittlePanel)control).Hover += littlePanel_Hover;
    }
}

Using a shared event handler:

void littlePanel_Hover(object sender, LittlePanel.HoverEventArgs e)
{
    // Your code goes here...
}

You might also consider adding shared functionality to the LittlePanel control, such as contained labels, etc.

Upvotes: 2

Tanmay Nehete
Tanmay Nehete

Reputation: 2196

try this on your single event e.g hope this will help you

private void Panaal_MouseMove(object sender, MouseEventArgs e)
{
   Control ctr = sender as Control;
   if (ctr.Name.Equals("Panel Name"))
    {
     //your POP Up code
    }
}

Upvotes: 1

leinadw2
leinadw2

Reputation: 65

I ended up going with what S. Brentson suggested. I made my own custom event handler called GridMouseHover in my Form.cs file.

    private void GridMouseHover(object sender, EventArgs e)
    {

    }

I then linked the MouseHover event of each of my controls to my new GridMouseHover event during my Form_Load event so that they will all be linked before a MouseHover event could possibly trigger.

    private void Form4_Load(object sender, EventArgs e)
    {
        this.panel1.MouseHover += GridMouseHover;
        this.panel2.MouseHover += GridMouseHover;
        this.panel3.MouseHover += GridMouseHover;
        //etc...
    }

I had already created all of my panels by hand in my Form.cs[Design] window which is why I did not try TaW's suggestion, but I will definitely try that next time since making everything by hand was very tedious. Thanks for the help!

Upvotes: 1

Related Questions