I create a Usercontrol in form. the usercontrol contain Button, line and label. I want the button background turn to Blue when its being hovered and return to black when mouse leave. When I tried to run the outcome its not that i expected. when i hover, nothing happen but when I leave the background color turn to blue.
this my code inside the usercontrol:
private void button1_MouseHover(object sender, EventArgs e)
{
button1.BackColor = Color.Blue;
}
private void button1_MouseLeave(object sender, EventArgs e)
{
button1.BackColor = Color.Black;
}
private void mEnter(object sender, EventArgs e)
{
button1.BackColor = Color.Blue;
}
private void mLeave(object sender, EventArgs e)
{
button1.BackColor = Color.Black;
}
Try using Mouse enter and mouse leave instead of mouse hover, mouse hover requires the mouse to actually be still above it. Enter just requires you to enter the bounds of the button.
Related
I have to create a C# program in visual studio 2015 that begins with showing three read only textboxes, the bottom one is green and the middle and top box are gray. When the tab key is pressed, the middle box should turn yellow and the other two boxes should be gray. Then with the tab key pressed again, the top box goes red and the bottom two go gray, and repeat with the tab key. I cannot get the boxes to change color unless I take it off of read only and proceed to type in the boxes. How to I fix my code to change colors with the tab key?
//when the txtRed box is active, it turns red and the others go gray
private void txtRed_TextChanged(object sender, EventArgs e)
{
txtRed.BackColor = System.Drawing.Color.Red;
txtYellow.BackColor = System.Drawing.Color.DarkGray;
txtGreen.BackColor = System.Drawing.Color.DarkGray;
}
//when the txtYellow box is active, it turns yellow and the others go gray
private void txtYellow_TextChanged(object sender, EventArgs e)
{
txtRed.BackColor = System.Drawing.Color.DarkGray;
txtYellow.BackColor = System.Drawing.Color.Yellow;
txtGreen.BackColor = System.Drawing.Color.DarkGray;
}
//when the txtGreen box is active, it turns green and the others go gray
private void txtGreen_TextChanged(object sender, EventArgs e)
{
txtRed.BackColor = System.Drawing.Color.DarkGray;
txtYellow.BackColor = System.Drawing.Color.DarkGray;
txtGreen.BackColor = System.Drawing.Color.Green;
}
//allows btnExit to terminate the program
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
"I cannot get the boxes to change color unless I take it off of read only and proceed to type in the boxes."
This is because you're using TextChanged event handlers. If you're wanting to perform an action following pressing the tab key, you'll need to use a PreviewKeyDown event handler:
private void txtRed_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Tab)
{
txtRed.BackColor = System.Drawing.Color.Red;
txtYellow.BackColor = System.Drawing.Color.DarkGray;
txtGreen.BackColor = System.Drawing.Color.DarkGray;
}
}
To achieve the desired effect you will need to respond to an Enter event rather than a TextChange event.
Your code should look as that shown below where tabbing between the textboxes will set the background to yellow if the textbox has focus or grey if it does not:
void txtRed_Enter(object sender, EventArgs e)
{
txtRed.BackColor = Color.Yellow;
txtYellow.BackColor = Color.Gray;
txtGreen.BackColor = Color.Gray;
}
void txtYellow_Enter(object sender, EventArgs e)
{
txtRed.BackColor = Color.Gray;
txtYellow.BackColor = Color.Yellow;
txtGreen.BackColor = Color.Gray;
}
void txtGreen_Enter(object sender, EventArgs e)
{
txtRed.BackColor = Color.Gray;
txtYellow.BackColor = Color.Gray;
txtGreen.BackColor = Color.Yellow;
}
You can view the relevant documentation here: Control.Enter Event
I'm creating a test C# WinForm Application and I have a closing button. When you hover over the button, the BackColor changes to a lighter color. When you stop hovering over it, the button changes back to the background color. Upon clicking the button, it changes to white, and when letting go, changes back to the background color. My question is, if someone hovers over and it changes to the hover color, then someone clicks and changes the color again, if they drag their mouse off of the button, can I make it change back to the hover color?
Code:
public Form1()
{
InitializeComponent();
bunifuImageButton1.MouseEnter += bunifuImageButton1_MouseHover;
bunifuImageButton1.MouseLeave += bunifuImageButton1_MouseLeave;
bunifuImageButton1.MouseDown += bunifuImageButton1_MouseDown;
bunifuImageButton1.MouseUp += bunifuImageButton1_MouseUp;
}
private void bunifuImageButton1_MouseHover(object sender, EventArgs e)
{
bunifuImageButton1.BackColor = SystemColors.Highlight;
}
private void bunifuImageButton1_MouseLeave(object sender, EventArgs e)
{
bunifuImageButton1.BackColor = SystemColors.HotTrack;
}
private void bunifuImageButton1_MouseDown(object sender, EventArgs e)
{
bunifuImageButton1.BackColor = SystemColors.Control;
}
private void bunifuImageButton1_MouseUp(object sender, EventArgs e)
{
bunifuImageButton1.BackColor = SystemColors.HotTrack;
}
private void bunifuImageButton1_Click(object sender, EventArgs e)
{
this.Close();
}
You can try to use a flat styled button setting the flatappearence colors, and see how it does the job automatically.
You could track if the mouse is down, and if the mouse is down and it leaves the button, change the color to the hover color.
There is a catch though, because MouseLeave doesn't fire if the mouse is down, so you need to check the mouse position on the MouseMove event, like this:
private bool isMouseDown;
private void bunifuImageButton1_MouseHover(object sender, EventArgs e)
{
bunifuImageButton1.BackColor = SystemColors.Highlight;
}
private void bunifuImageButton1_MouseDown(object sender, MouseEventArgs e)
{
bunifuImageButton1.BackColor = SystemColors.Control;
isMouseDown = true;
}
private void bunifuImageButton1_MouseUp(object sender, MouseEventArgs e)
{
bunifuImageButton1.BackColor = SystemColors.HotTrack;
isMouseDown = false;
}
private void bunifuImageButton1_MouseMove(object sender, MouseEventArgs e)
{
// If the mouse is down and the mouse is not over the button
if (isMouseDown && !bunifuImageButton1.Bounds.Contains(e.Location))
{
bunifuImageButton1.BackColor = SystemColors.Highlight;
}
}
When we pass the mouse over a button we can change the button back color using MouseOverBackColor and MouseDownBackColor using FlatApearance property box.
How can I change a button text color in the same mode when the mouse pass over it?
This should work for all sorts of Buttons, with or without FlatAppearance:
private void button1_MouseEnter(object sender, EventArgs e)
{
button1.ForeColor = Color.Red;
}
private void button1_MouseLeave(object sender, EventArgs e)
{
button1.ForeColor = SystemColors.ControlText;
}
By using RGB values, this can look like this:
//Hover Text Color changing
private void btnHome_MouseHover(object sender, EventArgs e)
{
btnHome.ForeColor = System.Drawing.Color.FromArgb(1, 102, 207);
}
private void btnHome_MouseLeave(object sender, EventArgs e)
{
btnHome.ForeColor = System.Drawing.Color.LightGray;
}
donĀ“t forget to do everytime a new method call for each button!
Just follow this figure to come to a nice solution:
If you only want to change button text color when mouse is over the button:
private void button1_MouseEnter(object sender, EventArgs e)
{
button1.ForeColor = System.Drawing.Color.Red;
}
private void button1_MouseLeave(object sender, EventArgs e)
{
button1.ForeColor = System.Drawing.Color.Black;
}
I am trying to apply a color to a hovered button like this:
private void button_MouseEnter(object sender, EventArgs e)
{
button.UseVisualStyleBackColor = false;
button.BackColor = System.Drawing.SystemColors.GradientInactiveCaption;
}
private void button_MouseLeave(object sender, EventArgs e)
{
button.UseVisualStyleBackColor = true;
button.BackColor = System.Drawing.SystemColors.ControlLight;
}
My Problem is there seems to be some kind of a default hover effect. The color is much darker than it should be. At the moment I do not know where to start to get rid of it.
I created a simple code here just to play around with C#. It has 3 buttons and 1 panel. If you click on the 2nd & 3rd button the panel height changes. Is that also possible to change the color?
For example, if I click on the 2nd button, I would like to have it as yellow and at the same time the height changes as well, and the same with 3rd button.
public partial class Form1 : Form
{
public int heightPanel;
public Form1()
{
InitializeComponent();
heightPanel = panel1.Height;
}
private void button1_Click(object sender, EventArgs e)
{
panel1.Height = heightPanel;
}
private void button2_Click(object sender, EventArgs e)
{
panel1.Height = this.Height/2;
}
private void button3_Click(object sender, EventArgs e)
{
panel1.Height = this.Height - 150;
}
}
I have an idea but I don't know where to put this. I think it would be something like this:
panel1.Height=this.BackColor.ToString();
Any inputs?
Response to reply
Yes, I would like to retain the 3 colors if I click in any of the button. I'm not sure if it's possible. For ex:
my button1 =pink
button2=yellow
button3=green
If I click on the button 1 I'll see the pink color, and if I click on the button2 I'll see the pink and yellow.
Is that possible?
All you'd need to do would be to set the BackColor on a new line. For instance...
private void button2_Click(object sender, EventArgs e)
{
panel1.Height = this.Height/2;
panel1.BackColor = Color.Yellow;
}
private void button3_Click(object sender, EventArgs e)
{
panel1.Height = this.Height - 150;
panel1.BackColor = Color.Yellow;
}
Simple as:
panel1.BackColor = Color.Red;
In button2_Click, just add another line like this:
panel1.BackColor = Color.Yellow;