School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
You're Browsing As A Guest! Register Now...
Become an Expert!

Join 358,291 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,472 people online right now.Registration is fast and FREE... Join Now!



MouseLeave on a panel

52 Weeks of Code Challenge: Android

Week #11 of the 52 Weeks of Code Challenge is Android, you should give it a shot. Click Here!

MouseLeave on a panel MouseLeave event triggers when i hover over a button on the panel Rate Topic: -----

#1 Gareth87  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 02-June 09


Dream Kudos: 0

Posted 02 June 2009 - 02:16 AM

Hi All,

I have a panel that i add to my main form using code. I then assign it a MouseEnter and MouseLeave event handler to change the background color, make things visible etc.

I then add to the panel a few buttons and a text box. Whenever i hover over one of these the MouseLeave event triggers and they disappear again.

I know it's because the mouse is leaving the visible part of the panel, but how do i stop this from happening?

Thanks for any help,
Gareth
Was This Post Helpful? 0
  • +
  • -


#2 noorahmad  Icon User is offline

  • Author
  • Icon

Reputation: 162
  • View blog
  • Posts: 2,198
  • Joined: 12-March 09


Dream Kudos: 1600

Re: MouseLeave on a panel

Posted 02 June 2009 - 02:29 AM

:code:
Was This Post Helpful? 0
  • +
  • -

#3 Gareth87  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 02-June 09


Dream Kudos: 0

Re: MouseLeave on a panel

Posted 02 June 2009 - 03:19 AM

Thanks for your reply.

Here is the code:
//Add the panel with a single button
void BtnAddClick(object sender, System.EventArgs e)
{
	int index = panels.Count;
	panels.Add(new Panel());
	((Panel)panels[index]).Size = new System.Drawing.Size(585, 25);
	((Panel)panels[index]).Margin = new Padding(0);
	((Panel)panels[index]).MouseEnter += new EventHandler(mouseEnterPanel);
	((Panel)panels[index]).MouseLeave += new EventHandler(mouseLeavePanel);
	flowLayoutPanel1.Controls.Add((Panel)panels[index]);
			
	Button b = new Button();
	((Panel)panels[index]).Controls.Add(b);
}
		
void mouseEnterPanel(object sender, System.EventArgs e)
{
	((Control)sender).BackColor = System.Drawing.Color.LightBlue;
}
		
void mouseLeavePanel(object sender, System.EventArgs e)
{
	((Control)sender).BackColor = System.Drawing.SystemColors.Control;
}


Hopefully it makes what i was describing a bit more clear now

Gareth
Was This Post Helpful? 0
  • +
  • -

#4 SixOfEleven  Icon User is online

  • 1-888-611-RULZ
  • Icon

Reputation: 270
  • View blog
  • Posts: 4,365
  • Joined: 18-October 08


Dream Kudos: 1100

Expert In: C, C#, XNA, Game Programming, Chocolate

Re: MouseLeave on a panel

Posted 02 June 2009 - 06:14 AM

View PostGareth87, on 2 Jun, 2009 - 04:16 AM, said:

Hi All,

I have a panel that i add to my main form using code. I then assign it a MouseEnter and MouseLeave event handler to change the background color, make things visible etc.

I then add to the panel a few buttons and a text box. Whenever i hover over one of these the MouseLeave event triggers and they disappear again.

I know it's because the mouse is leaving the visible part of the panel, but how do i stop this from happening?

Thanks for any help,
Gareth


Just so I'm clear, you want all controls on the panel to highlight when your mouse is in the new panel?
Was This Post Helpful? 0
  • +
  • -

#5 Gareth87  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 02-June 09


Dream Kudos: 0

Re: MouseLeave on a panel

Posted 02 June 2009 - 06:27 AM

Not quite,

I want the panel to stay highlighted when i mouse over another control within the panel.

The panel should only return to its normal color when the mouse leaves the bounds of the panel

Thanks for any help,
Gareth
Was This Post Helpful? 0
  • +
  • -

