Windows forms programming

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

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!";
        Application.Run(frm1);
    }
}

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;
        Application.Run(frm1);
    }
}

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;
Nathan Pakovskie is an esteemed senior developer and educator in the tech community, best known for his contributions to Geekpedia.com. With a passion for coding and a knack for simplifying complex tech concepts, Nathan has authored several popular tutorials on C# programming, ranging from basic operations to advanced coding techniques. His articles, often characterized by clarity and precision, serve as invaluable resources for both novice and experienced programmers. Beyond his technical expertise, Nathan is an advocate for continuous learning and enjoys exploring emerging technologies in AI and software development. When he’s not coding or writing, Nathan engages in mentoring upcoming developers, emphasizing the importance of both technical skills and creative problem-solving in the ever-evolving world of technology. Specialties: C# Programming, Technical Writing, Software Development, AI Technologies, Educational Outreach

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top