Programming Windows dialogs

Programming a simple dialog box and explaining the code. Good for C# beginners that only did their work in the console.

Usualy, the ‘Hello World’ example in C# books is coded for a console aplication. This outputs the message on a command prompt.

Below, you will see a more useful ‘Hello World’ example, because the ‘Hello World’ text appears in a familiar Windows dialog box.

If you didn’t code any ‘Hello World’ console application yet, see this tutorial first.

Open Microsoft Visual Studio .NET and choose ‘New Project’ from the ‘File’ menu.
Choose ‘Empty Project’ from ‘Visual C# Projects’. We don’t choose ‘Windows Application’ because we don’t want any form automatically created.
After you enter ‘HelloWorld2’ as the name of the project, choose OK.

Unlike the ‘Console’ type of project, the ‘Empty Project’ doesn’t create any .cs file and doesn’t add any reference. You can see this in the solution explorer on the top-right edge of the window.

Right click on ‘Solution Explorer’ and choose ‘Add reference…’. From the list on the ‘.NET’ tab add ‘System.Windows.Forms.dll’ to the ‘Selected Components’ list by double-clicking it.

We need to add this reference because the ‘MessageBox’ class is located in the assembly file ‘System.Windows.Forms.dll’. We can’t use the ‘MessageBox’ class without including this file.
If you know just a little about C++ you probably remember including header files in your source code. Including references in C# are similar to including header files in C++.

After the reference is added we shall add a .cs file to our project. From the ‘Project’ menu choose ‘Add Class’. Enter ‘helloWorld2.cs’ as the name of the class file.

The following code is created by Visual Studio in our new file:


using System;

namespace HelloWorld2
{
    /// <summary>
    /// Summary description for helloWorld2.
    /// </summary>
    public class helloWorld2
    {
        public helloWorld2()
        {
            //
            // TODO: Add constructor logic here
            //
        }
    }
}



Replace the code with the following:


using System;

class helloWorld2
{
    static void Main()
    {
        MessageBox.Show("Hello World!\n(Part II)");
    }
}



I removed some unnecessary code from what Visual Studio created, like the ‘HelloWorld2’ namespace. Our program is so small that it doesn’t need a namespace.


System.Windows.Forms.MessageBox.Show("Hello World!\n(Part II)");



‘System.Windows.Forms’ is the namespace. ‘MessageBox’ is the class. ‘Show’ is the method.

I could of added ‘using System.Windows.Forms’ at the top of the code and use:


MessageBox.Show("Hello World!\n(Part II)");



instead of


System.Windows.Forms.MessageBox.Show("Hello World!\n(Part II)");



But we don’t use this namespace to much in our program, as you can see. Only once.

Compile your program using ‘Ctrl+F5’ (to compile without debugging).


Replace the line:


System.Windows.Forms.MessageBox.Show("Hello World!\n(Part II)");



with this one:


System.Windows.Forms.MessageBox.Show("Hello World!\n(Part II)", "Hello World 2");



This will add a title to our small dialog box.


Let’s add an icon to our dialog box and choose what buttons we wish to display on it:


using System;

class helloWorld2
{
    static void Main()
    {
        System.Windows.Forms.MessageBox.Show("Hello World!\n(Part II)", "Hello World 2", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
    }
}



You can choose to have other buttons on your dialog box:


System.Windows.Forms.MessageBoxButtons.OKCancel
System.Windows.Forms.MessageBoxButtons.YesNo
System.Windows.Forms.MessageBoxButtons.YesNoCancel
System.Windows.Forms.MessageBoxButtons.AbortRetryIgnore



The icon can be one of the following:


System.Windows.Forms.MessageBoxIcon.Information
System.Windows.Forms.MessageBoxIcon.Exclamation
System.Windows.Forms.MessageBoxIcon.Question
System.Windows.Forms.MessageBoxIcon.Error

You are familiar with them from the Windows environment.


There is much more to say about the ‘System.Windows.Forms’ namespace and programming Windows.
This tutorial is like a drop of water in a bucket. Anyway, there will be other tutorials following this, therefore stay tuned on Geekpedia 

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