Create Your First C# Windows Forms Application using Visual Studio

ProgrammingKnowledge2
22 Jul 202311:35

Summary

TLDRThis video tutorial guides viewers on creating a Windows Form application using C# in Visual Studio. It begins with creating a new project, selecting the appropriate template, and naming the project. The video then demonstrates how to use the designer to customize the form's appearance, add components like buttons and labels, and adjust their properties. The tutorial concludes with a simple coding example that shows how to display user input from a text box in a message box when a button is clicked, providing a basic foundation for Windows Form application development.

Takeaways

  • πŸ’» Start by opening Visual Studio and creating a new Windows Form application project in C# for the desktop platform.
  • πŸ” You can find the Windows Form application template by searching or selecting it from the list under C# and Desktop.
  • πŸ“ Name your project carefully, avoiding spaces for better compatibility with file systems and tools.
  • 🎯 Choose the latest .NET version available, such as .NET 6.0, to ensure you're using up-to-date features and optimizations.
  • πŸ–ΌοΈ The designer view allows you to visually manipulate the form's layout and properties, including resizing and setting titles.
  • πŸ”§ Use the toolbox to drag and drop UI components like buttons, labels, and text boxes onto your form.
  • πŸ“‘ The properties window is key for adjusting the appearance and behavior of components, such as changing text, font size, and anchoring.
  • πŸ”„ Anchoring components ensures they resize or move appropriately when the form's size changes, maintaining a responsive layout.
  • πŸ’¬ Write event handlers for components, like a button click, to define what happens when an action is performed, such as displaying a message box.
  • βœ… After coding, save your work and run the application to see the results, ensuring that the UI behaves as expected and that your code executes correctly.

Q & A

  • What is the main topic of the video?

    -The main topic of the video is creating a Windows Form application using C# in Visual Studio.

  • How does one start creating a new Windows Form application in Visual Studio?

    -To start creating a new Windows Form application, one should open Visual Studio, click on 'Create a new project', and then select 'Windows Form Application' under C# and Desktop platform.

  • What are the project type options available for Windows Forms in the video?

    -In the video, the project type options available for Windows Forms are 'Windows Forms App (.NET Core)' and 'Windows Forms App (.NET Framework)'.

  • What is the significance of not using spaces in the project name as mentioned in the video?

    -Not using spaces in the project name is significant because it avoids potential issues with file paths and command-line operations related to the project.

  • How can one access the designer view for the form in a Windows Form application?

    -To access the designer view for the form, one can double-click on the 'Form1.cs' file in the Solution Explorer, or expand the 'Form1.cs' node and double-click on 'Form1.Designer.cs'.

  • What is the default name given to the first form in a newly created Windows Forms application?

    -The default name given to the first form in a newly created Windows Forms application is 'Form1'.

  • How can one change the title of the form window in the designer?

    -To change the title of the form window, one can simply click on the title bar of the form in the designer and type the new title.

  • What is the purpose of the 'Toolbox' in a Windows Forms application?

    -The 'Toolbox' in a Windows Forms application is used to drag and drop components such as buttons, labels, and text boxes onto the form.

  • How can one anchor a control to a specific side or corner of the form?

    -To anchor a control, one can select the control, go to the 'Properties' window, find the 'Anchor' property, and choose the desired side or corner from the drop-down options.

  • What is the code that needs to be written to display a message box with the text entered in a text box when a button is clicked?

    -The code to display a message box with the text from a text box involves creating a string variable to store the text from the text box, and then using the 'MessageBox.Show' method to display the text when the button is clicked.

  • How does one run the Windows Form application after writing the code?

    -After writing the code, one can run the Windows Form application by clicking on the 'Start' button or the green play icon in Visual Studio.

Outlines

00:00

πŸ’» Creating Your First Windows Form Application

The video begins with a tutorial on creating a Windows Form application using C# in Visual Studio. The presenter guides viewers through the process of opening Visual Studio and initiating a new project. They emphasize selecting the 'Windows Form Application' option and choosing the .NET Core framework. The presenter then demonstrates how to name the project, avoiding spaces, and proceed with the creation. Upon project creation, they introduce the solution explorer and the main form file, explaining how to open the designer view. The video shows how to customize the form's title and manipulate its properties, such as font size and behavior, using the properties window. The presenter also explains how to use the toolbox to add components like buttons, labels, and text boxes to the form.

05:01

πŸ”§ Customizing Form Components and Behavior

