1

I have made a custom button control and want it to behave like RadioButtons in a group. The button will be placed in a group or collection of its type on the same container or panel (button dragged_dropped onto the container). When i click any of these buttons, i need the following to happen:

  1. Change the BackColor of the clicked button to a predefined color: Color.Cycan;

  2. Set the checked state of the clicked button from false to true

  3. Ensure that all other buttons of the same type on the same panel/container are set to their default settings which are: BackColor = Color.Gray; checked = false;

I have tried the below code but i am obtaining this result:

  1. The BackColor of the clicked button is changing successfully to Color.Cycan;

  2. The default BackColor of all other buttons of the same type on the same container is being set to default successfully: Color.Gray;

  3. But the checked state of the clicked button is not changing to true. It remains false.

I have attached the code i have tried, below:

What could be the issue or where am i missing the point?

This is a Winform / Windows Forms button control and i am programming in C#.

I do not want to use a radio button for this because i have custom designed MyButton.

Thank you.

THIS IS THE CODE I HAVE TRIED:

// Calling 'ClickedButton();' on click event.

private bool checked = false;
private void ClickedButton()
{

do(

BackColor = Color.Cycan;
checked = true;

if(Parent == null)
break;

foreach(MyButton button in Parent.Controls.OfType<MyButton>())
{

if(button == this) { continue }

else{

BackColor = Color.Gray;
checked = false;
}
}

)
while(false)
}
Russell Chidhakwa
  • 339
  • 2
  • 5
  • 16
  • Where is checked variable defined? – EylM Nov 03 '19 at 19:13
  • It is defined in the same class as the function. I have edited the code to reflect this. I had bad-formatted the code so StackOverflow couldn't show the variable. I have edited it to reflect the variable. – Russell Chidhakwa Nov 03 '19 at 19:23
  • See the code [here](https://stackoverflow.com/a/56938597/7444103). It defines the behavior of some controls, related to each other, using `Action()`s. – Jimi Nov 03 '19 at 19:56
  • What is the role of the `do...while` here? Doesn't make sense. The foreach...next is enough and will do the job. Remove the `do..while` block and give it a try. –  Nov 03 '19 at 20:59
  • @JQSOFT: Removing the ```do..while``` loop does not solve the problem. I want the checked state of the clicked button to be set to ```true``` and set all other buttons checked state to ```false```. Be sure enough to include tested code in your response, i may be missing something here. I resorted to use the ```do while loop``` in an attempt to change the ```boolean``` state of the buttons. – Russell Chidhakwa Nov 03 '19 at 21:46
  • How did you managed to do this? `private bool` **checked** `= false;` Anyways, @Hotkey gave you a beautiful solution. Good luck. –  Nov 04 '19 at 02:24
  • @JQSOFT: Most likely your advice to remove the ```do...while``` loop was just exactly like what we see in the solution provided by @Hotkey. I was missing the point. I got it. Thank you for your advice. – Russell Chidhakwa Nov 04 '19 at 03:28
  • Most welcome mate. At your disposal anytime. –  Nov 04 '19 at 03:32
  • Have you ever noticed `Appearance` property of `RadioButton` can be set to `Button` and `FlatStyle` can be set to `Flat` and `FlatAppearance` have propertied for setting `MouseOverBackColor` and `CheckedBackColor`? You can satisfy the requirement without any custom control. Also a `ToolStrip` will be a really good option for a vertical navigation menu. Take a look at [this post](https://stackoverflow.com/a/36252030/3110834). – Reza Aghaei Nov 04 '19 at 08:42

1 Answers1

3

How about use this method?

private bool isSelected = false;

private Color activeColor = Color.FromArgb(117, 117, 117);
private Color defaultColor = Color.FromArgb(66, 66, 66);

private void ClickedButton()
{
    foreach (Control parentControl in Parent.Controls)
    {
        if (parentControl is MyButton parentButton)
        {
            parentButton.isSelected = false;
            parentButton.BackColor = parentButton.isSelected ? parentButton.activeColor : parentButton.defaultColor;
        }
    }

    this.isSelected = true;
    this.BackColor = isSelected ? activeColor : defaultColor;
}

It works like this.

buttonTest

Hotkey
  • 421
  • 5
  • 11
  • 1
    I appreciate your solution. Not only does it work but the code is much cleaner and faster than iterating inside a ```do...while``` loop. I tested your solution and it generates the desired result. – Russell Chidhakwa Nov 04 '19 at 03:26