0

I have a button / panel. I want it to be like when I click it'll be green and when i click once more it'll be black.

disabled: DISABLED

enabled: ENABLED

And here is my broken script:

private void panel11_Click(object sender, EventArgs e)
{
    if (panel11.Enabled)
    {
        panel11.BackColor = Color.Lime;
    }
    else
    {
        panel11.BackColor = Color.Black;
    }
}
jps
  • 20,041
  • 15
  • 75
  • 79
  • Where are you changing the state of `panel11.Enabled`? – Matthew Watson Mar 19 '21 at 10:03
  • Maybe you could use `panel11.BackColor = (panel11.BackColor == Color.Lime) ? Color.Black : Color.Lime;` – Matthew Watson Mar 19 '21 at 10:04
  • What are you targetting: Winforms, WPF, ASP..? YOU should __always__ TAG your questions correctly so one can see it on the questions page! – TaW Mar 19 '21 at 10:06
  • What is wrong with your code? What does it not do that you expect it to do? – DavidG Mar 19 '21 at 10:06
  • 2
    If a Control is not Enabled __you can't click it__. So this will not work. Why even disable it? What is the goal? You can find various solutions, e.g. use the `Tag` property to save a state or create a subclass with a bool property `checked`.. Or you could simply go for a real ChaeckBox and style it to look any way you want. (Make it's appearence flat..!) – TaW Mar 19 '21 at 10:07
  • @TaW Checkbox is easier i know. But i need to make the checkbox bigger... –  Mar 19 '21 at 10:16
  • Set AutoSize = false and you can make it as large as you want. You can also tweak various alignments. But writing a class of your own (a Custom control class) is always an interesting experience and of course the most extensible solution.. – TaW Mar 19 '21 at 10:18
  • @TaW is there any way to maker the checkbox bigger? only the checkbox. –  Mar 19 '21 at 10:29
  • Maybe [this post](https://stackoverflow.com/questions/35252382/how-can-i-change-the-color-of-the-check-mark-of-a-checkbox/35252891#35252891) helps? – TaW Mar 19 '21 at 10:32
  • Sorry I meant one of these [posts](https://stackoverflow.com/search?q=user%3A3152130+ControlPaint+) which are about using the [controlpaint class](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.controlpaint?view=net-5.0) – TaW Mar 19 '21 at 10:37

1 Answers1

0

Enabled is not a state of the button being clicked, it is availability of the button to be clicked. The only thing the script is doing is always setting its color to Lime, as I got it... You should introduce kind of boolean and switch between colors on click, it`s gonna be better