In this segment, the presenter focuses on customizing the form's components and their behavior. They demonstrate how to change the text of a label and anchor it to different sides of the form to ensure it resizes appropriately when the form's dimensions change. The video also covers how to adjust the button's text and anchor it to the bottom of the form. The presenter explains the use of the properties window to modify the appearance and behavior of form elements. They show how to change the background color of the form and its components. The video then transitions into writing code to create an interaction where text entered in a text box is displayed in a message box when a button is clicked. The presenter provides a step-by-step guide on how to write this functionality, including creating a string variable to store the text from the text box and using a message box to display it.

10:01

πŸš€ Running and Testing the Application

The final part of the video script describes the process of running and testing the newly created Windows Form application. The presenter instructs viewers to save their code and use the run icon in Visual Studio to execute the application. They demonstrate the application's responsive design, showing how the form's components resize and adjust as the form's size changes. The presenter tests the functionality of the 'click me' button, which triggers a message box displaying the text entered in the text box. The video concludes with the presenter expressing hope that the viewers enjoyed the tutorial and hints at future content by mentioning the next video.

Mindmap

Keywords

πŸ’‘Windows Form Application

A Windows Form Application is a type of GUI (Graphical User Interface) application that runs on the Windows operating system. It is designed to provide a user-friendly interface for tasks such as data entry, processing, and display. In the video, the tutorial focuses on creating a Windows Form Application using C# in Visual Studio, which is a fundamental step for beginners in desktop application development.

πŸ’‘Visual Studio

Visual Studio is an integrated development environment (IDE) from Microsoft that is used to develop applications for various platforms, including Windows, Android, and iOS. It provides a comprehensive set of tools for software developers, including a code editor, debugger, and designer tools. In the context of the video, Visual Studio is used to create a Windows Form Application, showcasing its capabilities for desktop application development.

πŸ’‘C#

C# (pronounced 'C sharp') is a high-level, object-oriented programming language developed by Microsoft. It is widely used for developing various types of applications, including web, mobile, and desktop applications. In the video, C# is the programming language chosen for creating a Windows Form Application, highlighting its role in building interactive user interfaces.

πŸ’‘.NET Core

.NET Core is an open-source, cross-platform framework for building applications. It is a modular runtime and library implementation that allows developers to build applications that can run on multiple platforms. In the video, the tutorial selects 'Windows Form app using .NET Core' as the project type, indicating the use of .NET Core for creating a cross-platform Windows Form Application.

πŸ’‘Project

In the context of software development, a project refers to a planned set of tasks that aim to achieve a specific goal, such as creating a software application. In the video, the process of creating a new project in Visual Studio is demonstrated, which is the initial step in starting a Windows Form Application development.

πŸ’‘Designer

A Designer in Visual Studio is a tool that allows developers to create and modify the user interface of their applications without writing code. It provides a visual representation of the application's UI components. In the video, the designer is used to modify the form's appearance, such as changing the title and resizing the form.

πŸ’‘Components

Components in a Windows Form Application refer to the various UI elements that can be added to the form to create a functional and interactive interface. These can include buttons, labels, text boxes, and more. The video demonstrates how to drag and drop components like buttons and labels onto the form using the toolbox in Visual Studio.

πŸ’‘Properties

Properties in the context of a Windows Form Application are the attributes or characteristics of UI components that can be set or modified, such as the text, size, color, and behavior. The video shows how to change the properties of components, like setting the text of a label or resizing a text box, to customize the appearance and functionality of the form.

πŸ’‘Anchor

Anchoring in Windows Forms is a layout feature that allows UI components to remain positioned relative to the form's edges or corners when the form is resized. This ensures that the components do not overlap or become misaligned. The video explains how to use anchoring to ensure that a button and a text box resize and maintain their positions relative to the form's edges.

πŸ’‘Event Listener

An event listener in programming is a mechanism that allows code to respond to user actions or system events. In the video, a button click event listener is created by double-clicking the button in the designer. This action generates an event handler method that is called when the button is clicked, which is used to execute the code that displays a message box with the text entered in the text box.

πŸ’‘Message Box

A Message Box is a small dialog window that displays a message to the user. It is a common UI element used for alerting users or gathering simple input. In the video, a Message Box is used to display text entered by the user in a text box when a button is clicked, demonstrating how to handle events and interact with the user in a Windows Form Application.

Highlights

Introduction to creating a Windows Form application in C# using Visual Studio.

Creating a new project in Visual Studio and selecting the Windows Form application template.

Choosing the .NET Core framework for the Windows Form application.

Naming the project and setting up the framework version.

Overview of the project solution and form file structure in Visual Studio.

