How to set mouse hover for ALL buttons? - c#

I have bunch of buttons on my form. And I would like to make it a bit nicer so button changing color and font to bold when mousei s over it seems like good idea. I would appreciate any help
button.BackColor = Color.Cyan;
button.Font = new Font(button.Font.Name, button.Font.Size, FontStyle.Bold);
EDIT:
this is working for me:
private void button1_MouseEnter(object sender, EventArgs e)
{
((Button)sender).BackColor = Color.PaleTurquoise;
((Button)sender).Font = new Font(((Button)sender).Font.Name, ((Button)sender).Font.Size, FontStyle.Bold);
}
private void button1_MouseLeave(object sender, EventArgs e)
{
((Button)sender).BackColor = Color.WhiteSmoke;
((Button)sender).Font = new Font(((Button)sender).Font.Name, ((Button)sender).Font.Size, FontStyle.Regular);
}
(there is button1_mousenter (or mouseleave) set as action for every button

Just select them all in your Form view and go to the mousehover event.
and write your code like this:
private void button_mousehover (object sender, EventArgs e)
{
((Button)sender).BackColor = Color.Cyan;
((Button)sender).Font = new Font(((Button)sender).Font.Name, ((Button)sender).Font.Size, ((Button)sender).FontStyle.Bold;
}

You can add MouseEnter and MouseLeave events to your buttons that changes the buttons' colors.
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.mouseenter(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.mouseleave(v=vs.110).aspx
// bind handler to MouseEnter Event
this.yourButton1.MouseEnter += new System.EventHandler(this.allButtons_MouseEnter);
this.yourButton2.MouseEnter += new System.EventHandler(this.allButtons_MouseEnter);
// bind handler to MouseLeave Event
this.yourButton1.MouseLeave += new System.EventHandler(this.allButtons_MouseLeave);
this.yourButton2.MouseLeave += new System.EventHandler(this.allButtons_MouseLeave);
// enter handler
private void allButtons_MouseEnter(object sender, System.EventArgs e)
{
Button btn = (Button)sender;
btn.BackColor = Color.Cyan;
btn.Font = new Font(btn.Font.Name, btn.Font.Size, FontStyle.Bold);
}
// leave handler
private void allButtons_MouseLeave(object sender, System.EventArgs e)
{
Button btn = (Button)sender;
btn.BackColor = Color.DeepPink; // whatever your original color was
btn.Font = new Font(btn.Font.Name, btn.Font.Size, FontStyle.Regular);
}

Related

How to change the ForeColor of a disabled button

So I have this button that launches another program. I disable it after launch to prevent double launch situations. Problem is the ForeColor changes from white to black which doesnt look good against my dark BackColor. How do I make the ForeColor not change when disabling or even change it after disable?
You need to use EnabledChanged and paint events of the current button. Suppose your button name is button1.
private void button1_EnabledChanged(object sender, EventArgs e)
{
Button currentButton = (Button)sender;
button1.ForeColor = currentButton.Enabled== false ? Color.White : currentButton.ForeColor ;
button1.BackColor = currentButton.Enabled == false? Color.Black : currentButton.BackColor;
}
private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
}
private void button1_Paint(object sender, PaintEventArgs e)
{
Button btn = (Button)sender;
var solidBrush = new SolidBrush(btn.ForeColor);
var stringFormat = new StringFormat
{
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center
};
e.Graphics.DrawString(button1.Text, btn.Font, solidBrush, e.ClipRectangle, stringFormat);
solidBrush.Dispose();
stringFormat.Dispose();
}

How to remove panels from button C#

I have created a function that creates a new panel each time a button is pressed, alongside with a button, which should remove the entire panel when pressed.
Here is my code for creating the panels and the button :
Panel panel;
private void button1_Click(object sender, EventArgs e)
{
panel = new Panel();
panel.BackColor = Color.FromArgb(38, 38, 38);
panel.Margin = new System.Windows.Forms.Padding(10);
flowLayoutPanel1.Controls.Add(panel);
panel.Show();
Button delbutton = new Button();
delbutton.Text = "X";
flowLayoutPanel1.Controls.Add(delbutton);
delbutton.Click += new EventHandler(this.ButtonFunction_Click);
}
Considering that every panel created has this delbutton , how can i remove the panel of which delbutton button was pressed?
I tried to add this method to the button, but it removes panel randomly :
void ButtonFunction_Click (Object sender,EventArgs e)
{
foreach (Control controlObj in flowLayoutPanel1.Controls)
{
flowLayoutPanel1.Controls.Remove(controlObj);
controlObj.Dispose();
}
}
You can add the relevant Panel as a Tag property of its Button, then you can get that in the Click handler
private void button1_Click(object sender, EventArgs e)
{
var panel = new Panel
{
BackColor = Color.FromArgb(38, 38, 38),
Margin = new Padding(10),
};
flowLayoutPanel1.Controls.Add(panel);
panel.Show();
var delbutton = new Button
{
Text = "X",
Tag = panel,
};
delbutton.Click += ButtonFunction_Click;
flowLayoutPanel1.Controls.Add(delbutton);
}
private void ButtonFunction_Click(Object sender, EventArgs e)
{
var button = (Button)sender;
((Control)button.Tag).Dispose();
button.Dispose();
}

Remove Handler to Button

I'm trying to remove a handler (Click) from a button, I tried many ways but they didn't work.
I create two buttons like this:
private void CreateButtons()
{
Button button1 = new Button()
{
Text = "Start"
};
Button button2 = new Button()
{
Text = "Stop"
};
this.Controls.Add(button1);
button1.Click += Button1_Click;
this.Controls.Add(button2);
button2.Click += Button2_Click;
}
The first one just creates a MessageBox:
private void Button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello World");
}
The second one should remove the handler:
private void Button2_Click(object sender, EventArgs e)
{
button1.Click -= Button1_Click;
}
Unfortunately after clicking button2, button1 keeps generating a MessageBox. What am I doing wrong?
It appears that your form has a different button you've created at an earlier time named button1.
Remove that, and make your button1 and button2 fields of the form.
public partial class Form1 : Form
{
private Button button1;
private Button button2;
public Form1()
{
InitializeComponent();
}
private void CreateButtons()
{
button1 = new Button()
{
Font = new Font(Font.FontFamily, 10, FontStyle.Bold),
BackColor = Color.Yellow,
Width = 79,
Height = 62,
Location = new Point(141, 191),
Text = "Start"
};
button2 = new Button()
{
Font = new Font(Font.FontFamily, 10, FontStyle.Bold),
BackColor = Color.Yellow,
Width = 79,
Height = 62,
Location = new Point(338, 191),
Text = "Stop"
};
this.Controls.Add(button1);
button1.Click += Button1_Click;
this.Controls.Add(button2);
button2.Click += Button2_Click;
}
private void Button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello World");
}
private void Button2_Click(object sender, EventArgs e)
{
button1.Click -= Button1_Click;
}
private void Form1_Load(object sender, EventArgs e)
{
CreateButtons();
}
}

