0
votes

I have a quiz application where there can be up to 3 possible answers. The answers are buttons which have been assigned a style. All the answer buttons have the same click event...

Private Sub butAnswer_Click(ByVal sender as Object, ByVal e as System.Windows.RoutedEventArgs)
    'An Answer has been clicked

    Dim butC as Button
    Dim butS as String = TryCast(sender, Button).Name.ToString()
    'Cycle through all answers and set style
    For x = 1 to 3
        butC = DirectCast(FindName("butAnswer" & x), Button) 
        If butS = "butAnswer" & x.ToString Then
            butC.Style = DirectCast(FindResource("GlassButtonSelected"), Style)
        Else
            butC.Style = DirectCast(FindResource("GlassButton"), Style)
        End If
    Next

End Sub

I now want to expand on this so that if the user clicks a second time on a 'selected' answer the style is reverted back to 'GlassButton'

If butS = "butAnswer" & x.ToString Then
'Check what style the button has
   If 'style is GlassButton' Then
      butC.Style = DirectCast(FindResource("GlassButtonSelected"), Style)
   Else
      butC.Style = DirectCast(FindResource("GlassButton"), Style)
   End If
Else
   butC.Style = DirectCast(FindResource("GlassButton"), Style)
End If

My Question is how do I determine what style the button has?

ADDITIONAL UPDATE

Kent, Thanks, I'm new to wpf and did not know about all these extra controls... I've now converted my Buttons to ToggleButtons and have a single checked and unchecked event that is used by all ToggleButtons. I've amended my style so that the target is ToggleButton and I now just have the one. I've got rid of the style I was going to use to indicate a selected answer - basically a change in background colour. My Checked event has...

Private Sub togAnswer_Checked(ByVal sender as Object, ByVal e as System.Windows.RoutedEventArgs)
    'togAnswer was clicked....
    Dim togC as ToggleButton
    Dim togS as String = TryCast(sender, ToggleButton).Name.ToString()
    'Cycle through all answers and set style
    Dim bc = New BrushConverter()

    For x = 1 to 3
        togC = DirectCast(FindName("togAnswer" & x), ToggleButton) 

        If togS = "togAnswer" & x.ToString Then
                            'Set ToggleButton to selected colour
            togC.Background = DirectCast(bc.ConvertFrom("#AAFF8020"), Brush)
        Else    
            'Set other ToggleButtons to unselected colour
            togC.Background = DirectCast(bc.ConvertFrom("#AA000000"), Brush)
        End If
    Next
End Sub

Unfortunately the button colour does not change. Any idea why?

1

1 Answers

3
votes

In short, you're doing it wrong. You need to identify the behavior (a button that is in one of two states) and distinguish it from the visual manifestation of that behavior. Doing so would lead you to use a ToggleButton and simply alter its appearance based on its IsChecked property.