#6 Gareth87  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 02-June 09


Dream Kudos: 0

Re: MouseLeave on a panel

Posted 03 June 2009 - 03:16 AM

Hopefully these images will show what i mean a bit better.
+ shows mouse position.

Here the mouse is over the panel and it highlights as expected:
Posted Image

Now i move the mouse over a textbox that has been added to the panel (same happens on the button):
Posted Image
As you can see, the panel loses its highlight because the MouseLeave event occurred.

How can i keep the panel highlighted when i mouse over a control that has been placed on it?

Thanks,
Gareth
Was This Post Helpful? 0
  • +
  • -

#7 noorahmad  Icon User is offline

  • Author
  • Icon

Reputation: 162
  • View blog
  • Posts: 2,198
  • Joined: 12-March 09


Dream Kudos: 1600

Re: MouseLeave on a panel

Posted 03 June 2009 - 03:31 AM

simply Select your all controls in panel and goto properties and and events and select MouseOver and type your code and do it for MouseLeave

This post has been edited by noorahmad: 03 June 2009 - 03:32 AM

Was This Post Helpful? 0
  • +
  • -

#8 SixOfEleven  Icon User is online

  • 1-888-611-RULZ
  • Icon

Reputation: 270
  • View blog
  • Posts: 4,365
  • Joined: 18-October 08


Dream Kudos: 1100

Expert In: C, C#, XNA, Game Programming, Chocolate

Re: MouseLeave on a panel

Posted 03 June 2009 - 09:25 AM

Since you are trying to do this dynamically that complicates things a little. But, there is a way to do it. You can set the parent container of the controls to the panel. Then, when you create the new control you hook them all to the same event handler. In that event handler you can use the parent property of the control to set the parent's back ground color. Here is a simple program I wrote. I just dragged a panel onto the form and added the two controls dynamically a button and a text box. It behaves like the images you added.

Happy programming!

*edit*
You can also modify the handler so that the controls don't highlight when the mouse is over them if you don't want that effect.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.panel1.MouseEnter += new EventHandler(panelMouseOver);
            this.panel1.MouseLeave += new EventHandler(panelMouseLeave);
            Button btn = new Button();
            btn.Parent = panel1;
            btn.Text = "OK";
            btn.Location = new Point(10, 10);
            btn.MouseEnter += new EventHandler(parentMouseOver);
            btn.MouseLeave += new EventHandler(parentMouseLeave);
            panel1.Controls.Add(btn);
            TextBox tb = new TextBox();
            tb.Text = "Put your text here.";
            tb.Location = new Point(10, 80);
            tb.Parent = panel1;
            tb.MouseEnter += new EventHandler(parentMouseOver);
            tb.MouseLeave += new EventHandler(parentMouseLeave);
            panel1.Controls.Add(tb);
        }

        void parentMouseLeave(object sender, EventArgs e)
        {
            ((Control)sender).Parent.BackColor = System.Drawing.SystemColors.Control;
        }

        void parentMouseOver(object sender, EventArgs e)
        {
            ((Control)sender).Parent.BackColor = System.Drawing.Color.LightBlue;
        }

        void panelMouseOver(object sender, EventArgs e)
        {
            ((Control)sender).BackColor = System.Drawing.Color.LightBlue;
        }

        void panelMouseLeave(object sender, EventArgs e)
        {
            ((Control)sender).BackColor = System.Drawing.SystemColors.Control;
        }
    }
}


This post has been edited by SixOfEleven: 03 June 2009 - 09:28 AM

Was This Post Helpful? 0
  • +
  • -

#9 Gareth87  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 02-June 09


Dream Kudos: 0

Re: MouseLeave on a panel

Posted 18 June 2009 - 06:17 AM

Hi SixOfEleven, thanks for your help.

It works perfectly on the button, but not quite right on the textbox.

When i mouse over the textbox's "border", the panel does not highlight. This means there is a 1px gap around the textbox in which the panel returns to its default color.

Can anyone help any further with this?

Thanks, Gareth
Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month