Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: VB.NET
Expertise: Intermediate
Jul 16, 2004



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Synchronize Color Changes Between Controls in a Windows Form

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's BackColor. Being bound to Button1's BackColor, the BackColor of Button2 and Button3 will sycnchronize to Button1's BackColor.

<font color="#008000">' Change Button1's BackColor to Blue
' Because Button2 and Button3 BackColors are bound to Button1's BackColor, their BackColor is
changed too.</font>
Me.Button1.BackColor = Color.Blue
Juli Jorgensen
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date