2

In a windows form, I want to replace all this forms' buttons with a custom button, CustomNamespace.controls.customButton, which as you guess, extends the Button class from system.windows.controls.button.

Currently, the only proper way I can think of doing it is deleting the original button, adding in the customized one and manually changing the properties one-by-one. However, this becomes a hassle when you need to replace more than one button with different properties or events.

Another way that I can think of doing while keeping its properties intact is replacing ... = new system.windows.controls.button() to ... = new CustomNamespace.controls.customButton() in the .designer.cs file. As much as possible, I would prefer not to go that way unless there are no other better solutions.

Community
  • 1
  • 1
John Evans Solachuk
  • 1,953
  • 5
  • 31
  • 67
  • 3
    In your case, you can safely ignore the advice against editing the .Designer.cs file. If all you're doing is replacing the type of the control, and if you make sure you change both the Designer-generated field and the instantiation, then the code will be just like you had dragged your custom control into the form using the Designer and it won't be overwritten back to a regular `Button` (as the cautions warn you about in the post to which you've referred). – Peter Duniho Sep 19 '16 at 03:58
  • As stated, your question is way too broad. If you've tried something and had a problem getting it to work, then please be specific about that. Otherwise, there are many different ways to approach this refactoring task. – Peter Duniho Sep 19 '16 at 03:58
  • @PeterDuniho I'm not sure how more specific I can make it be. And yes, all I wanted to do was replace the original control with a customized one while keeping its properties the same. – John Evans Solachuk Sep 19 '16 at 05:13
  • _"I'm not sure how more specific I can make it be"_ -- well, you could show the relevant code. You could explain what you've already tried with respect to trying to address your problem. You could explain what specific problem you ran into when doing that. See also [ask] for more advice on how to present your question in a clear, specific, and answerable way. – Peter Duniho Sep 19 '16 at 06:02
  • I had to do the same not so long ago. The easiest way is to open the designer.cs file, do your replacing. Wait for VS to show any error (properties that dont exist anymore for example) and fix them. Its as simple as that it only takes lots of time. – GuidoG Sep 19 '16 at 06:59
  • 2
    In fact the linked answer is extremist (if we don't say it's wrong). *Usually* you should not change the designer.cs file or any other auto-generated file, because your changes *may* be overwritten, or you may introduce some problems in the file. Even in the case which is asked in the linked question, changing the `Dispose` method has no problem and your changes will be preserved. Also for such changes which you asked about in current question, the best option is using find/replace which changes the designer.cs without any problem. – Reza Aghaei Sep 19 '16 at 07:22
  • 1
    It's like changing Registry. If the job get done with enough knowledge and enough caution (and if needed creating backup before change) there is no problem. – Reza Aghaei Sep 19 '16 at 07:33
  • You could always do a recursive loop through the controls on the page - if button, create new customButton and transfer properties using reflection, then drop the button and add the customButton. Not the world's most convenient way, but certainly solid and not a problem if you're talking in the realm of a few to tens of controls per page load. – Shannon Holsinger Sep 19 '16 at 10:04

1 Answers1

2

After advised by @Peter Duniho, the best option I have is to just find and replace the existing component in .desginer.cs files because it will have the same effect of deleting the existing and adding the customized component.

Thanks also to others in the questions' comments section above for their input.

John Evans Solachuk
  • 1,953
  • 5
  • 31
  • 67