I use Mouse events MouseEnter and MouseLeave with a pictureBox. The Back Color changes with Mouse Enter but do not change in normal with mouse Leave Event.
public void pictureBox1_MouseEnter(object sender, EventArgs e)
{
pictureBox1.BackColor = Color.Blue;
}
public void pictureBox1_MouseLeave(object sender, EventArgs e)
{
pictureBox1.BackColor = SystemColors.Control;
}
Maybe you should remember what the previous color was?
Color prevColor = Color.Black;
public void pictureBox1_MouseEnter(object sender, EventArgs e)
{
prevColor = pictureBox1.BackColor;
pictureBox1.BackColor = Color.Blue;
}
public void pictureBox1_MouseLeave(object sender, EventArgs e)
{
pictureBox1.BackColor = prevColor;
}
you have to know what;s the previous color and change it accordingly. also make sure the control it registered the both the events:
for example, if the color was gray before then:
public void pictureBox1_MouseEnter(object sender, EventArgs e)
{
pictureBox1.BackColor = Color.Blue;
}
public void pictureBox1_MouseLeave(object sender, EventArgs e)
{
pictureBox1.BackColor = Color.Gray;
}
It seems correct.
You should put a breaking point in the mouseleave event.
Maybe the pictureBox1_MouseLeave event is not set correctly.
Related
I'm trying to make draggable window form, but when I tried to detect mouse down and up event to form, but event does not fire.
Other things seems working but those 2 events are not.
public form()
{
InitializeComponent();
this.BackColor = Color.Fuchsia;
this.TransparencyKey = this.BackColor;
Debug.Write("initialized");
}
private void form_MouseDown(object sender, MouseEventArgs e)
{
Debug.Write("dragging..");
}
private void form_MouseUp(object sender, MouseEventArgs e)
{
Debug.Write("drag done");
}
it is better to add a panel to your form and set Dock to top in order to cover the title bar area. then use this sample code
private bool isDragging = false;
private Point lastLocation;
private void panel_MouseDown(object sender, MouseEventArgs e)
{
isDragging = true;
lastLocation = e.Location;
}
private void panel_MouseMove(object sender, MouseEventArgs e)
{
if (isDragging)
{
this.Location = new Point(
(this.Location.X - lastLocation.X) + e.X,
(this.Location.Y - lastLocation.Y) + e.Y);
this.Update();
}
}
private void panel_MouseUp(object sender, MouseEventArgs e)
{
isDragging = false;
}
don't forget to add handlers in the form constructor
panel.MouseDown += panel_MouseDown;
panel.MouseMove += panel_MouseMove;
panel.MouseUp += panel_MouseUp;
In your constructor for form, register those methods your using to listen for the mouse down events like this.
public form() {
InitializeComponent();
this.BackColor = Color.Fuchsia;
this.TransparencyKey = this.BackColor;
Debug.Write("initialized");
//register the methods to listen for the events
this.MouseDown += form_MouseDown;
this.MouseUp += form_MouseUp;
}
private void form_MouseDown(object sender,
MouseEventArgs e) {
Debug.Write("dragging..");
}
private void form_MouseUp(object sender,
MouseEventArgs e) {
Debug.Write("drag done");
}
Let's say I have a panel and inside this panel I have a label. What I want to do is to fire panel's MouseEnter but when I do MouseEnter on label control. Any idea?
When MouseEnter event of your panel is fired all handlers that are subscribed to this event will be called. So, instead of firing panel's event you can call those handlers. See following demo code.
Color backColor;
private void Panel1_MouseEnter(object sender, EventArgs e)
{
backColor = panel1.BackColor;
panel1.BackColor = Color.Red;
}
private void Panel1_MouseLeave(object sender, EventArgs e)
{
panel1.BackColor = backColor;
}
private void Label1_MouseEnter(object sender, EventArgs e)
{
Panel1_MouseEnter(this, EventArgs.Empty);
}
private void Label1_MouseLeave(object sender, EventArgs e)
{
Panel1_MouseLeave(this, EventArgs.Empty);
}
Try that
private void label_MouseEnter(object sender, EventArgs e)
{
panel.RaiseEvent(new MouseEventArgs(Mouse.PrimaryDevice, 0) { RoutedEvent = Mouse.MouseEnterEvent });
}
I am trying to make the text of the button change colour when a checkbox is checked, but for some reason, I just don't know how. Would I need to write an If statement, if so how do I do that?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ColourCheckBox.ForeColor = Color.Red;
}
private void ColourCheckBox_CheckedChanged(object sender, EventArgs e)
{
ColourCheckBox.ForeColor = Color.Black;
}
}
Your question is so obscure, but based on the things that I understand, you should check the Checked property.
private void ColourCheckBox_CheckedChanged(object sender, EventArgs e)
{
if (ColourCheckBox.Checked)
{
ColourCheckBox.ForeColor = Color.Black;
}
else
{
ColourCheckBox.ForeColor = Color.Red;
}
}
In the CheckedChanged event, you can use the Checked property:
ColourCheckBox.ForeColor = ColourCheckBox.Checked ? Color.Black : Color.Red;
In case of a Triple state checkbox, with 3 color you can switch on CheckState value :
Unchecked = 0
Checked = 1
Indeterminate = 2
using System.Drawing;
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
button1.ForeColor = Color.Red;
}
I have RadioButton which has a background. Now I need to change background on MouseEnter event. I can do that with
private void button_MouseEnter(object sender, EventArgs e)
{
button.BackgroundImage = Image.FromFile("D:/img/sample.png");
}
But I already have that image as resource in project and I don't know how to get to it.
try as follow:-
private void button_MouseEnter(object sender, EventArgs e)
{
button.BackgroundImage = <YourNameSpace>.Properties.Resources.<ResourceName>;
}
In case you use winforms, include the images to your resource:
public Form1()
{
InitializeComponent();
button1.MouseEnter += new EventHandler(button1_MouseEnter);
button1.MouseLeave += new EventHandler(button1_MouseLeave);
}
void button1_MouseLeave(object sender, EventArgs e)
{
this.button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.img1));
}
void button1_MouseEnter(object sender, EventArgs e)
{
this.button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.img2));
}
Please note I'm trying to do the following, when I hover the mouse on a button I want the panel to be visible, when the mouse leaves the button or panel, the panel should not be visible. Below you can see the code I have, but the panel it's not staying visible.
private void FormMain()
{
buttonMenu.MouseEnter += new EventHandler(buttonMenu_MouseEnter); //open panel
buttonMenu.MouseLeave += new EventHandler(buttonMenu_MouseLeave);
panelMenu.MouseEnter += new EventHandler(panelMenu_MouseEnter);
panelMenu.MouseLeave += new EventHandler(panelMenu_MouseLeave);
mbB1.MouseEnter += new EventHandler(mbB1_MouseEnter);//button in panel
mbB2.MouseEnter += new EventHandler(mbB2_MouseEnter);//button in panel
}
private void buttonMenu_MouseEnter(object sender, EventArgs e)
{
this.panelMenu.Visible = true;
}
private void buttonMenu_MouseLeave(object sender, EventArgs e)
{
this.panelMenu.Visible = false;
}
private void panelMenu_MouseEnter(object sender, EventArgs e)
{
this.panelMenu.Visible = true;
}
private void panelMenu_MouseLeave(object sender, EventArgs e)
{
this.panelMenu.Visible = false;
}
private void mbB1_MouseEnter(object sender, EventArgs e)
{
this.panelMenu.Visible = true;
}
private void mbB2_MouseEnter(object sender, EventArgs e)
{
this.panelMenu.Visible = true;
}
One solution that came to my mind is using a short timer. You may further optimize it to cut down on LoC, but it works. You may also lower the timer's delay, but 100ms is what I think would be a safe one.
On a side note, I don't think this is a good design. If you want this kind of behaviour, you should either use a ContextMenuStrip or a Click event on the button, and a hide event on panelMenu.MouseLeave. Still, if it's what you really need, this is how I solved it:
public partial class Form1 : Form
{
private bool mouseInPanel;
private Timer hideTimer;
public Form1()
{
InitializeComponent();
buttonMenu.MouseEnter += new EventHandler(button_MouseEnter);
buttonMenu.MouseLeave += new EventHandler(button_MouseLeave);
mbB1.MouseEnter += panelButton_MouseEnter;
mbB2.MouseEnter += panelButton_MouseEnter;
panelMenu.MouseEnter += new EventHandler(panelMenu_MouseEnter);
panelMenu.MouseLeave += new EventHandler(panelMenu_MouseLeave);
hideTimer = new Timer {Interval = 100};
hideTimer.Tick += hidePanel;
}
private void button_MouseEnter(object sender, EventArgs e)
{
this.panelMenu.Visible = true;
}
private void button_MouseLeave(object sender, EventArgs e)
{
hideTimer.Start();
}
private void panelMenu_MouseEnter(object sender, EventArgs e)
{
mouseInPanel = true;
this.panelMenu.Visible = true;
}
private void panelMenu_MouseLeave(object sender, EventArgs e)
{
mouseInPanel = false;
hideTimer.Start();
}
private void panelButton_MouseEnter(object sender, EventArgs e)
{
mouseInPanel = true;
this.panelMenu.Visible = true;
}
private void hidePanel(object sender, EventArgs e)
{
hideTimer.Stop();
if (!mouseInPanel) this.panelMenu.Visible = false;
}
}
What I figured is your mouse actions needed a little bit of a delay, else the panel (along with your buttons mbB1 and mbB2) gets hidden before those buttons' actions could be triggered.
This is because by entering the panel buttons you leave the panel, and it disappears (along with it's capability to receive mouse actions) right before mbB1/mbB2's action can trigger.