0

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);
}
IDarkCoder
  • 709
  • 7
  • 18
  • See this https://stackoverflow.com/questions/4991041/c-sharp-change-a-buttons-background-color – Visakh V A Jun 26 '19 at 12:09
  • 1
    Hi welcome to SO, please post more of your code so we can retry in our conditions, `bc` variable could significant reason why. and can you also post error or exception it throws if any – Pribina Jun 26 '19 at 12:11
  • 3
    Why the `Thread.Sleep(...)`, it blocks the UIThread from updating the ui (this may be your problem) – Ackdari Jun 26 '19 at 12:13
  • 1
    Possible duplicate of [C# Change A Button's Background Color](https://stackoverflow.com/questions/4991041/c-sharp-change-a-buttons-background-color) – Denis Schaf Jun 26 '19 at 12:16
  • I have the thread.sleep because I want the user to see which of the answers is the correct one – Emil Kasyanov Jun 26 '19 at 12:18
  • 1
    @EmilKasyanov but due to the `Thread.Sleep` the user will likly don't see any change – Ackdari Jun 26 '19 at 12:34
  • @Ackdari so how can I fix that, I need a delay to show the user which answers are correct. – Emil Kasyanov Jun 26 '19 at 13:19
  • @EmilKasyanov: Please clarify your requirements. What outcome do you expect and what do you currently see? – mm8 Jun 26 '19 at 13:31

3 Answers3

0
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;
}
Jamez Fatout
  • 402
  • 4
  • 13
0

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.

IDarkCoder
  • 709
  • 7
  • 18
-1

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!

  • 4
    `BackColor` is not a property of a WPF `Button` – Ackdari Jun 26 '19 at 12:19
  • Change Backcolor to Background – Jamez Fatout Jun 26 '19 at 21:16
  • As other comments already said, `BackColor` doesn't exist for a WPF `Button` but `Color.Red` also doesn't exist here. For `System.Windows.Media.Color` the predefined colors are `Colors.Red`, `Colors.Green`, etc (also in `System.Windows.Media`) – IDarkCoder Jun 28 '19 at 09:01