Windows Form Application C#

In this article, we’ll achieve numerous goals. First of all, we’ll learn through a step-by-step process to create the project in Visual Studio 2019 and create a windows form application. Within the application, we’ll use the features of the drag and drop low code approach supported for the Designer in Visual Studio 2019. A small part of gamification has been creatively put in to make it interesting for beginners. Later, we run the application and look at the prospective possibilities with the windows from the application. 

The previous article, C# Connect to MySQL in Visual Studio 2019 will help you connect to the database for applications in C# and the .NET framework from Visual Studio itself.

Step 1

Let us start with the installation of Visual Studio 2019. The Community edition is free for use. Once installed, the following screen will pop up. Click on Launch. 

Step 2

To start a new project, click on Create a new project.

Step 3

Now, as we are making a Windows Forms App, we choose the Windows Forms App (.NET Framework). 

If you don’t find the options available, it's probably not installed yet. You can select the Install more tools and features and then choose the .NET desktop development workload which supports console application building, windows forms application, and WPF.

Step 4

Fill in the project name, the location where the solutions repository will be saved, the solution name, and the .NET framework of your choice. After that, Click on Create.

Step 5

The Visual Studio Solution will be created and the window will look similar to as shown below. 

Step 6

In caset he Toolbox is not there, you can opt for it from the View section in Menu.

Step 7

The Toolbox should look similar to the following image. 

Now, let us start our project. Choose the Button and then drag and top on the Designer.

I’ve created three buttons here, button1, button2, and button3.

The properties of the button can be seen in the Properties section. The Text shows the displayed name of the button - the first button.

Let us change the text to Press Here.

Moreover, the Name of the Design is the one that will call functions for in the program. Here, we name it btnPresshere.

Step 8

Let us go back to Toolbox and search for labels by typing in the search bar. Select the Label and drag and drop one in the designer.

Step 9

The new label i.e. label1 is placed below the button Press Here.

The reason for the label is to call an action while pressing the Press Here button.

Step 10

Let us change the name of the label to labelPressHere. A good convention is to create label names as per the action it operates to.

Step 11

Now double click the Press Here button from the Designer.

A class will be created automatically in the Form1.cs file that would look similar to the one below. 

Now, let us add the following code to create action while pressing the Press Here button. 

labelPresshere.Text = “Welcome to C# Corner” 

Let us test what the program can do now. You can run the program, by clicking the Start button. 

Let us see, what happens when we click the Press Here button. 

See, label1 changes to Welcome to C# Corner now.

Step 12 - ROCK PAPER SCISSOR 

Let us get creative now with button2. Let's create a small Rock Paper Scissor game.

Let us rename the button to Rock Paper Scissor and the label to labelRPS and create another label called labelDYW.

Double Clicking the Rock Paper Scissor button, new classes are created.

Add the following code for button2_Click action.

private void button2_Click(object sender, EventArgs e) {
    string[] game = {
        "Rock",
        "Paper",
        "Scissor"
    };
    Random rand = new Random();
    int index = rand.Next(game.Length);
    labelRockPaperScissor.Text = game[index];
    labelDYW.Text = "Did you win?";
}

An array of strings named game is created. The random function is called and with randomness for the possible outcome for Rock, Paper, and Scissor – this is now a legit game you can play when you are bored.

Step 13

When we run the program, we can see, how every time we press the Rock Paper Scissor button, the new Round comes out with a different answer. So, Did You Win? 

Step 14

Let us go, and change the label text to white spaces. This will make the User Interface of the App, cleaner as follows.

Step 15

Think of something creative of your own and making something out of the button3 from the lessons you learned above.

Conclusion

Thus, in this article, we learned about creating the Windows Form Application in C# using Visual Studio 2019. All the tools and resources in this article are freemium versions. So, you can try it without any payment requirement. Here, we went through a step-by-step procedure to create windows form app, design a simple app, call actions when clicking a button and with a pinch of creativity, literally made a game. I wanted to showcase; how powerful programming is. With this bit of learning, you have now entered a huge possibility of creation. You can now make an app, you might in the future make a fun game. Find out problems that can be solved, and with the stepping stone of today, their possibilities of creation are endless for you in days to come.