How to change color text in button? - c#

private void arrButton_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
if (turn == 0)
{
button.ForeColor = Color.Green; // Can't change color
button.Text = "X";
button.Enabled = false;
turn = 1;
}
else
{
button.Text = "O";
button.Enabled = false;
turn = 0;
}
}
I used button.ForeColor = new Color.Green but when I test X still can't change green color.
How to change color text in button ?

Disabled component doesn't effect any graphical changes. It must be enabled to reflect the Color change.
You should use any other condition to check disabled button if you want to keep the graphical changes.
For Example:
if(button.ForeColor == Color.Green)
//handle the click event

for wpf:
private void arrButton_Click(object sender, RoutedEventArgs e)
{
button.Foreground= Brushes.Blue;
}
for Winform:
private void arrButton_Click(object sender, EventArgs e)
{
button.BackColor = Color.Red;
}

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

Having trouble changing buttons background

I tried to change the background of the buttons when a button is clicked it doesn't color the background of the button.
My attempt:
private void Ans1_Click(object sender, RoutedEventArgs e)
{
//green the correct answer
Ans1.Background = bc.ConvertFromString("#FF3C9C27") as SolidColorBrush;
//rest all red
Ans2.Background = bc.ConvertFromString("#FFAE2F2F") as SolidColorBrush;
Ans3.Background = bc.ConvertFromString("#FFAE2F2F") as SolidColorBrush;
Ans4.Background = bc.ConvertFromString("#FFAE2F2F") as SolidColorBrush;
Thread.Sleep(1500);
}
private void Ans1_Click(object sender, RoutedEventArgs e)
{
var button = sender as Button; <-- sender is the current button
button.Background = bc.ConvertFromString("#FF3C9C27") as SolidColorBrush;
}
It seems that your conversion is the problem.
bc.ConvertFromString("#FF3C9C27") most likely returns a System.Windows.Media.Color.
System.Windows.Media.Color as SolidColorBrush returns null however.
This should give you the desired result:
private void Ans1_Click(object sender, RoutedEventArgs e)
{
//green the correct answer
Ans1.Background = new SolidColorBrush((Color)bc.ConvertFromString("#FF3C9C27"));
//rest all red
Ans2.Background = new SolidColorBrush((Color)bc.ConvertFromString("#FFAE2F2F"));
Ans3.Background = new SolidColorBrush((Color)bc.ConvertFromString("#FFAE2F2F"));
Ans4.Background = new SolidColorBrush((Color)bc.ConvertFromString("#FFAE2F2F"));
}
About the Thread.Sleep "issue": you could use a Timer instead.
To change the background color of your button, inside your clicked function that's been auto-generated add the following code:
Ans1.BackColor = Color.Green;
Ans2.BackColor = Color.Red;
Ans3.BackColor = Color.Red;
Ans4.BackColor = Color.Red;
Hope this helps!

how to clear button color second click in flow layout panel c#

I have a lot of buttons in flow layout panel. I created these buttons programmatically.
But I didn't if click button its color will be light green after second click will be reset color. I wrote this handler for flow layout panel. How can I do second click will clear button color:
private void Form1_Load(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
sb.Append( "3 x coca cola 33 cl");
sb.Append(Environment.NewLine);
sb.Append("DIN");
sb.Append("\t\t");
sb.Append("200");
//button2.Text = sb.ToString();
string products = "30000|3;3110002;Cola;400;150;100;1;1000|3;3110003;Snickers;400;150;100;1;1000|3;3110004;NesttleCrunch;400;150;100;1;1000|3;3110005;Marlbora;400;150;100;1;1000|3;3110006;Orange;400;150;100;1;1000|3;3110007;Milk;400;150;100;1;1000|3;3110008;Water;400;150;100;1;1000|3;3110009;Banana;400;150;100;1;1000|3;3110010;Honey;400;150;100;1;1000|3;3110011;Beer;400;150;100;1;1000|3;3110012;Hazelnut;400;150;100;1;1000|3;3110013;RedBull;400;150;100;1;1000|3;3110014;ChewingGum;400;150;100;1;1000|3;3110015;Apple;400;150;100;1;1000";
string[] listproducts = products.Split('|');
lblAvaliablePoint.Text = listproducts[0];
for (int i = 1; i < listproducts.Count();i++ )
{
string[] perproduct = listproducts[i].Split(';');
Button newButton = new Button();
newButton.Size = new System.Drawing.Size(170, 87);
newButton.BackColor = Color.LightGray;
newButton.UseVisualStyleBackColor = false;
newButton.Tag =perproduct[1];
newButton.Text = perproduct[0] + "x" + perproduct[2];
newButton.Click += new EventHandler(ButtonClickHandler);
flowLayoutPanel1.Controls.Add(newButton);
}
flowLayoutPanel1.VerticalScroll.Maximum = flowLayoutPanel1.Height+40;
flowLayoutPanel1.VerticalScroll.LargeChange = 30;
}
public void ButtonClickHandler(Object sender,EventArgs e)
{
((Button)sender).BackColor = Color.LightGreen;
}
What about add simple if condition into button handler like this?
public void ButtonClickHandler(object sender, EventArgs e)
{
if (((Button)sender).BackColor == Color.LightGreen)
{
((Button)sender).BackColor = Color.White; // Your default color
}
else
{
((Button)sender).BackColor = Color.LightGreen;
}
}
Change your ButtonClickHandler
public void ButtonClickHandler(Object sender, EventArgs e)
{
var currentButton = ((Button)sender);
if(currentButton != null)
{
currentButton.BackColor = currentButton.BackColor == Color.LightGreen ? Color.LightGray : Color.LightGreen;
}
}

