Manipulate instances of Windows forms in C#

This tutorial kicks off a series designed to help you understand the concepts behind C# development. To get you started, we'll show you how to open a new instance of a Form class.

The widespread introduction of a new language is a rare event. So C#, as a brand-spanking new language, has aroused considerable interest among members of the programming community. While C# borrows from Java, C++, C, Pascal, and even Visual Basic—for example, adopting VB’s foreach syntax for use with collections—it is a truly new language. In my opinion, it's also quite wondrous.

Is C# intended to be a “Java killer"? It’s true that if you look at Java and C# code side-by-side, they appear similar. But C# is primarily the native language of the .NET Framework. Whether you program the objects and classes of .NET in VB .NET or C#, a great deal of the work has to do with understanding those objects and classes rather than with the language used. In fact, most things you are likely to do in C# can also be done in VB .NET, making the choice of which language to use a matter of personal preference. Once you get used to C#, I think you’ll come to love it and find that it is particularly well suited to working with .NET.

This article is the first in a series that's intended to ease the learning curve and help you get started working productively with C#. It assumes that you’ve done some programming and perhaps are an experienced user of a pre-.NET version of Visual Basic. In this first installment, I'll explain how you manipulate instances of Windows forms in C#.

Form interoperability
In Visual Basic 6, a Windows form is a special kind of animal that is not entirely transparent. But in C# .NET, a form is a class just like any other class. To effectively work with multiple-form applications, you need to know how to invoke specific instances of these classes. (As you might expect, VB .NET works pretty much the same way as C# .NET in this respect.)

The Form class is the blueprint for an object. Instances of the Form class are objects you can use in your programs.

As I’ll show you in just a bit, it is easy enough to open a new instance of a Form class.

Form class code
Figure A shows a straightforward form—based on the class Form1—with a label, a text box, and two buttons. The user enters text in the text box and clicks one of the buttons to display it in the label. The other button will be used to hide the form and open an instance of the Form2 class.

Figure A

Listing A shows the complete code for this form class. (Some of this code is in the hidden Windows Form Designer region, so you won’t see it in the Visual Studio code editor unless you expand this region.) Let’s have a look at a few of the pieces of this code.

This line declares the Form1 class:
public class Form1 : System.Windows.Forms.Form

The colon operator (:) says that it inherits from the .NET Framework class System.Windows.Forms.Form. (If you’re curious, you can use the Object Browser to inspect the .NET Framework Form class.) Everything within the curly braces that follows the class declaration is part of the Form1 class.

Within the Form1 class, you’ll notice another reference to Form1 (mostly empty except for comments):
public Form1()

You can tell that this is the form constructor—where you should put initialization code you want executed when an instance of the form is created—because it is a method with the same name as the class.

Farther down the form source code module, you’ll find the following code:
static void Main()
   Application.Run(new Form1());

This is the application entry point and takes a few words of explanation. By default, when you open a new Windows Application project in C# .NET, the class Form1 is created and added to the project. It’s set by default to be the startup form, so it needs a Main method to be the entry point, or starting place, for the compiled application. Within this method, a new object instance of the Form1 class is created using the new keyword:
Application.Run(new Form1());

Finally, here’s the event code that fills the label with the text supplied by the user when the button is clicked:
private void btnText_Click(object sender, System.EventArgs e) {
   label1.Text = txtShow.Text;


Learn more about .NET development
The .NET framework can be overwhelming, but these books can help smooth the learning process:
Visual Basic .NET Programming
Visual C# .NET Programming

Opening a new form instance
Let’s kick this thing into gear by adding a second form class, Form2, to the project. To do this, select Add Windows Form from the Project menu, make sure Windows Form is selected in the Templates pane of the Add New Item dialog, keep the default name for the class module, and click Open.

We’re going to add a button to Form2 that will be used to display an instance of Form1. Going back to Form1, it’s easy to add the code to the second button’s click event that will be used to hide Form1 and show a new instance of Form2. The click event procedure looks like this:
private void btnForm2_Click(object sender, System.EventArgs e) {
   Form2 form2 = new Form2();

The first line uses the this keyword to refer to the current instance of the Form1 class. The Hide method “cloaks” the class instance so it is not visible on the screen (without deleting the information it references). Next, a new instance of the Form2 class, using the variable form2, is declared and instantiated:
Form2 form2 = new Form2();

The Show method is used on the form2 variable to display the instance of the Form2 class:

As far as it goes, this works just fine, and it does hide the first form and display an instance of the second form (Figure B).

Figure B

However, this technique does not help you much if you want to recall a specific form instance. In real-world applications, you will probably need to show forms containing information already collected—entered by the user or obtained programmatically—as new form instances. So how is a specific form instance displayed? We'll tackle that issue in our next article.

Editor's Picks