Getting Started with WinForms

 C-Sharp Learning Part 2

Hey everyone! In this article we are gonna see how to build our first app in C# using .NET. If you haven't installed Visual Studio click here.

First open Visual Studio 2022:

You will be presented with this screen:


Click on Create a new Project. You will get this:

Search for "WinForms" in the search box and just select it and click next.



Name your app in this screen and select the location of your Project and click Next:



In this screen, select .NET Core 3.1 (LTS), you can also select .NET 6.0 which is the latest:


Now when the project is created, select View at the top and select Designer. You will get this:
Please note that it might take some time if the Designer button is not present in the View Menu. If it is taking a lot of time, then please reopen your project.


Let us get started with each control in the toolbox. If you don't see the toolbox, then click View and select Toolbox at the bottom.
For starters, let's just create a very simple app.
First add a button from the toolbox to Form1 by dragging it. You can resize the button however you want:

To change the text in the button, right click it and select Properties. Scroll through the properties and you will find Text, just edit it there. If you also want to change the app name, then just do the same by going into Properties of the Form and editing the Text:

Now to show a Message Pop-Up on the button click, just double click the button or go to the Button's properties, select the button which says "Events" when hovered, at the top of the
Properties Tab and double click in the Click Event of the button:
After doing that, you will go into the Code Page where the fun starts:



Under the button1_Click, just enter the following code. You can customize the message:

 private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Hi! This is my first app.");
        }


Now, click on the button at the top which has the Play Icon and your app name:

After building your App, your App runs!

Click on the button and you got a messagebox!

Now for a more detailed MessageBox:
Stop Debugging by closing your app.
For a Title in the Messagebox:
Just add a piece of string next to it:
private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Hi! This is my first app.", "Message");
        }
For customizing the buttons:
    private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Hi! This is my first app.", "Message", MessageBoxButtons.OK);
        }
For an icon:

 private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Hi! This is my first app.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
Now just run it again:

Please Note: Since I am using Windows 11 Icons in Windows 10, the icons in the MessageBox may differ than the icons in your MessageBox even though you're running Windows 10.

For the Previous Tutorial, Click Here
For the Next Tutorial, Click Here

Comments

Popular posts from this blog

Getting Started With Coding

Using the CheckBox control in WinForms

Using the Button Control in WinForms