private method issue c# (change color from tool strip menu item)

I have a logic issue on a tic tac toe game.
I want to add a new feature.
When I click on "Yellow Color" in the menu, I want my red cross to become yellow when the cursor enters the button.
I can't see the variable "b" from the following method, so I was wondering how should I do that.
private void yellowColorToolStripMenuItem_Click(object sender, EventArgs e)
{
b.ForeColor= System.Drawing.Color.Yellow;
} //syntax error
private void button_enter(object sender, EventArgs e)
{
Button b = (Button)sender;
if (b.Enabled)
{
if (turn)
{
b.ForeColor = System.Drawing.Color.Red;
b.Text = "X";
}
else
{
b.ForeColor = System.Drawing.Color.Blue;
b.Text = "O";
}
}
}
I couldn't find anything on the net
You've declared a local variable in the button_enter method. That variable is only available within the method. If you want that variable to be part of the start of the instance, you need to make it an instance variable, declared outside any method.
However, it sounds like the real state that you want isn't another button reference - it's "the colour to set the foreground to when the cursor enters the button". So you might have:
private Color entryColor;
private void yellowColorToolStripMenuItem_Click(object sender, EventArgs e)
{
entryColor = Color.Yellow;
}
private void button_enter(object sender, EventArgs e)
{
Button b = (Button) sender;
if (b.Enabled)
{
if (turn)
{
b.ForeColor = entryColor;
b.Text = "X";
}
else
{
b.ForeColor = Color.Blue;
b.Text = "O";
}
}
}

Change label backcolor when click

Hello guys i have a easy problem here, if i click the label1 it will change back Color to Red but my default Back Color is transparent.
private void label_Click(object sender, EventArgs e)
{
label1.BackColor = Color.Red;
}
private void label2_Click(object sender, EventArgs e)
{
label2.BackColor = Color.Red;
}
what if i click the label again i want it to change color to transparent, how do i code that? Thank you in advance! :D
label.BackColor = Color.Transparent;
You just need to flip the color based on its current value. That can be done by doing:
label1.BackColor = label1.BackColor == Color.Red ? Color.Transparent : Color.Red;
The above is a conditional operator and is basically just shorthand for an if/else statement,
if (label1.BackColor == Color.Red)
label1.BackColor = Color.Transparent
else
label1.BackColor = Color.Red;
Why don't you just add an if statement:
private void label_Click(object sender, EventArgs e)
{
if(label1.BackColor == Color.Red)
{
label1.BackColor = Color.Transparent;
}
else
{
label1.BackColor = Color.Red;
}
}
private void label_Click(object sender, EventArgs e)
{
Label label1 = (Label)sender;
if (label1.BackColor == Color.Red)
label1.BackColor = Color.Transparent;
else
label1.BackColor = Color.Red;
}
by using the line Label label1 = (Label)sender; You can apply the same event for all your labels.
if( label.BackColor == Color.Red)
{
label.BackColor = Color.Transparent;
}else
{
label.BackColor = Color.Red;
}

Categories

Resources