Example #1

VB6 / VB.NET / C# / ASP.NET Simple Project

 

This example is a simple piece of code that writes strings to a text box.  Similar to the traditional "hello world" application -- with a few more buttons.  It was designed to make sure we all have a common starting point before going on to other examples.  This example is a check to make sure that you are at least an intermediate programmer.  If you can not create this entire program from scratch in only a couple of minutes then you should probably go back and do more general training on Visual Basic or C# before continuing.  

For more training on VB.NET we recommend these books

For more training on C#  we recommend these books

This program has three command buttons and one text box.  When you click on a command button a predefined string is written to the text box.  To run the ASP.NET version on-line click here

 

Download Options

  1. Click here to download the Visual Basic 6 source code for this example.  
  2. Click here to download the VB.NET source code for this example. 
  3. Click here to download the C# source code for this example. 
  4. Click here to download the ASP.NET source code for this example
  5. Click here to download the CE.NET, PocketPC, Windows Mobile, Smart Device source code for this example. 

 

VB6 to VB.NET Changes

The biggest changes from VB6 code to .NET are that (1) .NET does not allow control arrays and (2) "command buttons" are now simply called "buttons".  Therefore we used the tabindex property of the buttons to indicate which button was pressed.  Instead of using the prefix (object coding convention) of "cmd" for a command button we now use "btn". 

.NET has an "Application" object which is used to run and quit your application (in VB6 we use to unload all the forms to stop running).  However, the application object does not have a "PreviousInstance" property.  So we went with what the VB.NET migration wizard gave us which is shown in the form "load" event handler. 

In VB.NET one event handler (routine) handles events for multiple buttons by declaring the event handler and then at the end of the event handler declaration adding "Handles btnChangeMessage0.Click, btnChangeMessage1.Click, btnChangeMessage2.Click". 

Note that in VB.NET we could have used "MsgBox" as in VB6 but we chose to use the "MessageBox.Show" instead.  We wanted to be more explicit, use the .NET syntax which we hope is more intuitive to all programmers. 

The last change from the VB6 code to VB.NET is that we used the "DirectCast" function to cast the general "eventSender" object into a Button object.  This allows us to use the "Option Strict On".  In C# this cast is done in a similar manner. 

 

VB.NET to C# Changes

C# is very similar to VB.NET except with a lot more curly brackets ({}) and semicolons (;).  You also have to spell and capitalize everything exactly correct (VB.Net some how figures out what you really want and corrects it for you -- C# does not correct things for you).  In C# we had to go into the section of code hidden by

#Region Windows Form Designer generated code

and for each button change the following lines

this.btnChangeMessage0.Click += new System.EventHandler(this.btnChangeMessage_Click);

this.btnChangeMessage1.Click += new System.EventHandler(this.btnChangeMessage_Click);

this.btnChangeMessage2.Click += new System.EventHandler(this.btnChangeMessage_Click);

The "Select Case" in VB has to be converted to "switch" in C#. 

Note that since we used "MessageBox.Show" in the VB.NET example instead of "MsgBox" (as we did in VB6) -- we did not have to change the command when converting from VB.NET to C#.  In VB.NET we wanted to be more explicit, use the .NET syntax which we hope is more intuitive to all programmers. 

 

ASP.NET Version

To deploy the ASP.NET application to the web host:

  1. Obviously you have to have a server that is running ASP.NET
  2. You have to create an IIS target directory (virtual directory on our web host).  This is also referred to as a subweb. 
  3. We would recommend using Visual Studio to copy the project to the web host

The ASP.NET version is slightly different from the VB.NET and C#. 

  1. The "Exit" button and Application.Exit are no longer used.  You simply close the browser to exit the program. 
  2. The MessageBox function is not available. 

 


We try to offer a fair and balanced opinion on every page of our website.  We would appreciate more information from other users to express their opinions which we will then incorporate.  If you have questions or comments please post them on our message board (see button in left hand column) so that others can read and benefit. 

visual basic vb C# net 6 example source code tutorial factory laboratory industrial manufacturing automation