locked
Color buttons: Toggle a color button RRS feed

  • Question

  • Hi,

    I have a question related to toggle 2 different color within a button. Whenever i click on the button, it won't change to red "OFF" (the default is green "ON"), below is the code. Is there anything wrong?

     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            With Me.Button1
                If .BackColor = Color.Green Then
                    .BackColor = Color.Red
                    .Text = "OFF"
                Else
                    .BackColor = Color.Green
                    .Text = "ON"
                End If
            End With
    
    
    
        End Sub
    End Class
    Tuesday, October 13, 2009 4:39 PM

Answers

  • You put this code into the Load event of the form. So it will only happen one time ... when the form loads.

    I think you want this to happen every time the button is clicked, correct?

    Then go to the design surface and double click on the button. That should generate the correct button click method in your code. Then put this code into the Button_Click event instead of form load.

    Hope this helps.
    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    • Proposed as answer by dekurver Tuesday, October 13, 2009 4:52 PM
    • Marked as answer by Jeff Shan Friday, October 16, 2009 6:41 AM
    Tuesday, October 13, 2009 4:47 PM
  • You're running the code when the form loads rather than when the button is clicked. You should handle the button's Click event instead of the form's Load event. E.g.

    Private Sub ButtonClick(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        With Button1
            If .BackColor = Color.Green Then
                .BackColor = Color.Red
                .Text = "OFF"
            Else
                .BackColor = Color.Green
                .Text = "ON"
            End If
        End With
    End Sub
    • Marked as answer by Jeff Shan Friday, October 16, 2009 6:41 AM
    Tuesday, October 13, 2009 4:50 PM

All replies

  • You put this code into the Load event of the form. So it will only happen one time ... when the form loads.

    I think you want this to happen every time the button is clicked, correct?

    Then go to the design surface and double click on the button. That should generate the correct button click method in your code. Then put this code into the Button_Click event instead of form load.

    Hope this helps.
    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    • Proposed as answer by dekurver Tuesday, October 13, 2009 4:52 PM
    • Marked as answer by Jeff Shan Friday, October 16, 2009 6:41 AM
    Tuesday, October 13, 2009 4:47 PM
  • You're running the code when the form loads rather than when the button is clicked. You should handle the button's Click event instead of the form's Load event. E.g.

    Private Sub ButtonClick(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        With Button1
            If .BackColor = Color.Green Then
                .BackColor = Color.Red
                .Text = "OFF"
            Else
                .BackColor = Color.Green
                .Text = "ON"
            End If
        End With
    End Sub
    • Marked as answer by Jeff Shan Friday, October 16, 2009 6:41 AM
    Tuesday, October 13, 2009 4:50 PM