Search KB Filter article types
Data-centric Windows Forms to Web showcase
Categories: to Web
Tags: Architects, Developers, 1. Beginner, 2. Intermediate, 3. Advanced, Windows Forms, v6.3, v6.4 and Later
Revision: 1
Posted: 28/Jan/2010
Updated: 04/Dec/2010
Status: Publish
Types: Walkthrough

Table of contents
This article will have a few sections added to it soon, based on the following article type skeleton: CodeSample


Introduction

Making an already running Windows Forms desktop data application available on the Web or deploy it on the Cloud, has in the past been a very resource intensive and tedious task. You have very few options and all of them require that you rewrite most, if not all, of the user interface logic. Migrating to Visual WebGui provides you with a framework that, with very few exceptions, is code compatible with the Windows Forms API so your migration process becomes a simple copy of the source code and a few global Find/Replace operations on the source, and you have an almost ready Visual WebGui Web application with no need to rewrite the user interface.

In addition, Visual WebGui enhances some of it's controls compared to Windows Forms, which makes your converted Visual WebGui even more powerful and flexible than your original Windows Forms application. A classic example of those enhancements can be found in the converted sample applicaton and later in this article, where the ListView control has been enhanced with a dialog for reordering of columns, a dialog for selective sorting on multiple listview columns, automatic internal paging and relaying the sorting operation to the database layer to minimize the load on the web server.

Overview

This article will walk you through the major tasks required for conversion of Windows Forms application to Visual WebGui application, adding some enhancements along the way that are available in Visual WebGui and not in Windows Forms.

General overview of the process, as well as general overview of differences between Windows Forms applications and Visual WebGui applications can be found in the references section, where you will also find information about conversion of third party controls and thread safety considerations.

PreparationsDatabase

The sample applications require a database connection to an updated NorthWind database. The updates are minor, and are only in the form of a few added SQL views. For convenence, the updated database can be downloaded below. If you already have installed the NorthWind database on your server, you can also download an SQL script that will create the necessary SQL views.

Images and Resources folder

Visual WebGui applications by default require that application images and icons, that do not come from a database, reside on a folder named Resources, directly on the root of the virtual folder of the application. Also, by default, application images and icons are kept seperate on folders named Resources\Images and Resources\Icons.

When preparing for a conversion of Windows Forms application to Visual WebGui application, you will have to collect all your images and icons that are stored in your Windows Forms application into Resources\Images and Resources\Icons folders. Subfolders within those folders are of course allowed, and smart naming of subfolders that reflect the original placement within the Windows Forms application might ease the conversion considerably.

For the purpose of this sample, this step has already been performed, and all the (non-database) applications images and icons can be found on those folders within both of the sample code projects.

Conversion process
Create the Visual WebGui application

You start the conversion process by creating a Visual WebGui application.

Copy the Windows Forms code to the Visual WebGui application

This is a simple file based copy of all the *.cs and *.resx files from the Windows Forms application folder to the Visual WebGui application folder. You should leave the Program.cs file behind though, as it is not used in Visual WebGui. You should include the Resources folder in your copy, so all your application images and icons will be included.

Replace references to System.Windows.Forms

All over your Windows Forms project, there are references to System.Windows.Forms. These references include both C# "using" statements, as well as other namespace refererences to that namespace. To handle the majority of the conversion cases within your Visual WebGui application, it is safe to Find/Replace over your entire Visual WebGui project and serch for "System.Windows.Forms" and replace it with "Gizmox.WebGUI.Forms".

Adding other references
using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Common.Resources;
using Gizmox.WebGUI;
using Gizmox.WebGUI.Forms.Dialogs;
using Gizmox.WebGUI.Common.Interfaces;
using Gizmox.WebGUI.Common.Gateways;

As required by the codechanges, one or more of the following additional C# "using" statements were added to the source code:

Required API changes - differences in architectureAboutVWGForm class - Images/LinkLabel/Version

The major difference here is image and resource handling. In Windows Forms you use the FromFile function to retrieve the image bitmap directly from from the local disk from where the Windows Forms application is running, and assign it to the Image property of the PictureBox. In Visual WebGui, the image is stored within the virtual folder of the applicaton on the webserver, and we use an ImageResourceHandle (a builtin Gateway) to reference the image. The "value", assigned to PictureBox.Image in Visual WebGui, is a (gateway) reference to an image whose actual contents will be retrieved by the browser when the page is rendered.

Another difference here is that Windows Forms uses the LinkLabel.URL property for it's links, while Visual WebGui uses LinkLabel.Text for the same purpose.

