This tip shows how binding Windows Forms controls to each other can reduce the code and logic needed to synchronize color changes between controls in a Windows Form. This technique can also be used to implement 'themes' for Windows forms.
CODE EXAMPLE: Keep the BackColor of three Button's sychnronized through bindings. Three buttons are used in the example; Button1, Button2, and Button3.
1. Set the BackColor of Button1 then bind Button1's BackColor to Button2 and Button1's BackColor to Button3.
<font color="#008000">' Set the BackColor property of Button1 to LightBlue.</font>
Me.Button1.BackColor = Color.LightBlue
<font color="#008000">' Bind the BackColor property of Button2 to Button1's BackColor
Me.Button2.DataBindings.Add("Backcolor", Me.Button1, "BackColor")
<font color="#008000">' Bind the BackColor property of Button3 to Button1's BackColor
propery.</font> <font color="#008000">
Me.Button3.DataBindings.Add("Backcolor", Me.Button2, "BackColor")
2. To change the BackColor
of all three buttons at the same time change Button1
. Being bound to Button1
, the BackColor
will sycnchronize to Button1
<font color="#008000">' Change Button1's BackColor to Blue
' Because Button2 and Button3 BackColors are bound to Button1's BackColor, their BackColor is
Me.Button1.BackColor = Color.Blue