Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
How to create an application to generate design suface by using .Designe.cs file or .resx file ?
I do it normally using service:
C#
public Form1()
		{
			InitializeComponent();
			Initialize();
		}
		private ServiceContainer serviceContainer = null;

		private void Initialize()
		{
			IDesignerHost host;
			Form form;
			IRootDesigner rootDesigner;
			Control view;

			// Initialise service container and designer host
			serviceContainer = new ServiceContainer();
			serviceContainer.AddService(typeof(INameCreationService), new NameCreationService());
			//	serviceContainer.AddService(typeof(IUIService), new UIService(this));
			host = new DesignerHost(serviceContainer);

			// Start the designer host off with a Form to design
			form = (Form)host.CreateComponent(typeof(Form));
			form.TopLevel = false;
			form.Text = "vindana";

			// Get the root designer for the form and add its design view to this form
			rootDesigner = (IRootDesigner)host.GetDesigner(form);
			view = (Control)rootDesigner.GetView(ViewTechnology.WindowsForms);
			view.Dock = DockStyle.Fill;
			pnlLoadDesing.Controls.Add(view);

			// Subscribe to the selectionchanged event and activate the designer
			ISelectionService s = (ISelectionService)serviceContainer.GetService(typeof(ISelectionService));
			s.SelectionChanged += new EventHandler(OnSelectionChanged);
			host.Activate();
		} 

but I want to do this using .Designe.cs file or .resx file of any windows form application.

Thanks
Posted
Updated 11-May-11 19:09pm
v2

1 solution

In order to build a form,you need to have form.cs and form.Designer.cs files.
Do not bother about resources(resx) file.

  • Create an application that writes code into a TextWriter,the file should be written in such a way that you declare a control and add the control,set properties to the form and add it to the form.

For example:
textWriter.WriteLine("using System;");
textWriter.WriteLine("using System.Collections.Generic;");
textWriter.WriteLine("using System.ComponentModel;");
textWriter.WriteLine("using System.Data;");
textWriter.WriteLine("using System.Drawing;");
textWriter.WriteLine("using System.Linq;");
textWriter.WriteLine("using System.Text;");
textWriter.WriteLine("using System.Windows.Forms;");
textWriter.WriteLine("using System.IO;");
textWriter.WriteLine("namespace MyFramework.Gui.LoadItems");
textWriter.WriteLine("{");
textWriter.WriteLine("    public partial class Form1 : Form");
textWriter.WriteLine("    {");
textWriter.WriteLine("        public Form1()");
textWriter.WriteLine("        {");
textWriter.WriteLine("            InitializeComponent();");
textWriter.WriteLine("        }");
textWriter.WriteLine("}");


  • A better option for creating a form builder is to read the schema of the database that we are using and according to the datatypes used in the database use corresponding controls in the form.For eg,if 'Name' is a column in table of type varchar, add a corresponding textbox with name txtName and a label lblName to the form and set properties for both controls.The various properties like top,Left etc can be set in the designer code for placing the controls properly.

  • The same procedure can be used for building form.cs and inherit from Form class,make both classes partial.

  • Now , if you add both designer and form.cs to a project they will work normally as a form built using toolbox and manual coding.


If you have any doubts please ask me.
 
Share this answer
 
v4
Comments
vindanak 18-May-11 23:46pm    
can u give me a working sample
thanks

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