Finally we use the WGConst class to retrieve and display the version number of the Visual WebGui framework.

// Windows Forms
this.mobjPictureLogo.Image = Image.FromFile("Resources\\Images\\AboutVWGLogo.jpg");
this.mobjLabelVersion.Text = "1.0.0.0";
 
// Visual WebGui
this.mobjPictureLogo.Image = new ImageResourceHandle("AboutVWGLogo.jpg");
this.mobjLabelVersion.Text = string.Format(mobjLabelVersion.Text, WGConst.Version.Replace("WebGUI-V", ""));
this.linkLabel3.Url = "http://www.visualwebgui.com";



CategoryPictureResourceHandle class - Read images from Database

This class serves the purpose of retrieving images from a database. The Windows Forms version reads the image data from the database, writes it to a temporary file, and then uses the Image.FromImage() static method to retrieve the bitmap and return.

// Windows Forms
public class CategoryPictureResourceHandle 
 
// Visual WebGui
public class CategoryPictureResourceHandle : DatabaseResourceHandle

As mentioned before, Visual WebGui uses images in a fundamentally different way, by using gateway reference. For this purpose we use the DatabaseResourceHandle class in Visual WebGui. The most notable difference is the overridden WriteContent method. The way it works is like with other images in Visual WebGui. You assign a gateway reference to an Image property and when the browser renders the page, it will request the image contents. The purpose of the overridden DatabaseResourceHandle.WriteContent method is to retrieve the correct image from the database and write it's contents to the response outputstream, when the browser requests it.

// Visual WebGui
protected override void WriteContent(HttpResponse objResponse, IDataReader objDataReader)
{
    // Fix for northwind images
    byte[] objBytes = (byte[])objDataReader[objDataReader.GetOrdinal(this.ContentColumn)];
    objResponse.OutputStream.BeginWrite(objBytes, 78, objBytes.Length - 78, null, null);
}

frmItemDetails class - Reference images in Database

The actual use of the CategoryPictureResourceHandle class is very similar in both versions of the application, while the actual retrieving of the database image is quite different. In Windows Forms you assign the actual bitmap to the Image property and the image is retrieved at the time the server-side (code-behind) code is run, while in Visual WebGui, only a reference is created in the code-behind code and the actual image contents are fetched when the browser renders the page, with the help of CategotyPictureResourceHandle.WriteContent method.

// Windows Forms
((PictureBox)item).Image = new CategoryPictureResourceHandle().FromFile(mobjListViewItem.SubItems[1].Text);
 
// Visual WebGui
((PictureBox)item).Image = new CategoryPictureResourceHandle(Convert.ToInt16(mobjListViewItem.SubItems[1].Text));

ExtendedListView.cs - ImageList vs. IconResourceHandle

Same difference as before, applies to images in ListViewSubItem. The Windows Forms application uses an ImageList for the ListView images, for optimization purposes so that only one copy of each image will need to be retrieved from disk, although it would be used more than once in the ListView. For Visual WebGui, there wouldn't be much difference, as all images, ImageList or not, are references and retrieved when the browser renders the page, so we chose to use simple image reference without an ImageList. The GetIcon function is just a helper function for building the reference and returns an IconResourceHandle, thereby (by default) referencing icons from the Resources\Icons folder within the virtual folder of the application.

// Windows Forms
itmListItem = this.listView1.Items.Add("", "AttachedMail");
 
// Visual WebGui
itmListItem = this.listView1.Items.Add(GetIcon("AttachedMail.gif"));
 
private string GetIcon(string strName)
{
    return (new IconResourceHandle(strName)).ToString();
}


Form1.cs - Generate graphs

Building the graph bitmap is identical in both applications. As before, a Windows Forms application assigns the bitmap directly to the PictureBox.Image property, while Visual WebGui creates a reference to a gateway, and the browser will request the bitmap when it renders the page.

// Windows Forms
private void PaintGraph()
{
    this.pictureBox1.Image = RenderGraph().GetImage();
    this.pictureBox1.SizeMode = PictureBoxSizeMode.Normal;
}
 
// Visual WebGui
private void PaintGraph()
{
    this.pictureBox1.Image = new GatewayResourceHandle(new GatewayReference(this, "graph"));
    this.pictureBox1.SizeMode = PictureBoxSizeMode.Normal;
}

This intoduces the use of Visual WebGuigateway within the form itself, which will generate the bitmap and write it to the response strean upon request from the browser via the overridden ProcessGatewayRequest method:

