1

I've three toggle buttons: b1, b2, b3 and I put them in an array like this:

ToggleButton[] btnArray = new ToggleButton[] {b1, b2, b3};

If b1 is clicked, b1.FontWeight = FontWeights.Bold, and b2 & b3 equal to Normal. If b2 is clicked, b2.FontWeight = FontWeights.Bold, and b1 & b3 equal to Normal. If b3 is clicked, b3.FontWeight = FontWeights.Bold, and b1 & b2 equal to Normal.

b1   b2   b3
O    X    X
X    O    X
X    X    O

I know the easiest way to update each toggle button is listing down one by one, but it will make my code looks so duplicated. I've other toggle buttons' properties need to be updated at the same time as well. So, how can I use for loop to make it less duplicated? Or is there any other better way to do this?

YWah
  • 571
  • 13
  • 38
  • 1
    Maybe you should use RadioButtons instead [example](http://www.wpftutorial.net/RadioButton.html) and then style then if necessary. – E. Malmqvist Jul 17 '15 at 09:07

3 Answers3

3

Here is my suggestion:

ToggleButton[] btnArray = new ToggleButton[] {b1, b2, b3};

public SwitchButton(int index)
{
    foreach(var btn in btnArray)
    {
        // Set all buttons deactive (no hover)
        btn.Color = deactiveColor; // Example
    }

    // Set specified (by index) btn active (hover)
    var btn = btnArray[index];
    btn.Color = activeColor; // Example
}

That should be pretty simple and readable :)

Update for your request: In this code, you should set your deactive button properties in foreach, that will make all buttons deactive/unselected style. Later just change requested (by index) button's properties active style. I randomly used Color property (probably doesn't exists). You need to change your desired properties for your style.

Another Update (Suggestions): You can easily implement it with click events like Akansha's but I should advice you about using (ToggleButton)sender instead of sender as ToggleButton because it's faster (go here). Also you may want to use lambda instead of methods.

Community
  • 1
  • 1
Thus Spoke Nomad
  • 2,372
  • 4
  • 17
  • 34
  • Hi @MonoLightTech, thanks for your reply, I'm not quite understand it. In foreach loop, is it I need to set all deactive properties including b1 (if let say b1 is clicked), then I set set the active properties outside the foreach loop? – YWah Jul 17 '15 at 09:27
  • @YWah I don't know what are your properties so I commented in blocks. You should set your properties. For example, if Button has _color_ property you should have 2 different colors. One for active, another one for deactive. First, set all buttons deactive. Just make it like `btn.Color = deactiveColor` and later set active button (by index) like `btn.Color = activeColor`. I'll update my answer with this example now. (I repeat I'm guessing a property, I'm not sure what your Button class has) – Thus Spoke Nomad Jul 17 '15 at 10:08
1

Use only one button click event for all ToggltButtons b1,b2 and b3. Set all the buttons in the array to normal and then when change only the togglebutton fontweight to bold which is clicked

ToggleButton[] btnArray = new ToggleButton[] {b1, b2, b3};


  private void btn1_Click_1(object sender, RoutedEventArgs e)
    {
        foreach (ToggleButton item in btnArray)
        {
            item.FontWeight = FontWeights.Normal;
        }
        ToggleButton tb = sender as ToggleButton;
        tb.FontWeight = FontWeights.Bold;
    }
Akansha
  • 933
  • 7
  • 18
  • Hi @Akansha, is your solution similar to @MonoLightTech? – YWah Jul 17 '15 at 09:27
  • In that solution you need to maintain index for the toggleButton clicked. It will be an overhead. Instead you can directly use the sender object and change the 'FontWeight' property of a 'ToggleButton'. – Akansha Jul 17 '15 at 09:32
0
void button_press(object B)
{
   m=list of buttons

   for i running from 0:m 
      if (i=B)
         i.FrontWeight=FrontWeights.Bold;
      else 
          i.FrontWeight=FrontWeights.Light;
}

Make a function like the above , pass the button as an object and run a loop through all the buttons .Use an if condition to get the required button to be "BOLD " and the remaining to be "LIGHT"

Ralt
  • 2,084
  • 1
  • 26
  • 38