MouseLeave on a panel
Page 1 of 1
MouseLeave on a panel MouseLeave event triggers when i hover over a button on the panel
#1
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
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
#3
Re: MouseLeave on a panel
Posted 02 June 2009 - 03:19 AM
Thanks for your reply.
Here is the code:
Hopefully it makes what i was describing a bit more clear now
Gareth
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
#4
Re: MouseLeave on a panel
Posted 02 June 2009 - 06:14 AM
Gareth87, 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
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?
#6
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:

Now i move the mouse over a textbox that has been added to the panel (same happens on the button):

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
+ shows mouse position.
Here the mouse is over the panel and it highlights as expected:

Now i move the mouse over a textbox that has been added to the panel (same happens on the button):

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
#8
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.
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
#9
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
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
Page 1 of 1

Start a new topic
Add Reply





MultiQuote

| 


