views:

192

answers:

3

I have a form with a split panel. In the one split are a group of buttons which I want to programmatically change the color of the last pressed button. The following loop seems to run correctly and set the colors correctly but the form doesn't represent that. Once the loop is completed and I recheck the button colors, they revert to previous state.

For Each formControl As Control In Me.FormSplitContainer.Panel1.Controls
  If formControl.GetType() Is GetType(Button) Then
    If CType(sender, Button) Is CType(formControl, Button) Then
      CType(sender, Button).BackColor = Color.White
    Else
      CType(sender, Button).BackColor = System.Drawing.SystemColors.ControlDark
    End If
  End If
Next

I can get the desired effect by doing the below code but seems less elegant and would obviously require updates as buttons would be added or removed.

DataFeedButton.BackColor = System.Drawing.SystemColors.ControlDark
IncentiveButton.BackColor = System.Drawing.SystemColors.ControlDark

CType(sender, Button).BackColor = Color.White

Anyone see what I am missing?

A: 

Where are you calling this code? Each time they click a button? To me, it looks like it sets the backcolor of all the form's buttons to white. I don't see where you're testing for the 'last button pressed' condition.

Beth
You are correct. I am attempting to run the loop each time a button is clicked. sender is the button that was clicked. Sorry for the confusion.
Ikey
Instead of looping through the whole collection, can you save the prev button in a module-level var so when another is clicked, you know exactly which to reset to controlDark?
Beth
Ya this goes along with what Kyralessa said. Just all the buttons to default and set the clicked one to white. It would cover both the issues that Beth and Kyralessa are rasing.
kralco626
+1  A: 

Assign sender to a button variable, then assign the color.

dim b as button

And then in the loop, assign it this way:

b = sender
b.backcolor = color.white
xpda
Not sure exactly why but adding this inside the loop allows the code work as expected. Thanks
Ikey
I'm not sure exactly why cType didn't work on the left side of the assignment statement, but it's safer to assign Sender to a variable of the proper type if you're going to make changes to its properties.It's possible that the Ctype was optimized outside of the loop to a copy of the button object so the property change didn't affect the original button. Using cType as an assignment target generally seems a little unreliable.I'm not sure why you use formControl part of the time and Sender part of the time. That might be a (or the) problem.
xpda
A: 

Wow, that's some confusing code. Why not just set all the buttons to the default color, then set the current one to white? Something like this:

Private Sub anyButton_Click(ByVal sender As Object, ByVal e As EventArgs) _
      Handles btnWhatever.Click [etc. for all buttons]
    For Each c As Control In Me.Controls
        Dim button = TryCast(c, Button)
        If button IsNot Nothing Then button.BackColor = SystemColors.ControlDark
    Next
    Dim thisButton = TryCast(sender, Button)
    If thisButton IsNot Nothing Then thisButton.BackColor = Color.White
End Sub
Kyralessa