I am fairly new to C# and I am trying to change the color of a menustrip item when the mouse 'leaves' the button after its selected. But I can't use the MouseLeave as a boolean as it is an event. It tells 'MouseLeave can only appear on the left hand side of +='. Any tips?
Here is what I tried to do:
if (e.Item.MouseLeave)
{
using (Brush b = new SolidBrush(Color.FromArgb(80, 80, 80)))
{
e.Graphics.FillRectangle(b, e.Graphics.ClipBounds);
}
}
You can have a global boolean:
private void frm_Load(object sender, EventArgs e)
{
ctrl.MouseEnter += ctrl_MouseEnter;
ctrl.MouseLeave += ctrl_MouseLeave;
}
private bool bMouseInside = false;
private void ctrl_MouseEnter(object sender, EventArgs e)
{
bMouseInside = true;
}
private void ctrl_MouseLeave(object sender, EventArgs e)
{
bMouseInside = false;
}
To use in your code:
if (!bMouseInside)
{
// using (Brush b etc..
}
As MouseLeave is an event on a control, try something like this:
On creating the control, add a method to the event handler:
control.MouseLeave += control_MouseLeft;
Then define the action that you want to occur in your control_MouseLeft method.
void control_MouseLeft(objet sender, EventArgs e)
{
using (Brush b = new SolidBrush(Color.FromArgb(80, 80, 80)))
{
e.Graphics.FillRectangle(b, e.Graphics.ClipBounds);
}
}
Related
private void Form1_Load(object sender, EventArgs e)
{
if (MouseMove == button1;)
{
button1.Size= 100;70;
}
}
}
}
I couldn't find how to write code
Use the MouseEnter event to capture the mouse cursor hovering over the button borders,
and the MouseLeave event to detect when the cursor leaves the button borders, in order to return it to its original size.
Here is one way to implement such functionality:
private Size OriginalButtonSize;
private Size HoveredSize;
private void Form1_Load(object sender, EventArgs e)
{
OriginalButtonSize = this.button1.Size;
HoveredSize = new Size(OriginalButtonSize.Width + 30, OriginalButtonSize.Height + 30);
}
private void button1_MouseEnter(object sender, EventArgs e)
{
button1.Size = HoveredSize;
}
private void button1_MouseLeave(object sender, EventArgs e)
{
button1.Size = OriginalButtonSize;
}
Output:
You can use this code:
private void button1_MouseHover(object sender, EventArgs e)
{
button1.Width = 100;
button1.Height = 70;
}
if you want to set size to the previous size after leaving the mouse, you can use this (for example it was 60 and 30 at first place):
private void button1_MouseLeave(object sender, EventArgs e)
{
button1.Width = 60;
button1.Height = 30;
}
So I have a matrix of panels (maybe will change for Picture Boxes in the future), and what i want is that every time i press one of the panels after pressing the button on the toolbox it will change it's background to a certain picture.
Right now what i have is:
private void EtapaInicial_Click(object sender, EventArgs e)
{
EtapaInicialWasClicked = true;
}
private void panel_Click(object sender, EventArgs e)
{
if (EtapaInicialWasClicked)
{
panel1.BackgroundImage = Symbols.EtapaInicialbm;
EtapaInicialWasClicked = false;
}
}
What I would like to change was the panel1 to make it work for every panel (otherwise it will only change panel1 independently of the panel i've clicked), is that possible?
Try the following
private void EtapaInicial_Click(object sender, EventArgs e)
=> EtapaInicialWasClicked = true;
private void panel_Click(object sender, EventArgs e)
{
if (EtapaInicialWasClicked)
{
(sender as Panel).BackgroundImage = Symbols.EtapaInicialbm;
EtapaInicialWasClicked = false;
}
}
Yes it is. You have to loop through each panel
and assign the same event handler but you have to make some changes in the event handler itself
foreach(var p in allPanels)
{
p.Click += panel_Click;
}
Then change your event handler like this
private void panel_Click(object sender, EventArgs e)
{
var p = (Panel)sender;
if (EtapaInicialWasClicked)
{
p .BackgroundImage = Symbols.EtapaInicialbm;
EtapaInicialWasClicked = false;
}
}
Remember the sender argument contains reference to the actual control that initiated the event but you have to cast it first in order to use it.
However if you want to store more data for the event you've just handled you can use the panel.Tag property. This can be used to store EtapaInicialWasClicked for example
Imagine that i have 10 buttons and i want to use mouse enter event to increase buttons width and height by 30. if i want to do this i have to use mouse event for each of them one by one,
how can i write my code only once for all buttons.i'm trying to use foreach loop but not sure if i should use form load and don't know how to handle event. (windows form)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace main
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
foreach (Control item in Controls)
{
if (item is Button)
{
// code
}
}
}
}
}
First of all shorten your code by getting only the buttons on the form. Then add an eventhandler for the MouseEnter event:
foreach (Button btn in Controls.OfType<Button>())
{
btn.MouseEnter += new System.EventHandler(btn_MouseEnter);
}
Then in the event change the width:
private void btn_MouseEnter(object sender, System.EventArgs e)
{
var senderButton = (Button)sender;
senderButton.Width += 30;
}
You'all also have to add an event handler + event for MouseLeave to decrease the width again. Otherwise the buttons will keep on growing:
foreach (Button btn in Controls.OfType<Button>())
{
btn.MouseEnter += new System.EventHandler(btn_MouseEnter);
btn.MouseLeave += new System.EventHandler(btn_MouseLeave );
}
private void btn_MouseEnter(object sender, System.EventArgs e)
{
var senderButton = (Button)sender;
senderButton.Width += 30;
}
private void btn_MouseLeave (object sender, System.EventArgs e)
{
var senderButton = (Button)sender;
senderButton.Width -= 30;
}
The foreach-loop code can be added after InitializeComponent() or in the Form_Load event.
private void Form1_Load(object sender, System.EventArgs e)
{
foreach (Button btn in Controls.OfType<Button>())
{
btn.MouseEnter += new System.EventHandler(btn_MouseEnter);
btn.MouseLeave += new System.EventHandler(btn_MouseLeave );
}
}
You can easily loop through your all buttons using OfType extension method like this:
foreach(var button in this.Controls.OfType<Button>())
{
button.MouseEnter += button_MouseEnter;
}
Use functions to wrap your logic.
Write your code that change the weight and width in method, an subscribe to MouseMove or whatever event that suits you.
Then use GetChildAtPoint to locate the button, so you can change its size.
// This goes in the
foreach(var button in this.Controls.OfType<Button>())
{
button.Mouse += button_IncreaseSize;
}
protected override void button_IncreaseSize(MouseEventArgs e)
{
// use GetChildAtPoint to get the control
var button = GetChildAsPoint(new Point(e.X, e.Y));
// Change the size of button, eg. with Scal, and with the Size property, or by chaning Height and Width propertis manually
button.Scale(new SizeF(30, 30));
}
Alternatively you can also do this:
private void Form1_Load(object sender, EventArgs e)
{
foreach (Control item in this.Controls)
{
if (item is Button)
{
//item.MouseEnter += (s, ea) => { YourMethodName(); };
item.MouseEnter += new System.EventHandler(btn_MouseEnter);
}
}
}
private void btn_MouseEnter(object sender, System.EventArgs e)
{
//your code to increase width and height
var btnSenderButton = sender as Button;
btnSenderButton.Width += 30;
}
I am creating a "cropping tool", and i need to make a panel that contains 2 buttons draggable.
Until now i've tried something like this, but the change location event happens only when i click the right button of the mouse...
this.MouseDown += new MouseEventHandler(onRightClickMouse);
private void onRightClickMouse(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
Point localMouseClickPoint = new Point(e.X, e.Y);
panel1.Location = localMouseClickPoint;
}
}
My question: How can i make that panel draggable in my form?(I mean click on the panel then drag it to a location).
Try something like this:
delegate void updatePanelCallback();
panel1.MouseDown += new MouseEventHandler(onMouseDown);
panel1.MouseUp += new MouseEventHandler(onMouseUp);
System.Timers.Timer runTimer = new System.Timers.Timer(100);
runTimer.Elapsed += new ElapsedEventHandler(onTimerElapsed);
private void onMouseDown(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Right)
{
return;
}
runTimer.Enabled = false;
}
private void onMouseUp(object sender, MouseEventArgs e)
{
runTimer.Enabled = false;
}
public void updatePanelLocation()
{
if (this.InvokeRequired)
{
this.Invoke(new updatePanelCallback(updatePanelLocation), new object[] {});
}
else
{
Cursor curs = new Cursor(Cursor.Current.Handle);
panel1.Location = curs.Position;
}
}
private void onTimerElapsed(object source, ElapsedEventArgs e)
{
updatePanelLocation();
}
You could try something in two steps, preparing the action on MouseDown event and finishing it on MouseUp.
In the following code, how do I identify which control raised the Click event?
void x_Click(object sender, EventArgs e)
{
//How do I identify the sender?
}
private void fill()
{
for(blah)
{
Button x = new Button();
x.Click += new EventHandler(x_Click);
this.controls.Add(x)
}
}
void x_Click(object sender, EventArgs e)
{
Button who = (Button) sender;
// you can now access who.Text, etc.
}