How do I serialize a .NET control to CODE? - c#

I want to create a .NET Form at runtime, add buttons and other controls to that (also at runtime), and then be able to generate a something.designer.cs file from that form (which can then be added to a C# solution and compiled).
What I want to do is very similar to what the WinForm designer does. But instead of having a drag/drop interface for the user, I want to dynamically build the Form/Controls myself at runtime.
I was thinking I could just reuse what the WinForm designer is doing.
Is that possible?

This MSDN magazine article should have everything you need.

It's really not as simple as it was pre-.NET as the visual version of the form you see in Visual Studio is actually the result of multiple files.
But in the simplest form you could simply just mirror what .NET does at the start of creating a new form:
Create three files Form.cs, Form.Designer.cs and Form.resx (which is an XML file).
Place the same default content in them that VS does
Mimic the code generated when adding controls, code-behind and resources
It will be a tedious task, but it can be done. Adding resources however will be burdensome.

Yes, you can do achieve this using Compiler Services (compiling c# code) or Emit class if you know building correct MSIL.

Related

Convert WinForms design to XML

I'm building a generic survey engine where end users can generate and design their own surveys (including UI design). I'm interested in building (or perhaps even buying) a custom WinForms designer which would be able to output design in XML format. I'd then save the XML to database and when users load a survey, XML would be parsed and the form would be dynamically generated based on the XML data.
Building my own C# to XML converter is an option, but I'd like to avoid it since users would have to use Visual Studio to get ahold of WinForms code and then run a custom tool to generate XML output, which is a pretty clumsy solution (besides I can't force customers to buy VS).
I'd appreciate it if anyone could point me in the right direction.
Thanks!
The bits of the WinForms Designer are exposed by the .NET framework: Design surface, toolbox, property grids,...
This MSDN article and this CodeProject article should help you get started.
Of course, this is only the start of the story.
Your second problem is to make the designer emit XML rather than C#/VB code. Last but not least, you'll have to create the form at runtime, which is usually done by compiling the code generated by the designer. Needless to say that compiling XML is another story.
It would certainly be easier to store the C# code in your DB rather than some XML model. You can then compile it programmatically at runtime (no need to have VS installed on the client's computer. The .NET runtime is enough) and instantiate the form class dynamically. BTW, whether you complie the code in the designer or in runtime is up to you.
However, other routes may be easier, such as Hans' suggestion: InfoPath.
The Mono project has a Winforms Designer. I don't know how easy it would be to re-use as a componant. Here it is in action:
I don't know how you would go about serializing the output to XML and deserializing it again. Maybe you could even store the C# in your database?
Check this post on how to create xml files in C#: Working with XML
Then use sp_xml_preparedocument/sp_xml_removedocument in SQL to create/parse xml files (use stored procedures to do this)
Traverse and use the generated xml in your winform (I suggest you use LINQ to XML to do this)
Probably the hardest thing you'll encounter here is the creating xml part and traversing the xml nodes, but aside from that everything will be a breeze.
We're already implementing this structure/architecture at work but on webforms not winforms. But I think the flow/logic is the same. HTH.
Edit: This post might help you. Cheers.

How to re-use ASP.NET .aspx or .ascx files?

I know if someone wants to re-use some classes (not UI), he must gather all of them and put in a Visual Studio Class Library, build it to some dells and distribute these dlls. In this approach there just one code, you just update code in one place.
But what about ASP.NET's markups? For example you have an .ascx file or a collection of .aspx files regarding user management. If I want to use them in another project I am forced to copy them in new project again. By this I have two same code that is very hard to maintain.
So is it any way to re-use .ascx and .aspx files just like simple .dlls? For example building them?
Many Thanks,
Afshar Mohebbi
With the default configuration, .ascx and .aspx files will need to exist on disk, because they need to have a path associated with them for everything to work. All the code (everything but the first line which specifies which class to inherit) in them, however, can be compiled away into a DLL file. It would probably be possible to get around this by writing custom handlers and build providers that load things from DLLs, but it's not worth the effort.
If you want to put your user controls into a DLL file, create them as custom controls instead of user controls (.ascx files). That's how all the custom control libraries for sale around the 'net are done.

C# Templates for Visual Studio 2005

I'm looking for some template examples, such as a C# windows dialog.
Do many hackers take advantage of template files?
There are times when I'm creating dialog forms that are very similar. I want to make all my forms look and behave consistently, without doing a lot of coping and pasting.
Add New Item -> My Templates
Here's an article on creating your own Project and Project Item templates:
Create Reusable Project And Item Templates For Your Development Team
Much cooler, but not exactly what you asked for is T4 (text template transformation toolkit) that allow you to do intelligent code generation during builds. Take a look at this MSDN article for more info:
Generating Artifacts By Using Text Templates
Make the template yourself, from the first dialog you made. This will avoid the problem of having to search for a template that does exactly what you want.
There are lots of C# code snippets around. They will provide scaffolding for different structures (properties, dispose pattern and more).
Here is one source.

ASP.NET Web User Control Library

We have a bunch of user controls we would like to pull out of a web application and into a separate assembly/library, and I thought it would be as simple as creating a class library and pulling the ascx and ascx.cs files into the project, and compiling a DLL to be reused among our applications.
This was not the case, however.
Our ultimate goal is to have a single distributable DLL (similar to how Telerik distributes their controls) that we can throw into any web application. The steps here: Turning an .ascx User Control into a Redistributable Custom Control were very simple to follow, however this results in many files named controlname.ascx.guid.dll, which is not the desired result. I couldn't even get these to work anyways, since we have additional classes that need to be compiled into the assembly.
Has anyone successfully created a web user control library in .NET (we're using 3.5 here)? I can't seem to find a nice step-by-step guide.
I realize this is an old topic, but if anyone is looking for a solution for creating reusable user control libraries, it turns out it's fairly simple. Here are two good step-by-step guides along with source code:
From MSDN: Turning an .ascx User Control into a Redistributable Custom Control
From Code Project: Straight way to create ASP.NET user controls library
The second link provides a solution to the multiple dlls created by the first link.
Edit- (2) Seems to be a dead link. Here's the new link
https://www.codeproject.com/Articles/30247/Straight-way-to-create-ASP-NET-user-controls-libra
If you want to share controls among project, my experience has shown that the best way is to build custom asp.net server controls instead of usercontrols. User controls are good for sharing within the same project, but not over multiple ones.
For this purpose I suggest you to build a set of custom server controls inside a class library and use that on all of your projects.
This book does quite a good job at explaining the basics of creating server controls
Edit:
I'm currently developing a .net web server control library. I actually didn't follow any step-by-step guide. I mostly considered using the book I mentioned above and the MSDN library + Reflector, which is a great tool for inspecting existing MS server controls and learning from them.
I found the tutorial Creating and Using User Control Libraries but it seems like a bit of a hack as it relies on a post-build command line event to copy the user controls from one project to another.
Somewhat late, I admit.
To create a re-usable library of user controls; create a new Web Application Project, delete all the scaffolding, add a (number of) user control(s). Create a Web Deployment Project from the Web Application Project, in the WDP properties choose the option to Merge all control output and assign a name for the library and ensure that Allow this website to be updatable is NOT checked.
Build the WDP and use Reflector to examine the generated library; you'll see that it contains an ASP namespace and the types you carefully crafted have been renamed i.e. usercontrol_ascx. In your target website(s) add references to BOTH the output dlls from your WDP, add a system.web/pages/controls node to web.config using the namespace ASP and the name of the assembly that you defined in the WDP.
Now when you use the library in a page (for example) you must use the alias that you defined in web.config and the typename as seen in Reflector i.e.
<ucl:usercontrol_ascx ... />
I found it useful to add a dependancy for the website(s) on the WDP so that the WDP is built before the websites; now I can change the user controls in the WAP without having to remember to build the WAP before building the website(s).
I hope that someone finds this useful as it cost me a few grey hairs getting to this stage and still have VS do its 'automagically' thing.

Generic ServerControl syntax?

Is there a way that I can have a server control
MyControl<T>
so that I can register and use it in an aspx page like so
<mc:MyControl<ThingForControlToUse> ID="instanceOfMyControl" runat="server"
Obviously the designer doesn't like this, are there any cool ways round it other than creating a non generic wrapper with a type parameter?
Generic Tag Names are not possible in ASP.NET, please refer to this article: Generic Controls
You are on the right track in thinking that you will need to create a wrapper with a TYPE parameter.
A quick note - I am not sure if this is true in Visual Studio 2008 but if you were to have that control in a project that you were editing with Visual Studio 2005 you would completely break intellisense with that control. When Visual Studio attempts to create XML schema files to use for intellisense popups in markup files it completely breaks when it sees generic controls like this one and it abandons the schema file completely leaving you with no intellisense for any controls in that assembly.

Categories