jacob Denis jacob Denis - 4 months ago 12
Vb.net Question

Cannot change the back color of a button via a custom method

Here is my code that should change the back color of any of my buttons, but it doesn't work and I don't have any idea what's wrong:

Public Sub color(ByVal backcolor As System.Drawing.Color)
backcolor = Drawing.Color.CadetBlue
End Sub


And here is how I call it when a button was clicked. It should change the back color of
Button1
, but it doesn't. Am I doing it right or am I missing something?

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
color(Button1.BackColor)
End Sub

Answer
Public Sub color(ByVal btn As Control, ByVal clr As System.Drawing.Color)
    btn.BackColor = clr
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    color(Button1, Drawing.Color.CadetBlue)
End Sub

Or

Public Sub color(ByVal btn As Control)
    btn.BackColor = Drawing.Color.CadetBlue
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    color(Button1)
End Sub

That works with any control (Textboxes, Labels e.t.c)

Comments