Change Button text color - c#

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

Related

Windows Form Application "How do I increase the size of the button when the mouse hovers over the button?"

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

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

Removing the default hover effect of a button

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.

Show button only when focus is on textbox

Is this possible to display button on Windows Form only when focus is on specific textbox?
Tried that with this approach:
private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show("OK");
}
private void textBox2_Enter(object sender, EventArgs e)
{
button3.Visible = true;
}
private void textBox2_Leave(object sender, EventArgs e)
{
button3.Visible = false;
}
No luck, because button click does not work then, because button is hidden immediately after textbox lost focus, preventing it from firing button3_Click(/*...*/) { /*...*/ } event.
Now I'm doing it like that:
private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show("OK");
}
private void textBox2_Enter(object sender, EventArgs e)
{
button3.Visible = true;
}
private void textBox2_Leave(object sender, EventArgs e)
{
//button3.Visible = false;
DoAfter(() => button3.Visible = false);
}
private async void DoAfter(Action action, int seconds = 1)
{
await Task.Delay(seconds*1000);
action();
}
Form now waits for a second and only then hides button3.
Is there any better approach?
I think you want to display the button only when focus is on specific textbox or the focus is on the button.
To do this you can check the Focused property of button3 in the Leave event of textBox2 and only hide the button if the button doesn't have focus. Note that the button will get focus before the Leave event of textBox2 fires.
You will then need to hide the button in the scenario where button3 loses focus and the focus moves to somewhere other than textBox2. You can use exactly the same technique here by handling the Leave event of button3 and only hiding button3 if textBox2 does not have focus.
The following code should fit your requirements:
private void textBox2_Leave(object sender, EventArgs e)
{
if (!button3.Focused)
{
button3.Visible = false;
}
}
private void button3_Leave(object sender, EventArgs e)
{
if (!textBox2.Focused)
{
button3.Visible = false;
}
}
private void textBox2_Enter(object sender, EventArgs e)
{
button3.Visible = true;
}
private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show("Button clicked");
}
Why not work with the GotFocus and LostFocus event of the TextBox?
private void textBox2_GotFocus(object sender, EventArgs e)
{
button3.Visible = true;
}
Then hide the button on the click event.
private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show("OK");
button3.Visible = false;
}
How about you add a Panel and place the button and text boxes in that panel and when user MouseHovers that Panel then display the button...
This way user would be able to click on the button...
This is the event you are looking for, I think...
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousehover(v=vs.110).aspx
UPDATE:
var textboxFocussed = false;
private void textBox2_Enter(object sender, EventArgs e)
{
textboxFocussed = true;
}
private void textBox2_Leave(object sender, EventArgs e)
{
textboxFocussed = false;
}
UPDATE 2
private void Panel_GotFocus(object sender, EventArgs e)
{
button3.Visible = textboxFocussed;
}
private void Panel_LostFocus(object sender, EventArgs e)
{
button3.Visible = false;
}
Here are the details of the Panel Events
you can add Enter event handler for all controls on form at Load. Just make sure to skip the controls on which you want to show the button.
List<string> strControlException = new List<string>();
public Form1()
{
InitializeComponent();
strControlException.Add("btnMain");
strControlException.Add("txtMain");
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < this.Controls.Count;i++ )
{
if (!strControlException.Contains(Controls[i].Name))
{
Controls[i].Enter += new EventHandler(hideButton);
}
}
}
private void txtMain_Enter(object sender, EventArgs e)
{
btnMain.Visible = true;
}
private void hideButton(object sender, EventArgs e)
{
btnMain.Visible = false;
}
btnMain (Button you want to Manipulate) and txtMain (Which controls the vibility of the button) are the controls in contention here
Add more controls on the form to test.
Explanation for the above code :
First initialize a list with the names of controls that should show the Button
On Form Load add an Event handler to all controls (except the one in our list)
In the handler function hide the button. (You might want to perform more logic here based on the control that called this function)
Button is hidden by default and only on textbox Enter event we show the button.

How to gray out default text in textbox?

When I click on a textbox, I want the default text to disappear. Is there any other property that would work for this purpose?
The following Will set Default text in Gray color. Also When you leave the textbox as blank, again set default text to the textbox.
private void Form1_Load(object sender, EventArgs e)
{
this.textBox2.Enter += new EventHandler(textBox2_Enter);
this.textBox2.Leave += new EventHandler(textBox2_Leave);
textBox2_SetText();
}
protected void textBox2_SetText()
{
this.textBox2.Text = "Default Text...";
textBox2.ForeColor = Color.Gray;
}
private void textBox2_Enter(object sender, EventArgs e)
{
if (textBox2.ForeColor == Color.Black)
return;
textBox2.Text = "";
textBox2.ForeColor = Color.Black;
}
private void textBox2_Leave(object sender, EventArgs e)
{
if (textBox2.Text.Trim() == "")
textBox2_SetText();
}
Add a method to the GotFocus event of the TextBox that will change the Text property to ""
private void Form1_Load(object sender, EventArgs e) {
this.textBox1.GotFocus += new EventHandler(textBox1_Focus);
this.textBox1.Text = "some default text...";
}
protected void textBox1_Focus(Object sender, EventArgs e) {
textBox1.Text = "";
}
It sounds like you are wanting a Watermark in your Textbox.
See these articles.
http://www.codeproject.com/Articles/319910/Custom-TextBox-with-watermark
Watermark in System.Windows.Forms.TextBox
and if you are using wpf something like this http://msdn.microsoft.com/en-us/library/bb613590.aspx

Categories