// Visual WebGui
protected override IGatewayHandler ProcessGatewayRequest(HttpContext objHttpContext, string strAction)
{
    // If is "graph" action print a graph to the response, otherwise let the base class handle the request.
    if (strAction == "graph")
    {
        gp = RenderGraph();
 
        // Create a Bitmap instance    
        Bitmap objBitmap = new Bitmap(500, 400);
        Graphics objGraphics = Graphics.FromImage(objBitmap);
        gp.Draw(objGraphics);
         
        objBitmap.Save(objHttpContext.Response.OutputStream, ImageFormat.Jpeg);
 
        return null;
    }
    else
    {
        return base.ProcessGatewayRequest(objHttpContext, strAction);
    }
}




Visual WebGui enhancements

In Visual WebGui we have various enhancements over Windows Forms implementation and we use a few of those enhancements in the demo application.

Menu click handling in single handler (Form1.cs)

In Windows Forms we need one MenuClick event handler for each MenuItem, as we can't distinguish between MenuItems by the EventArgs type only. In Visual WebGui we can use the same approach, or we can use the fact that the argument to the MenuClick event handler is of MenuItemEventArgs type, which makes enables us to move all the MenuItem event processing inside of one single MenuClick event handling procedure.

// Windows Forms
void ShowGraphMenuClick(object sender, EventArgs e)
{
 
    this.splitContainer1.Panel2Collapsed = !this.splitContainer1.Panel2Collapsed;
    this.mainMenu1.MenuItems[2].MenuItems[0].Checked = !this.mainMenu1.MenuItems[2].MenuItems[0].Checked;
}
 
// Visual WebGui
void mainMenu1_MenuClick(object objSource, MenuItemEventArgs objArgs)
{
    string strAction = objArgs.MenuItem.Tag as string;
 
    switch (objArgs.MenuItem.Text)
    {
        case "Show Graph":
            this.splitContainer1.Panel2Collapsed = !this.splitContainer1.Panel2Collapsed;
            objArgs.MenuItem.Checked = !objArgs.MenuItem.Checked;
            break;
    }
 
}

Reordering ListView columns (ExtendedListView.cs)

In Visual WebGui we have a ListViewColumnOptions dialog form that we can use to enable ListView columns reordering and selection of columns to display in the ListView. This greatly enhances the richness of the ListView functionality with just just two extra lines of code. This functionality does not exist in Window Forms.

// Visual WebGui
public void ReorderColumns()
{
    ListViewColumnOptions objListViewColumnOptions = new ListViewColumnOptions(this.listView1);
    objListViewColumnOptions.ShowDialog();
}

Sorting ListView data (ExtendedListView.cs)

In Visual WebGui we have a ListViewSortingOptions dialog form that we can use to enable sorting options in the ListView, even on multiple columns you select. This yet more enhances the richness of the ListView functionality with just two extra lines of code. This functionality does not exist in Windows Forms.



// Visual WebGui
private void Sort()
{
    ListViewSortingOptions objListViewSortingOptions = 
            new ListViewSortingOptions(this.listView1);
    objListViewSortingOptions.ShowDialog();
}

Internal paging in ListView (ExtendedListView.cs)

In Visual WebGui we have an internal capability of paging as an integral part of the ListView. Paging in a ListView is an optimization method that prevents huge mounts of data to be loaded in the client's browser. The ListView (either fully automatic or manually as in the sample application) "pages" the data and only sends the records for the currently active page in the ListView to the client's browser, thus only a small portion of the total number of records are actually sent to the browser at a time. Using fully automatic ListView internal paging does however mean that the whole dataset will be loaded on the server side on the webserver. Best practice, to minimize resource usage on the web server, is to have implement manual paging where only the data for the currently active ListView page is loaded from the database to the webserver and then to the client.

This capability does not exist in Windows Forms.

// Visual WebGui
public void FillListView(SqlDataReader myData)
{
    // ....
    this.listView1.TotalPages = mintTotalPages;
 
    if (this.listView1.TotalPages !=0)
    {
        listView1.CurrentPage = mintTop / 14;    
    }
    this.listView1.UseInternalPaging = false;
    // ....
}
 
void listView1_PagingChanged(object sender, System.EventArgs e)
{
 
    mintTop = listView1.CurrentPage * 14;
    mintBottom = mintTop - 14;
 
    LoadList(mstrSelect, mstrTableName, mstrWhere, mstrOrderBy, true);
}



Sort using the Database and not just displayed rows (ExtendedListView.cs)

In Visual WebGui we can get the sort order from the Sort (ColumnClick) event and then run the sorting on the Database, loading the result set again in order to sort the entire set of records, not just the displayed ones.