Accessing the form designer to modify the form's appearance.

Changing the form's title to customize the application's name.

Using the toolbox to add components like buttons and labels to the form.

Customizing component properties such as text and anchoring.

Adjusting the form's behavior and appearance through properties.

Demonstration of resizing components and form with anchoring.

Changing the background color of the form and its components.

Writing code to handle button click events and display text from a textbox.

Using the MessageBox component to display user input.

Explanation of how to retrieve text from a textbox and use it in code.

Running the application to test the functionality of the form and components.

Demonstration of the form resizing and button click functionality in action.

Conclusion and invitation to the next video in the series.

Transcripts

play00:00

hey guys in this video I'm going to show

play00:01

you how you can create your first

play00:03

Windows Form application since C sharp

play00:06

on your Visual Studio

play00:08

so let's get started and let's see how

play00:10

we can do it

play00:11

so when you open Visual Studio it will

play00:14

look like this here we are going to

play00:17

create a new project so just click on

play00:20

create a new project

play00:22

and then here either you can search for

play00:25

Windows Form application or you can just

play00:28

select these drop Doms and select C

play00:30

sharp here and the platform will be

play00:33

desktop so you can just select windows

play00:38

for example and then you can choose the

play00:42

all project type and the project type

play00:44

here is desktop and from here you can

play00:49

see two Windows Form app options so here

play00:53

we can select this option which is

play00:56

Windows Form app using.net core so just

play01:00

select this option and then click on

play01:02

next

play01:03

on the next window you can give the name

play01:06

of your project I'm going to name my

play01:08

project as my first project for example

play01:13

and you can see I don't like to give

play01:16

spaces in the project name so if

play01:19

possible just write or give a project

play01:22

name without spaces right and then click

play01:25

on next

play01:26

and here the framework will be dot net

play01:29

6.0 or whatever is the latest version of

play01:33

dotnet in my case it's dotnet 6.0 click

play01:37

on create

play01:39

and this is going to create a Windows

play01:42

Form application in c-sharp for you so

play01:45

now you can see my project has been

play01:47

created

play01:48

you will be able to see this kind of

play01:51

solution on the left hand side and on

play01:54

the right hand side you can see this

play01:57

form one dot CS file so this is the

play02:00

program file where you will write the

play02:03

program

play02:04

now how you can open the designer you

play02:06

can see this form one dot CS file right

play02:10

and when you just expand this uh you

play02:14

will be able to see C sharp form one

play02:16

designer also here right so when you

play02:18

double click on this form one dot CS

play02:21

file it's also going to open the

play02:23

designer for you so just wait for the

play02:26

designer to load for this form one so

play02:29

now you can see this form one dot CS

play02:31

design window is open

play02:34

you can resize

play02:36

this form one window here you can even

play02:41

give the title to it right now the title

play02:44

here is form one you can change the text

play02:47

of this let's say you want to change the

play02:50

text to uh my app and then press enter

play02:56

the title has been changed to my app

play02:59

right so you can change the text for

play03:03

your form by just changing the title

play03:07

here you have all the other options to

play03:10

change the appearance of your form or

play03:14

you can even change the font size

play03:16

if you are using some fonts in your form

play03:20

or you can also change the behavior of

play03:24

your form so you can drag and drop some

play03:28

components in your form from the toolbox

play03:30

so if you don't see the toolbox just

play03:33

click on this toolbox option here which

play03:35

is going to load all the toolbox if you

play03:39

want to pin the toolbox

play03:41

window you can just click on pin and to

play03:45

box window will be pinned here so you

play03:47

can easily drag and drop all the

play03:50

components from here

play03:52

so you can see

play03:54

all Windows Form components you can see

play03:57

buttons labels and other components

play04:00

I'm going to use a button so I'm going

play04:04

to just drag and drop this button into

play04:08

my form I can even use the label so I

play04:12

can drag and drop this label into my

play04:15

form

play04:16

and let me also use the text box so let

play04:21

me search for

play04:23

this text box and put it here okay

play04:27

so I can

play04:29

you can see a drag and drop all these

play04:32

components here now for this label

play04:36

I can change the label text

play04:39

from this

play04:41

properties window so the text here is

play04:45

label one I can just write

play04:48

enter some text for example or enter

play04:51

your value because I want the user to

play04:54

enter something into the text box right

play04:57

so I'm going to just write enter

play05:00

some

play05:02

text here and then press enter and you

play05:04

can see this label shows that I will

play05:08

just drag this text box here also

play05:12

and now

play05:14

in the behavior you will be able to see

play05:17

