Click here to Skip to main content
15,895,833 members
Articles / Web Development / ASP.NET

Converting WinForms => Web Forms using CodeDom

,
Rate me:
Please Sign up or sign in to vote.
4.82/5 (40 votes)
10 Feb 2005CPOL8 min read 273.9K   14.3K   139  
Introducing a workable approach to converting Windows Forms to ASP.NET Web Forms.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace suite4.net.WinForms2WebForms
{
	#region XML comment
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	#endregion
	public class Form1 : System.Windows.Forms.Form
	{
		#region private declaration
		private System.Windows.Forms.Button cmdWriteAspx;
		private System.Windows.Forms.TextBox txtEmployeeID;
		private System.Windows.Forms.Label lblEmployeeID;
		private System.Windows.Forms.TextBox txtTitle;
		private System.Windows.Forms.Label lblTitle;
		private System.Windows.Forms.TextBox txtLastName;
		private System.Windows.Forms.TextBox txtFirstName;
		private System.Windows.Forms.Label lblFirstName;
		private System.Windows.Forms.Label lblLastName;
		private System.Windows.Forms.Label lblStreet;
		private System.Windows.Forms.TextBox txtPostalCode;
		private System.Windows.Forms.Label lblRegion;
		private System.Windows.Forms.TextBox txtRegion;
		private System.Windows.Forms.Label lblCountry;
		private System.Windows.Forms.Label lblPostalCode;
		private System.Windows.Forms.Label lblCity;
		private System.Windows.Forms.TextBox txtCountry;
		private System.Windows.Forms.TextBox txtCity;
		private System.Windows.Forms.TextBox txtStreet;
		private System.Windows.Forms.GroupBox grpAddress;
		private System.Windows.Forms.GroupBox grpName;
		#region XML comment
		/// <summary>
		/// Required designer variable.
		/// </summary>
		#endregion
		private System.ComponentModel.Container components = null;
		#endregion
		#region constructor
		#region Form1()
		public Form1()
		{
			this.InitializeComponent();
		} // end public Form1()
		#endregion
		#endregion
		#region void Dispose(bool disposing)
		#region XML comment
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		#endregion
		protected override void Dispose(bool disposing)
		{
			if(disposing)
			{
				if(components != null)
				{
					components.Dispose();
				} // end if(components != null)
			} // end if(disposing)
			base.Dispose(disposing);
		} // end protected override void Dispose(bool disposing)
		#endregion
		#region Windows Form Designer generated code
		#region void InitializeComponent()
		#region XML comment
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		#endregion
		private void InitializeComponent()
		{
			this.cmdWriteAspx = new System.Windows.Forms.Button();
			this.txtEmployeeID = new System.Windows.Forms.TextBox();
			this.lblEmployeeID = new System.Windows.Forms.Label();
			this.txtTitle = new System.Windows.Forms.TextBox();
			this.lblTitle = new System.Windows.Forms.Label();
			this.txtLastName = new System.Windows.Forms.TextBox();
			this.txtFirstName = new System.Windows.Forms.TextBox();
			this.lblFirstName = new System.Windows.Forms.Label();
			this.lblLastName = new System.Windows.Forms.Label();
			this.lblStreet = new System.Windows.Forms.Label();
			this.txtPostalCode = new System.Windows.Forms.TextBox();
			this.lblRegion = new System.Windows.Forms.Label();
			this.txtRegion = new System.Windows.Forms.TextBox();
			this.lblCountry = new System.Windows.Forms.Label();
			this.lblPostalCode = new System.Windows.Forms.Label();
			this.lblCity = new System.Windows.Forms.Label();
			this.txtCountry = new System.Windows.Forms.TextBox();
			this.txtCity = new System.Windows.Forms.TextBox();
			this.txtStreet = new System.Windows.Forms.TextBox();
			this.grpAddress = new System.Windows.Forms.GroupBox();
			this.grpName = new System.Windows.Forms.GroupBox();
			this.grpAddress.SuspendLayout();
			this.grpName.SuspendLayout();
			this.SuspendLayout();
			// 
			// cmdWriteAspx
			// 
			this.cmdWriteAspx.Location = new System.Drawing.Point(64, 320);
			this.cmdWriteAspx.Name = "cmdWriteAspx";
			this.cmdWriteAspx.Size = new System.Drawing.Size(192, 48);
			this.cmdWriteAspx.TabIndex = 4;
			this.cmdWriteAspx.Text = "Write aspx";
			this.cmdWriteAspx.Click += new System.EventHandler(this.cmdWriteAspx_Click);
			// 
			// txtEmployeeID
			// 
			this.txtEmployeeID.Location = new System.Drawing.Point(112, 11);
			this.txtEmployeeID.Name = "txtEmployeeID";
			this.txtEmployeeID.Size = new System.Drawing.Size(96, 20);
			this.txtEmployeeID.TabIndex = 1;
			this.txtEmployeeID.Text = "EmployeeID";
			// 
			// lblEmployeeID
			// 
			this.lblEmployeeID.Location = new System.Drawing.Point(18, 13);
			this.lblEmployeeID.Name = "lblEmployeeID";
			this.lblEmployeeID.Size = new System.Drawing.Size(83, 16);
			this.lblEmployeeID.TabIndex = 0;
			this.lblEmployeeID.Text = "EmployeeID";
			this.lblEmployeeID.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// txtTitle
			// 
			this.txtTitle.Location = new System.Drawing.Point(104, 18);
			this.txtTitle.Name = "txtTitle";
			this.txtTitle.Size = new System.Drawing.Size(96, 20);
			this.txtTitle.TabIndex = 3;
			this.txtTitle.Text = "Title";
			// 
			// lblTitle
			// 
			this.lblTitle.Location = new System.Drawing.Point(8, 18);
			this.lblTitle.Name = "lblTitle";
			this.lblTitle.Size = new System.Drawing.Size(83, 16);
			this.lblTitle.TabIndex = 2;
			this.lblTitle.Text = "Title";
			this.lblTitle.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// txtLastName
			// 
			this.txtLastName.Location = new System.Drawing.Point(104, 42);
			this.txtLastName.Name = "txtLastName";
			this.txtLastName.Size = new System.Drawing.Size(160, 20);
			this.txtLastName.TabIndex = 7;
			this.txtLastName.Text = "LastName";
			// 
			// txtFirstName
			// 
			this.txtFirstName.Location = new System.Drawing.Point(104, 66);
			this.txtFirstName.Name = "txtFirstName";
			this.txtFirstName.Size = new System.Drawing.Size(160, 20);
			this.txtFirstName.TabIndex = 9;
			this.txtFirstName.Text = "FirstName";
			// 
			// lblFirstName
			// 
			this.lblFirstName.Location = new System.Drawing.Point(8, 66);
			this.lblFirstName.Name = "lblFirstName";
			this.lblFirstName.Size = new System.Drawing.Size(83, 16);
			this.lblFirstName.TabIndex = 8;
			this.lblFirstName.Text = "FirstName";
			this.lblFirstName.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// lblLastName
			// 
			this.lblLastName.Location = new System.Drawing.Point(8, 42);
			this.lblLastName.Name = "lblLastName";
			this.lblLastName.Size = new System.Drawing.Size(83, 16);
			this.lblLastName.TabIndex = 6;
			this.lblLastName.Text = "LastName";
			this.lblLastName.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// lblStreet
			// 
			this.lblStreet.Location = new System.Drawing.Point(8, 24);
			this.lblStreet.Name = "lblStreet";
			this.lblStreet.Size = new System.Drawing.Size(83, 16);
			this.lblStreet.TabIndex = 10;
			this.lblStreet.Text = "Street";
			this.lblStreet.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// txtPostalCode
			// 
			this.txtPostalCode.Location = new System.Drawing.Point(106, 72);
			this.txtPostalCode.Name = "txtPostalCode";
			this.txtPostalCode.Size = new System.Drawing.Size(160, 20);
			this.txtPostalCode.TabIndex = 15;
			this.txtPostalCode.Text = "PostalCode";
			// 
			// lblRegion
			// 
			this.lblRegion.Location = new System.Drawing.Point(8, 96);
			this.lblRegion.Name = "lblRegion";
			this.lblRegion.Size = new System.Drawing.Size(83, 16);
			this.lblRegion.TabIndex = 16;
			this.lblRegion.Text = "Region";
			this.lblRegion.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// txtRegion
			// 
			this.txtRegion.Location = new System.Drawing.Point(106, 96);
			this.txtRegion.Name = "txtRegion";
			this.txtRegion.Size = new System.Drawing.Size(160, 20);
			this.txtRegion.TabIndex = 17;
			this.txtRegion.Text = "Region";
			// 
			// lblCountry
			// 
			this.lblCountry.Location = new System.Drawing.Point(8, 120);
			this.lblCountry.Name = "lblCountry";
			this.lblCountry.Size = new System.Drawing.Size(83, 16);
			this.lblCountry.TabIndex = 18;
			this.lblCountry.Text = "Country";
			this.lblCountry.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// lblPostalCode
			// 
			this.lblPostalCode.Location = new System.Drawing.Point(8, 72);
			this.lblPostalCode.Name = "lblPostalCode";
			this.lblPostalCode.Size = new System.Drawing.Size(83, 16);
			this.lblPostalCode.TabIndex = 14;
			this.lblPostalCode.Text = "PostalCode";
			this.lblPostalCode.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// lblCity
			// 
			this.lblCity.Location = new System.Drawing.Point(8, 48);
			this.lblCity.Name = "lblCity";
			this.lblCity.Size = new System.Drawing.Size(83, 16);
			this.lblCity.TabIndex = 12;
			this.lblCity.Text = "City";
			this.lblCity.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// txtCountry
			// 
			this.txtCountry.Location = new System.Drawing.Point(106, 120);
			this.txtCountry.Name = "txtCountry";
			this.txtCountry.Size = new System.Drawing.Size(160, 20);
			this.txtCountry.TabIndex = 19;
			this.txtCountry.Text = "Country";
			// 
			// txtCity
			// 
			this.txtCity.Location = new System.Drawing.Point(106, 48);
			this.txtCity.Name = "txtCity";
			this.txtCity.Size = new System.Drawing.Size(160, 20);
			this.txtCity.TabIndex = 13;
			this.txtCity.Text = "City";
			// 
			// txtStreet
			// 
			this.txtStreet.Location = new System.Drawing.Point(106, 24);
			this.txtStreet.Name = "txtStreet";
			this.txtStreet.Size = new System.Drawing.Size(160, 20);
			this.txtStreet.TabIndex = 11;
			this.txtStreet.Text = "Street";
			// 
			// grpAddress
			// 
			this.grpAddress.Controls.Add(this.lblStreet);
			this.grpAddress.Controls.Add(this.txtPostalCode);
			this.grpAddress.Controls.Add(this.lblRegion);
			this.grpAddress.Controls.Add(this.txtRegion);
			this.grpAddress.Controls.Add(this.lblCountry);
			this.grpAddress.Controls.Add(this.lblPostalCode);
			this.grpAddress.Controls.Add(this.lblCity);
			this.grpAddress.Controls.Add(this.txtCountry);
			this.grpAddress.Controls.Add(this.txtCity);
			this.grpAddress.Controls.Add(this.txtStreet);
			this.grpAddress.Location = new System.Drawing.Point(8, 144);
			this.grpAddress.Name = "grpAddress";
			this.grpAddress.Size = new System.Drawing.Size(280, 152);
			this.grpAddress.TabIndex = 20;
			this.grpAddress.TabStop = false;
			this.grpAddress.Text = "Address";
			// 
			// grpName
			// 
			this.grpName.Controls.Add(this.txtTitle);
			this.grpName.Controls.Add(this.lblTitle);
			this.grpName.Controls.Add(this.txtLastName);
			this.grpName.Controls.Add(this.txtFirstName);
			this.grpName.Controls.Add(this.lblFirstName);
			this.grpName.Controls.Add(this.lblLastName);
			this.grpName.Location = new System.Drawing.Point(8, 40);
			this.grpName.Name = "grpName";
			this.grpName.Size = new System.Drawing.Size(280, 96);
			this.grpName.TabIndex = 21;
			this.grpName.TabStop = false;
			this.grpName.Text = "Name";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(312, 382);
			this.Controls.Add(this.grpName);
			this.Controls.Add(this.grpAddress);
			this.Controls.Add(this.txtEmployeeID);
			this.Controls.Add(this.lblEmployeeID);
			this.Controls.Add(this.cmdWriteAspx);
			this.Name = "Form1";
			this.Text = "Windows forms => Web forms";
			this.grpAddress.ResumeLayout(false);
			this.grpName.ResumeLayout(false);
			this.ResumeLayout(false);

		} // end private void InitializeComponent()
		#endregion
		#endregion
		#region Main
		#region void Main()
		#region XML comment
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		#endregion
		[STAThread()]
		static void Main()
		{
			System.Windows.Forms.Application.EnableVisualStyles();
			System.Windows.Forms.Application.DoEvents();
			Application.Run(new Form1());
		} // end static void Main()
		#endregion
		#endregion
		#region private methods (event handler)
		#region void cmdWriteAspx_Click(object sender, System.EventArgs e)
		private void cmdWriteAspx_Click(object sender, System.EventArgs e)
		{			
			suite4.net.WinForms2WebForms.Convert2Aspx convert2Aspx;
			string directory = @"C:\WinForms2WebForms";

			try 
			{
				//Check for the existence of the target directory, create if absent.
				System.IO.DirectoryInfo        directoryInfo;
				directoryInfo = new System.IO.DirectoryInfo(directory);
				if(!directoryInfo.Exists)
				{
					directoryInfo.Create();
				}
			}
			catch (System.IO.IOException ioEx)
			{
				MessageBox.Show(ioEx.Message);
			}
            
			try 
			{
				//Convert the form to an ASP.NET Web Form
				convert2Aspx = new suite4.net.WinForms2WebForms.Convert2Aspx();
				convert2Aspx.AspxType = suite4.net.WinForms2WebForms.AspxTypes.Page;
				convert2Aspx.SourceLanguage = suite4.net.WinForms2WebForms.SourceLanguages.C_Sharp;
				convert2Aspx.Convert(this, directory);

				//Convert the form to an ASP.NET user control
				convert2Aspx = new suite4.net.WinForms2WebForms.Convert2Aspx();
				convert2Aspx.AspxType = suite4.net.WinForms2WebForms.AspxTypes.UserControl;
				convert2Aspx.SourceLanguage = suite4.net.WinForms2WebForms.SourceLanguages.C_Sharp;
				convert2Aspx.RootName = this.Name + "UC";
				convert2Aspx.Convert(this, directory);
			}
			catch (System.ApplicationException appEx)
			{
				MessageBox.Show(appEx.Message);
			}

			MessageBox.Show("Two files have been written to:\n" + directory);

		} // end private void cmdWriteAspx_Click(object sender, System.EventArgs e)
		#endregion
		#endregion
	} // end public class Form1
} // end namespace suite4.net.WinForms2WebForms

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer (Senior) M-net Telekommunikations GmbH
Germany Germany
.NET developer based in Munich, Germany. Specialising in development automation and code generation for software developers using .NET.

Owner and Developer of form.suite4.net

Written By
Web Developer
Germany Germany
Hardy is an independent consultant and developer located in Munich, Germany. He spezializes in ASP.NET development and runs the .NET Developers Group Munich since April 2003.

Company website: www.netspectrum.de

Comments and Discussions