I want to be able to add code in a Form's ctor (or Form_Load
etc.) and have that reflected in the designer. Is there any way to do that? E.g. when I add BackColor = Color.Green;
in the Form's ctor - the Form does not turn green. I would like it to.
Asked
Active
Viewed 52 times
0

ispiro
- 26,556
- 38
- 136
- 291
-
What have you tried so far? Please post the code. – Rufus L May 23 '17 at 21:34
-
@RufusL It's in the question. – ispiro May 23 '17 at 21:34
-
What @RufusL is asking, is for you to show a [MCVE] demonstrating what you have tried. – t0mm13b May 23 '17 at 21:35
-
@t0mm13b OK: Create a new Winforms project, and add the line in the question to the ctor. Then open the designer (the mode, not the file) and see that the Form is not green. – ispiro May 23 '17 at 21:37
-
@RufusL See my previous comment (to t0mm13b). – ispiro May 23 '17 at 21:38
-
Read this [MSDN](https://msdn.microsoft.com/en-us/library/ms973818.aspx) on how this is done. By using a custom code serializer for forms designer at design time, one can create a custom `InitializeComponent` method that will do just that. – t0mm13b May 23 '17 at 21:43
-
The designer doesn't run the code which you put in constructor. Read [this post](https://stackoverflow.com/a/32299687/3110834) to learn more about how the designer works and take a look at the interesting example in the post to get better understanding. – Reza Aghaei May 23 '17 at 21:48
-
@t0mm13b Before I go on a wild goose chase and read that 3k word page, I'd like to verify that you know that `InitializeComponent ` already exists (it's created by the default template) and is simply a method called by the Form's ctor. You know that, right? So what is the crux of the difference between that and the rest of the ctor? – ispiro May 23 '17 at 21:48
-
@RezaAghaei Perfect. That explains a lot. Is there a way to run arbitrary code _without_ messing around with `Designer.cs`? I wouldn't want to jeopardize anything. – ispiro May 23 '17 at 21:51
-
*Before I go on a wild goose chase and read that 3k word page* that sums it all up... unfortunately, you don't learn by short cutting it and asking for how to do it without understanding. – t0mm13b May 23 '17 at 21:53
-
@t0mm13b Of course. I was just verifying that you understood me and was answering to the point. It is _quite_ common here to get advice where the advisor misunderstood the question. – ispiro May 23 '17 at 21:56
-
@ispiro If you want to have some code which run during design time of your form, you can put them in constructor of the base class of your form. Also if you really want to manipulate designer generated code which is not recommended, you can change `Form1.Designer.cs` file or move its content to `Form1.cs` and manipulate it. – Reza Aghaei May 23 '17 at 21:57
-
Is this question related to one that you posted previously [about an hour ago](https://stackoverflow.com/questions/44144698/add-child-to-custom-control-in-designer-mode) which is part of your "wild goose chase"? – t0mm13b May 23 '17 at 21:59
-
@RezaAghaei As we both agree, it's best to leave `Designer.cs` alone. As for your first idea - how would I go about adding code to the base clase? `Form1`'s base class is `Form`, and is not in my hands, it's been written by MS. – ispiro May 23 '17 at 22:00
-
I'm not sure about why you want to do that, but just for learning purpose, create a `public MyBaseForm:Form{}` class and in your `Form1`, derive from it. This way, all codes which you write in constructor of the base class will be run in design time of `Form1`. Just keep in mind that you need to build the project to see changed in `Form1` designer. See this other [post](https://stackoverflow.com/a/33535154/3110834) as an example. – Reza Aghaei May 23 '17 at 22:06
-
Read both linked posts and examples, you may find both of them useful. – Reza Aghaei May 23 '17 at 22:07
-
@RezaAghaei Thanks again. You have been of much help. You can also post an answer so that I can upvote you. My goal is to have near-designer ease of designing a Form using custom controls. In my search for complete design time support for them (including being able to drop a Control on a custom control that is on my Form) I've found that to be pretty difficult to achieve. So this is "plan b" - to edit the ctor, and immediately see the result without having to run the app. – ispiro May 23 '17 at 22:11
-
It's better to follow Windows Forms rules. Usually in a windows forms application, users avoid code based UI design and they like using designer, property grid and property pages. So you can add a Property, a Designer Verb, a Smart Tag (Designer Action), a Property Tab and so on to make working with your control easier. – Reza Aghaei May 23 '17 at 22:19
-
Thank you for your kind offer to post an answer. Since your question is general I prefer to keep my recommendations as comment. You can vote for the linked posts if you liked them :) – Reza Aghaei May 23 '17 at 22:22
-
@RezaAghaei Upvoted both. About the "Designer Verb" etc. - unfortunately, that won't help with adding Controls to my custom control that is on the Form. At least not easily. My problem is not the custom control's location, size, etc. but rather adding and editing the properties of a button (for example) that I want to put on it (-on the custom control). – ispiro May 23 '17 at 22:33
-
Thanks, Let's suppose you have created a `MyPanel:Panel` class. Then what's your question exactly? Please describe what do you want to do exactly and step by step :) – Reza Aghaei May 23 '17 at 22:38
-
@RezaAghaei I add `myPanel1` to `Form1` (this I know how to do, even in the designer), and now I want to add `button1` to `myPanel1`, see how it looks, and move `button1` around if needed. – ispiro May 23 '17 at 22:48
-
Since `MyPanel` is derived from `Panel` then when you drop an instance of it o the the form, the designer of `myPanel1` is enabled and you can simply drop a `Button` on it and move and change `button1` properties. You don't need to do anything to gain this. – Reza Aghaei May 23 '17 at 22:53
-
@RezaAghaei That's what I thought. But as i wrote in my question - that is not the case. As soon as the designer window is closed and reopened - anything you dropped on myPanel1 disappears. – ispiro May 23 '17 at 22:58
-
You can test what I said simply using this class `public class MyPanel : Panel { }`. I saw your previous question too. I could't reproduce the problem. It's better to post a code to reproduce the problem. – Reza Aghaei May 23 '17 at 23:00
-
@RezaAghaei I just created another project and it works as expected there. I'll have to investigate this further. (though perhaps tomorrow.) Thanks again. – ispiro May 23 '17 at 23:15