0

I have this project that has quite a number of winforms and how do I change button background color or text color or any other attributes from another winform? I'm currently using delegate and this is not efficient as I will need to create a new delegate everytime I want to change a new attributes. Is there any better way to do it?

Thanish K
  • 37
  • 5
  • 2
    While not exactly what you asked for, the concept is the same - See https://stackoverflow.com/questions/4822980/how-to-access-a-form-control-for-another-form – NoChance Apr 19 '21 at 04:17

1 Answers1

1

In the Designer, click on your button

Ensure that GenerateMember is true and Modifiers is public. Name the button using PascalCase, for example CancelButton

Now you can in some other form:

var formWithButton = new FormX();
formWithButton.CancelButton.BackColor = Color.Black;

If you have a large number of changes to make, perhaps consider a theming framework instead

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
  • Thank you for the answer. But what do you mean by theming framework? I might have quite a lot of changes to make. Also, how do I know BackColor stands for Background Color? What if I wanna change the font size or text? Then how do I name it or is there any documentation for it? – Thanish K Apr 19 '21 at 05:07
  • Theming: applying a set of visual stylings to an application to change its appearance. Winforms isn't great for it, WPF is better, but things have been created to do it in winforms eg visualstyler plugin for VS. BackColor sets the background color, though how I come to know this I don't really remember - I probably looked in the Designer properties grid about 23 years ago when I used VB6. Font is controlled with the Font property. Microsoft publishes a huge and comprehensive set of documentation called MSDN https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.control.backcolor – Caius Jard Apr 19 '21 at 05:16
  • The FormX() is the form that I would like to do changes to or the form that I'm currently doing changes at? – Thanish K Apr 19 '21 at 05:32
  • Both, I think. The question isn't very clear – Caius Jard Apr 19 '21 at 07:54
  • Tested it, it is not working.. I tried putting into a click event and the Background color didnt change – Thanish K Apr 19 '21 at 08:44
  • Tried putting *what* exactly? – Caius Jard Apr 19 '21 at 11:48