Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
The designer in VisualStudio generates code for various GUI objects, like this:
C#
. . .
this.textBox.MaxChars = 1000;
this.textBox.Name = "textBox";
this.textBox.Size = new System.Drawing.Size(168, 235);
. . .

Is there anyway in my application to access this functionality? That is, pass an object to some magical method and get in return an array of strings containing the code to generate the object?

I know I could do this myself with lots of calls to the reflection classes, but I'm hoping that this is an already solved problem. This is for C# 2.0 (VS 2005).

Why? (in response to the requests for clarification):

One of my recently submitted projects to CodeProject (the Spiral TrackBar control) includes an app (SpiralTrackBarExplorer) which allows you to set all of the members for the control and see the results interactively and play with the control.

It would be nice to have a "generate code for this control" button, which could be then cut-and-paste into the user's application.

Many thanks in advance,
Graham
Posted
Updated 25-Feb-14 16:51pm
v2
Comments
Sergey Alexandrovich Kryukov 25-Feb-14 17:57pm    
Why? why?!
—SA
Graham Wilson 25-Feb-14 22:52pm    
I've updated my question and hopefully answered yours.
BillWoodruff 25-Feb-14 20:20pm    
To reply to your post meaningfully, I'd need to know more about the scope of what you want to achieve. Are you talking of creating a visual ui with toolbox, drag-drop ? Or, are you talking about something much more modest ?
Graham Wilson 25-Feb-14 22:53pm    
I'd added a clarification to my question.

1 solution

Yeah, it's solved, inside Visual Studio. The Designer is what does this "magic". There's an old MSDN article on doing it here[^]. It's FAR from a trivial thing to implement!

[EDIT]
In your updated case, the designer solution would be WAY over the top. What you have to do is keep track of those values in your control, which you're already doing!, and then just emit a formatted string that contains the standard code for setting those values. There's no reflection required to do that.

It's really as simple as:
StringBuilder sb = new StringBuilder();
sb.AppendFormat("\t{0}.SomeProperty = {1}", controlName, somePropertyValue);
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 25-Feb-14 17:58pm    
Interesting enough, my 5. If OP really needs it seems questionable to me... :-)
—SA
Dave Kreskowiak 25-Feb-14 22:34pm    
I'm not 100% convinced it's what he needs either, but that wasn't really spelled out in the original post. It was more of a "hey, I want this end result" instead of "this is the overall goal. Is this going to be an appropriate solution?" kind of thing.
Graham Wilson 25-Feb-14 22:59pm    
Thanks for your solution; I've updated the original question to better explain my end goals.
Sergey Alexandrovich Kryukov 25-Feb-14 23:07pm    
So typical...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900