Changing the properties of a dynamically created control

I have a Form with a LayoutPanel that has dynamically added buttons inside. The buttons are added at runtime, which works, but my problem is I'd like to set the properties to one of the buttons to disabled if a textBox is empty and enable it when it the textBox is not empty.
Here is a code example, with the error I am receiving below:
private void Form1_Load(object sender, EventArgs e)
{
// Create 3 buttons
Button button1 = new Button();
button1.Name = "button1";
tableLayoutPanel1.Controls.Add(button1, 0, 0);
Button button2 = new Button();
button1.Name = "button2";
tableLayoutPanel1.Controls.Add(button2, 0, 0);
Button button3 = new Button();
button3.Name = "button1";
tableLayoutPanel1.Controls.Add(button3, 0, 0);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox1.Text))
{
button1.Enabled = false;
}
else
{
button1.Enabled = true;
}
}
CS0103 The name 'button1' does not exist in the current context
Should I be declaring the buttons elsewhere so the entire code can see that they do in fact exist or is my problem elsewhere? Thanks.
You must declare a Button outside Form1_Load if you want to access the button with name directly from other methods, because in your case the buttons are just available in the Form1_Load method :
Button button1;
private void Form1_Load(object sender, EventArgs e)
{
// Create 3 buttons
button1 = new Button();
button1.Name = "button1";
tableLayoutPanel1.Controls.Add(button1, 0, 0);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
button1.Enabled = !string.IsNullOrEmpty(textBox1.Text);
}
or if you want declare Buttons inside Form1_Load, you can access to Button like this :
private void textBox1_TextChanged(object sender, EventArgs e)
{
var btn = tableLayoutPanel1.Controls.OfType<Button>().Where(x => x.Name == "button1").FirstOrDefault();
(btn as Button).Enabled = !string.IsNullOrEmpty(textBox1.Text);
}
As an alternative, you could dynamically get the control from the Layout Panel and set the enabled property like this:
private void textBox1_TextChanged(object sender, EventArgs e)
{
Control button = tableLayoutPanel1.Controls["button1"];
button.Enabled = string.IsNullOrEmpty(textBox1.Text) ? false : true;
}
This approach is not as "safe" as declaring the buttons at form level, but I thought it would be useful to mention for times when you need to be genuinely dynamic in referencing controls.
There are a couple ways you can do this, one would be to use the Find method of the TableLayoutPanel's Controls Collection like this.
private void textBox1_TextChanged(object sender, EventArgs e)
{
Button btn =(Button)tableLayoutPanel1.Controls.Find("button1", true)[0];
if (string.IsNullOrEmpty(textBox1.Text))
{
btn.Enabled = false;
}
else
{
btn.Enabled = true;
}
}
The second would be to use the buttons Tag Property to determine which control to use, I have used this in the past for dynamically generated controls.
private void Form1_Load(object sender, EventArgs e)
{
// Create 3 buttons
Button button1 = new Button();
button1.Name = "button1";
button1.Tag = 1; //note the Tag property being used
tableLayoutPanel1.Controls.Add(button1, 0, 0);
Button button2 = new Button();
button1.Name = "button2";
button2.Tag = 2;
tableLayoutPanel1.Controls.Add(button2, 0, 0);
Button button3 = new Button();
button3.Name = "button3";
button3.Tag = 3;
tableLayoutPanel1.Controls.Add(button3, 0, 0);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
foreach (Control c in tableLayoutPanel1.Controls) //iterate through controls
{
if ((int)c.Tag == 1) //if Tag is equal then process
{
if (c is Button)
{
if (string.IsNullOrEmpty(textBox1.Text))
{
((Button)c).Enabled = false;
}
else
{
((Button)c).Enabled = true;
}
break; //if you have multiple controls to process remove this
} //and assign the same tag to the controls you want processed
}
}
}

