Change label backcolor when click - c#

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

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 change color text in button?

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

how to change color text when edited in datagridview

how can i change the color of the text which is being edited. i have a datagridview with values inside. what i want is when the user edit the text inside the cell. it must change the text to red how can i do that?
private void Gridview_Output_CellFormatting_1(object sender, DataGridViewCellFormattingEventArgs e)
{
if (Gridview_Output.Columns[e.ColumnIndex].Name == "FontOut")
{
Gridview_Output.Rows[e.RowIndex].Cells[3].Value = "FontOut";
DataGridViewCellStyle Cstyle = new DataGridViewCellStyle();
Cstyle.ForeColor = Color.Red;
}
}
This will work for all Columns:
private void Gridview_Output_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
DataGridViewCell cell = Gridview_Output[e.ColumnIndex, e.RowIndex];
cell.Style.ForeColor = Color.Red;
}
If you want to restrict it simply add a check like this:
if (cell.OwningColumn.Name == "yourColumnName")
cell.Style.ForeColor = Color.Red
If you want to keep the color when the user edits the cell but leaves it with the same value as before use these two events (if your Tag is not in use):
private void DGV_Points_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
DataGridViewCell cell = DGV_Points[e.ColumnIndex, e.RowIndex];
cell.Tag = cell.Value != null ? cell.Value : "";
}
private void DGV_Points_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
DataGridViewCell cell = DGV_Points[e.ColumnIndex, e.RowIndex];
if (cell.Tag != null && cell.Tag.ToString() != cell.Value.ToString())
cell.Style.ForeColor = Color.Red;
}
So you want to show the edited values in red right??
Then do like this..
object previousValue;
public Form1()
{
dgv.CellBeginEdit += dgv_CellBeginEdit;
dgv.CellEndEdit += dgv_CellEndEdit;
}
void dgv_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (dgv[e.ColumnIndex, e.RowIndex].Value != previousValue)
dgv[e.ColumnIndex, e.RowIndex].Style.ForeColor = Color.Red;
}
void dgv_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
previousValue = dgv[e.ColumnIndex, e.RowIndex].Value;
}

Flashing lines of GridView in C # WinForms

I would like make a kind of signaling the red lines make it RED to white every a fiew moment with timer.until now i have made only the color RED without flash this is the code :
private void dataGridViewX1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (DateTime.Parse(dataGridViewX1.Rows[e.RowIndex].Cells[8].Value.ToString()).AddMonths(-1).Month == DateTime.Now.Month)
{
e.CellStyle.BackColor = System.Drawing.Color.Red;}}
until now I have only the color red which appears
So I have tried write some code to get glow ( flashing my color Red with white) in event of Timer control timer_tick but its false i have only the same result
private void timer1_Tick(object sender, EventArgs e)
{
int i = 0;
while (i < dataGridViewX1.Rows.Count - 1)
{
if(
dataGridViewX1.Rows[i].DefaultCellStyle.BackColor == Color.Red)
{
dataGridViewX1.Rows[i].DefaultCellStyle.BackColor = Color.White;
}
i++;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (dataGridViewX1.RowsDefaultCellStyle.BackColor == Color.Red)
{
dataGridViewX1.RowsDefaultCellStyle.BackColor = Color.White;
return;
}
if (dataGridViewX1.RowsDefaultCellStyle.BackColor == Color.White)
{
dataGridViewX1.RowsDefaultCellStyle.BackColor = Color.Red;
return;
}
}

Categories

Resources