c# - How to set variable color for winforms color property -
i'm begginer , think selution simple cant find on internet.
i'm looking way of setting color of forms , controls color variable if change value green exapmple, every control color set maincolor turn green , changes show in designer.
public class mainform:form { public static color maincolor=[some color]; public static color seccolor=[some color]; public main() { initializecomponent(); backcolor=maincolor; control1.backcolor=seccolor; control2.backcolor=seccolor; control3.backcolor=seccolor; } }
like changing maincolor , seccolor controls changed wont show in designer.
right way of doing it?
, sorry english.
yes, called databinding, , it's done partly designer , partly code.
instead of declaring
public static color maincolor=[some color];
declare property:
public color mycolor { { return mycolor; } set { mycolor = value; } }
in main form, edit each of controls want influenced variable bind color property it. i'll use panel sample control, add panel objects form. in each panel object, in properties panel, click + next databindings, click in empty box next (advanced). click ellipsis (...) , select backcolor property. then, under binding dropdown, select add project data source. select object in next dialog , navigate form, , select that. list of properties appear in formatting , advanced binding dialog box , can select property mycolor.
note once have created data source first one, won't need recreate each panel - reference 1 created already.
you can change backcolor of of controls did databinding on changing value of mycolor property in program. change together.
you need arrange property broadcast message has changed, add line set() method.
form1bindingsource.resetbindings(false);
so set method looks this.
set { mycolor = value; form1bindingsource.resetbindings(false); }
that tells binding source object notify subscribers update themselves, , happen automatically every time mycolor property changed.
Comments
Post a Comment