uh all these Behavior options

play05:21

I want to change the anchor option here

play05:24

so you can see the anchor is right now

play05:27

top and left I can even

play05:32

click on this option to Anchor it to the

play05:35

right side so what it's going to do is

play05:38

let me just

play05:40

resize it according to my form

play05:44

and I can even

play05:48

do the same for this label but I do not

play05:50

need this for the label

play05:53

so what I it's going to do is when I

play05:55

anchor it to the right when I resize my

play05:59

window you can see the text box resizes

play06:02

right

play06:03

same I can do with the button also so

play06:08

this button text says button one I'm

play06:10

going to just change it to

play06:14

click me for example

play06:17

and then press enter and you can see the

play06:19

button text is changed

play06:21

I can even change the anchor for it so

play06:25

right now it's top left I can anchor it

play06:28

to the right and then I can just move it

play06:32

here and

play06:34

then just

play06:36

resize my button

play06:38

until it shows this kind of

play06:43

line here right

play06:45

so now

play06:46

when I even resize my window it's going

play06:52

to uh

play06:53

just resize according to the width of my

play06:58

frame right I can even anchor this

play07:01

button to the bottom so that

play07:04

it moves to the bottom let's see how can

play07:07

we do that so I can anchor it to the

play07:09

bottom and then

play07:12

what happens is when I just make it

play07:15

bigger this button size becomes bigger

play07:17

for example right so this is how you can

play07:20

change the behavior so you need to play

play07:22

with all these options and see what you

play07:24

can do

play07:25

for example I can change the background

play07:27

color from here

play07:29

so here I have all these options for

play07:34

changing the background color

play07:36

let's say I want to just choose this one

play07:39

and then you can see the background

play07:41

color of my form has been changed and

play07:46

the background color of my button also

play07:48

has been changed

play07:50

so now let's write some code so what I

play07:53

want to do here is whenever some text is

play07:56

entered into this text box and when I

play08:00

click the button I want to open a

play08:03

message box with the same text for

play08:07

example right so

play08:09

the main thing to note here is the name

play08:12

of this text box so when I select this

play08:15

text box the name of the text box is

play08:18

text box one so for using this name I'm

play08:22

going to use this text box text and show

play08:27

the text on the message box for example

play08:31

right so just copy this ah name from

play08:36

here and then select your click me

play08:40

button just double click on this button

play08:42

which is going to

play08:44

create a listener for your button right

play08:47

so the button name was Button one you

play08:51

can even verify that by going here and

play08:55

then you can see the name of this button

play08:59

you can see the name of the button is

play09:01

button one and the method which is

play09:04

created is button one click listener

play09:07

right so let me just write some code I'm

play09:11

going to create a

play09:12

string variable I am going to name it as

play09:16

text is equal to and I am going to take

play09:20

the name of my text box right which was

play09:24

text box one so I'm going to just write

play09:27

Dot

play09:28

text which is going to uh

play09:31

give me the text whatever I write in the

play09:34

text box it's going to just take the

play09:36

text and assign it to the text variable

play09:39

right now I want to show this text into

play09:42

a message box so I can just use a

play09:47

message box component just write

play09:50

message box

play09:53

dot show

play09:55

and then

play09:56

I can just write

play10:00

U entered and then semicolon space and

play10:05

then plus and then the text variable

play10:09

which is text okay once you are done

play10:12

just save your code and then you can run

play10:16

your code by just clicking on this run

play10:19

Icon so I'm going to run my code

play10:23

and hopefully everything works fine you

play10:25

can see my code is running

play10:28

when I resize you can see the button and

play10:31

the text box resizes according to the

play10:33

size when I move down the button size is

play10:37

changed right now let me enter something

play10:40

so I'm going to just write

play10:43

Dom here and then I'm going to click on

play10:46

click me button and it says you entered

play10:49

Dom right I can just write 34 here and

play10:55

I'm going to just click on click me and

play10:58

it says you entered 34 okay

play11:02

so it is going to open this message box

play11:04

every time when I click on the button

play11:07

and it's going to show whatever text I

play11:09

wrote here

play11:11

so this is the basic Foundation of how

play11:15

you can create a Windows Form

play11:17

application in Visual Studio using C

play11:21

sharp and create your forms and use your

play11:25

forms in Windows Form application so I

play11:30

hope you've enjoyed this video and I

play11:32

will see you in the next video

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
Windows FormsC# ProgrammingVisual StudioApp Development.NET CoreUI DesignCoding TutorialSoftware CreationProgramming GuideDeveloper Tools