Removing the default hover effect of a button - c#

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.

Related

How to Change BackColor of a Textbox when tab key is pressed

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

Returning to 'MouseEnter' Color After 'MouseDown' is Called?

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;
}
}

Make TextBox Editable by ContextMenuStrip - c# winform

I have a textbox in my C# Winform. The program assigns a value to the textbox by default.
I want the user to have a right click function to edit this text at runtime. So when the user right click to edit, the backgroud should become white and user should be able to edit the text. And after editing, the background should return to default and non editable
I have created a ContextMenuStrip with right click event to edit text as follows and assigned readonly property to false when user right clicks and press edit menu item:
private void editTextToolStripMenuItem_Click(object sender, EventArgs e)
{
itxt_CommonTitle.ReadOnly = false;
}
I am not sure how to proceed further. Is this possible using textbox?
If you have not changed the BackColor of the TextBox in the designer then the background color should automatically change from white to gray when you set ReadOnly = true and change from gray back to white when you set ReadOnly = false. However, if you have changed it to something else in the designer, then the easiest way is just to set a private variable to remember the original BackColor before you enable the control for editing. Then you can restore the color after you set it back to read-only.
private void editToolStripMenuItem_Click(object sender, EventArgs e)
{
MakeTextBoxEditable(itxt_CommonTitle);
}
private void itxt_CommonTitle_Leave(object sender, EventArgs e)
{
MakeTextBoxReadOnly(itxt_CommonTitle);
}
private void Form1_Click(object sender, EventArgs e)
{
MakeTextBoxReadOnly(itxt_CommonTitle);
}
private Color origTextBoxBackColor = SystemColors.Control;
private void MakeTextBoxEditable(TextBox textBox)
{
origTextBoxBackColor = textBox.BackColor;
textBox.ReadOnly = false;
textBox.BackColor = Color.White;
textBox.Focus();
}
private void MakeTextBoxReadOnly(TextBox textBox)
{
textBox.ReadOnly = true;
textBox.BackColor = origTextBoxBackColor;
}
I think you are missing a process. After edit, there should be an update or save method.
textbox readonly = true;
edit textbox: textbox readonly = false;
button save: textbox readonyl = true;
Edit:
Something like this:
private void buttonSave_Click(object sender, EventArgs e)
{
textBox1.ReadOnly = true;
}
private void editToolStripMenuItem_Click(object sender, EventArgs e)
{
textBox1.ReadOnly = false;
}
You dont need to change backColor, just readonly prop is fine.

Button Hover in usercontrol

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.

Change Button text color

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;
}

Categories