Change WinForms Button Style upon mousedown and leave

How can I change a button's style, in particular the image, upon the mouse being pressed and held and then the cursor dragged away from the button?
You'll notice that, on this action, the default behaviour for the button is to revert it's style to hover style. This can be partially configured using the MouseOverBackColor. I want to ensure that, whenever the MouseOverBackColor is applied, I also have a specific image on the button.
I have tried the code below, to have an "isMouseDown" flag which is checked in the leave event. However, this doesn't work for me.
private void btnFormMinimize_MouseClick(object sender, MouseEventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
bool isMouseDown = false;
private void btnFormMinimize_MouseDown(object sender, MouseEventArgs e)
{
Button b = (Button)sender;
b.Image = Properties.Resources.icon_minimize_click;
isMouseDown = true;
}
private void btnFormMinimize_MouseEnter(object sender, EventArgs e)
{
Button b = (Button)sender;
b.Image = Properties.Resources.icon_minimize_hover;
}
private void btnFormMinimize_MouseLeave(object sender, EventArgs e)
{
Button b = (Button)sender;
if (isMouseDown)
{
b.Image = Properties.Resources.icon_minimize_hover;
}
else
{
b.Image = Properties.Resources.icon_minimize;
}
}
private void btnFormMinimize_MouseUp(object sender, MouseEventArgs e)
{
isMouseDown= false;
}
Thanks in advance.

Categories

Resources