// Visual WebGui
void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
{
    string strSortOrder = string.Empty;
 
    if (listView1.Columns[e.Column].SortOrder == Gizmox.WebGUI.Forms.SortOrder.None || listView1.Columns[e.Column].SortOrder == Gizmox.WebGUI.Forms.SortOrder.Descending)
    {
        strSortOrder = "DESC";
    }
    else
    {
        strSortOrder = "ASC";
    }
 
    LoadList(mstrSelect, mstrTableName, mstrWhere, listView1.Columns[e.Column].Text + " " + strSortOrder, true);
}


Sample code

The samples assume you have the NorthWind database installed on a local SQLEXPRESS database server and the database name is NorthWind. If your setup is different, you will need to change database connection strings to reflect your actual environment. The database connection string can be found in 3 places in each project and you will have to update all of them (CategoryPictureResourceHandle.cs, frmItemDetails.cs, ExtendedListView.cs). Search for the string "Data Source" (without the quotes) in the projects and make your adjustments to the connection string(s).

You might also want to change the temporary path used to store images in the Windows Forms application. Open CategoryPictureResourceHandle.cs, view FromImage method and change the value for the str variable, if current setting does not fit your needs.

NorthWind database

The database used in the sample code is the original NorthWind sample database with a few SQL views added for convenience. The NorthWind database, as used in the sample, is included for convenience. If you already have

Summary

The article has walked you through the major steps involved in migrating a Windows Forms application to Visual WebGui. It discusses the steps needed to prepare for the migration and how you can simplify some of the tasks. The article then discusses the major differences between Windows Forms and Visual WebGui that we faced while migrating this particular application, and provides solutions and suggestions for how to handle them. Finally it discusses a few of the many improvements Visual WebGui implements on it's controls compared to Windows Forms.

References

About the author

Related Articles

to Web  
Title Update Author
Tags: Developers, 2. Intermediate, 3. Advanced, Pre v6.3, v6.3, v6.4 and Later
07/Jan/2008    2008/01/07
Put your applications on Web or Cloud at a fraction of the cost & time by avoiding manual rewrite, leveraging existing code & skills and maintaining user experience.
Tags: Architects, Developers, 2. Intermediate, 3. Advanced, Pre v6.3, v6.3, v6.4 and Later
19/Aug/2010    2010/08/19
The video demonstrates the CloudMove solution which allows the shortest path from Client/Server applications to Web, Cloud and Mobile. The process includes the free AssessmentTool providing readiness level and Azure cost estimations and the automated TranspositionTool for transforming the code.
Tags: Architects, CIOs, Developers, 1. Beginner, 2. Intermediate, 3. Advanced, ASP.NET, Cloud, Pre v6.3, v6.3, v6.4 and Later
07/March/2011    2011/03/07
Abstract: The Web, Cloud and SaaS models are changing the computing world forever, and us, the developers will have to adjust and serve this trend. One of the biggest challenges is moving software assets that were developed for desktop architecture to the new deployment models. Just imagine that you...
Tags: Architects, CIOs, Developers, C#, 1. Beginner, 2. Intermediate, 3. Advanced, Windows Forms, Pre v6.3, v6.3, v6.4 and Later
07/Jan/2010    2010/01/07
Visual WebGui introduces a new platform to develop web applications and migrating desktop applications to the web. In this “How To” we are going to learn how to take an existing Windows Forms application and migrate it to the the web.
Tags: Architects, Developers, C#, 1. Beginner, 2. Intermediate, Windows Forms, Pre v6.3, v6.3, v6.4 and Later, 3. Advanced
16/Dec/2010    2010/12/16
Visual WebGui (VWG) provides the shortest path from Client/Server business applications to Windows Azure allowing to step up to the SaaS delivery model and enjoy the Cloud’s economics & scalability at minimal cost & time. In this session we will explain how VWG bridges the gap between Client/Server...
Tags: Architects, CIOs, Developers, 1. Beginner, 2. Intermediate, 3. Advanced, ASP.NET, Cloud, Pre v6.3, v6.3, v6.4 and Later
15/March/2011    2011/03/15
.NET Web, Cloud and Mobile application delivery platform | Sitemap | Terms of Use | Privacy Statement | Copyright © 2005-2011 Visual WebGui®       Visual WebGui weblog on ASP.NET Visual WebGui Group on LinkedIn Visual WebGui updates on Twitter Visual WebGui Page on Facebook Visual WebGui YouTube Channel Visual WebGui Platform News RSS