C# Forms -- First Look

Start Visual Studio.

Go to "File", and then "New" and then "Project", and select "Windows Form Application". Pick a name for it. The default names are rather uninformative and boring.

The Program.cs file contains the application's startup code, generated automatically. Do not modify this.

There are references here to "Form1", which is a class derived for you from "Form". We do not normally use the Form class directly.

The Form1.cs file contains code associated with the form, some of which was automatically generated. It will contain code such as the code to respond to a button click. This file has several components such as the Form1.Designer.cs file, which contains some automatically-generated code and also code you supply.

Bear in mind that the overall namespace can contain various other classes as well.

You should now have a window popped up marked "Form1". You can change its caption: Right click on the window and a pop-up menu called "Properties" appears. One of the items is "Text". Go there and change the name to "TestForm". The files involved will still refer to "Form1", as "TestForm" is merely a caption (data). If we choose "Form1.cs" from the list, we should again be looking at the window.

Let's put a button in the window. Go to "View" and click on "Toolbox". This will give you a long list of different kinds of common controls. Click and drag "Button" and drop it in the "TestForm" window. You can resize it or move it around with the mouse.

The file "Form1.Designer.cs" should now contain a line about "button1".

The button has properties as well. Change its name to "Press me". Double-click on the button. This should now show you "Form1", one of the components of "Form1.cs", which should now have a line referring to "button1_Click". This line is for an event handler to do something when a user clicks the button. (It wasn't there until you clicked on the button.) It looks like this:

     private void button1_Click(object sender, EventArgs e)
       {
       }

We will add some code to button1_click. Put in a line:

     MessageBox.Show("Thank you for pressing me.");

Now go to "Build" and "Build Solution". Fix any errors and then click on "Start".

This should run the program. The box pops up with title "TestForm" and a button saying "Press me". When you click on the button, you should have a second box pop up with a note saying "Thank you for pressing me." and a button saying "OK". When you click on "OK", the response box disappears. You can end the program's run by clicking on the X in the upper right-hand corner (though only after you get rid of the MessageBox).

Windows and buttons have other properties as well. We could change the colors used or the font, for example.


Let's try again.

Start a new project. Choose "Windows Forms Application" and name it "TBoxDemo". Click on "OK".

We now have a blank form marked "Form1" Right-click on it to get the menu of properties. Change its "text" property (the caption at the top) to "TextBox Demo".

Click on "View" and then "Toolbox". Pick "Label" and drag it over to the form. Type in "Your Name".

Now pick "TextBox" and drag it over the the form next to the label.

Now pick "Button" and drag it over to the form as well. Go look at "Form1.Designer.cs". You should see listed a Label called label1, a TextBox called textBox1, and a Button called button1. It is probably possible to change these names, but you would have to change them everywhere they appear in the code, which might be a nuisance. For this reason, I suggest that if you have multiple buttons or labels or whatever, all with these uninformative names, find the place where they are defined and put in comments identifying their purposes.

Right-click on the button to get the menu of properties for the button, and change its Text to "Submit".

Click on the button. The display should now shift to the code of "Tboxdemo.Form1", and you should see "button1_Click", an event handler for clicking on the button.

In the event handler, type in:

     String S = textBox1.Text;
     MessageBox.Show("Hello " + S);

Compile this (using "Build") and run it. You should have a window titled "TextBox demo" containing a label "Your Name" and a text box for entering a name, and also a "Submit" button. When you run it, type your name in the textbox and then click on "Submit". See what happens.


In general, when you work with a form, you will have a class called "Form1" derived from a base class called "Form".

In designing a form, more or less everything has properties. You can call up a list of these by right-clicking on the item. Most or all of them can be changed.

When you create a form, you start with a fairly small window. In the upper left is its name (by default "Form1") and in the upper right are standard tiny buttons for Minimize, Maximize and Exit (but which don't do much as a default). You can resize the window but not move it around.

There are multiple files involved. Some of them are: