Is your client looking for a programming or design skill that you don't have? Refer a project to us, earn up to $10,000 in referral commission.
Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.

in JavaScript Programming Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

Windows forms programming

Creating forms in C#, and handling them, the object oriented way.

On Wednesday, April 14th 2004 at 09:07 PM
By Andrew Pociu (View Profile)
*****   (Rated 4.3 with 34 votes)
Contextual Ads
More C# Resources

In this tutorial we'll see how to create a form and change some of its properties.

Create a new 'Empty Project' named 'firstForm' in Microsoft Visual Studio .NET.
Add a class to it named Class1.cs.
Now add the reference 'System.Windows.Forms' to the project (right click on the 'References' folder in 'Solution Explorer' and click 'Add Reference'.

Overwrite the code that Visual Studio created automatically in the Class1.cs file with this one:

using System;
using System.Windows.Forms;

class Class1
    public static void Main()
        Form frm1 = new Form();
        frm1.Text = "Hello form!";

Now it's time to analyze it.

Form frm1 = new Form(); - create a new instance of 'Form()' named 'frm1';

frm1.Text = "Hello form!"; - set the property named 'Text' of 'frm1' to the specified string;

Application.Run(frm1); - this method makes the form visible. It takes 'frm1' (the instance of the 'Form' object) as an argument.

The thing is you can display the form using other methods different from Application.Run(). But when should you use them? This I'll try to explain in the following example.

Create a new 'Empty Project' named 'twoForms'.
Again, add a class to it named Class1.cs and 'System.Windows.Forms' reference.

Overwrite the code generated by Visual Studio with this one:

using System;
using System.Windows.Forms;

class Class1.cs
    public static void Main()
        Form frm1 = new Form();
        Form frm2 = new Form();

        frm1.Text = "frm1"; // for recognizing the two forms
        frm2.Text = "frm2"; // for recognizing the two forms
             frm2.Show();    // Method 1
        Application.Run(frm1); // Method 2

As you can see, we now create two forms, the same way we did in the first example.
Though, we now display 'frm1' using the method 'Application.Run()', and 'frm2' using 'Show()'.
Compile the program and you will see the difference.
After you run the program, there will be two forms created, 'frm1' and 'frm2'.
Close 'frm2' using the 'X' button. As you can see, 'frm1' still remains, and the program doesn't end.
Now close 'frm1'. You can see that the program ends, and in the output window you can see:

The program '[3508] twoForms.exe' has exited with code 0 (0x0)

That means the program terminated succesfully.

Run the program again. Now first close 'frm1'. You can see that both forms close and again, the program ends.

From this we can reach the conclusion that the form displayed using 'Application.Run()' is somehow the main form.
'frm2', displayed using 'Show()' is dependent of 'frm1'. We can even say that 'frm1' is the program itself.

Changing the properties of the form

Create a new 'Empty Project' named 'formProp' and add the same 'Class1.cs' file and 'System.Windows.Forms' reference.

Replace the code genereated by Visual Studio with the following:

using System;
using System.Windows.Forms;

class Class1
    public static void Main()
        Form frm1 = new Form();
        frm1.Text = "Form properties";
        frm1.Cursor = Cursors.Cross;
        frm1.ShowInTaskbar = false;

As you can see, a new form is created. After this we set some of its properties.

frm1.Cursor = Cursors.Cross; - the type of cursor you want to be displayed when you are with the mouse over the form;

frm1.ShowInTaskbar = false; - if you set this property to false, the form will not be displayed in the taskbar, along with the other applications;

For settting further properties you need to add the reference 'System.Drawing'.
After you add that reference to the project, you can change further properties, the background color for example:

frm1.BackColor = System.Drawing.Color.DeepSkyBlue;
Digg Digg It!     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by Matt W on Thursday, April 22nd 2004 at 05:18 PM

Or, use Visual Studio.NET's Form designer! =O)

by nickeax on Saturday, May 7th 2005 at 09:52 PM

Very good for a beginner like myself. Although there is an error in the first example for the class name. I had to remove the .cs from Class1.

by J.Celmer on Tuesday, March 7th 2006 at 01:45 PM

Can you please tell me what do I do if
1. I have two separate projects, one form in each, say Form1, Form2 respectively and I want to combine these 2 forms in one project and start the second form when clicking a button in the first one.
2. What if I already have more than one form in the same project and I would like to start one and the second loads automatically with some delay replacing the first one?

by Mona on Thursday, July 20th 2006 at 02:22 AM

Just wondering in the above example, while using the forms juz show up and disappear. They dont stay, is it because they have been created dynamically. Is there a way to make them stay onscreen?

by eqquito on Friday, August 4th 2006 at 11:47 PM

try with showdialog() instead of show()

by on Tuesday, February 13th 2007 at 12:02 PM


by Mithilesh Kumar on Monday, July 20th 2009 at 05:21 AM

very good learning material for beginner.

by Mr. Wizard on Wednesday, October 7th 2009 at 09:00 PM

I'll leave C# to the script kiddies, real programmers use C ! Sorry kids...

by kimz on Thursday, June 24th 2010 at 12:37 AM

Mr. Wizard...get us a** out of here then...zzzzz

by unsecured credit cards for bad credit on Wednesday, December 14th 2011 at 10:24 AM

Why don't you post your own solution Pavlo..... it would have sure taken less time than writing all that.

Comment Comment on this tutorial
Name: Email:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs C# Job Search
My skills include:
Enter a City:

Select a State:

Advanced Search >>
Discover Geekpedia