0% found this document useful (0 votes)
9 views

Introduction to Visual Studi

The document provides an introduction to Visual Studio, C#, and .NET, focusing on creating a basic Windows Desktop Application using the Windows Forms App template. It includes step-by-step code examples and covers essential concepts such as using the Solution Explorer, Toolbox, Designer, Event Handlers, and creating Classes and Methods. This tutorial aims to equip readers with foundational skills for developing various applications using Visual Studio and C#.

Uploaded by

berna
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Introduction to Visual Studi

The document provides an introduction to Visual Studio, C#, and .NET, focusing on creating a basic Windows Desktop Application using the Windows Forms App template. It includes step-by-step code examples and covers essential concepts such as using the Solution Explorer, Toolbox, Designer, Event Handlers, and creating Classes and Methods. This tutorial aims to equip readers with foundational skills for developing various applications using Visual Studio and C#.

Uploaded by

berna
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

https://www.halvorsen.

blog

Visual Studio and C#

Hans-Petter Halvorsen
Contents
• Introduction to Visual Studio, C# and .NET
• How to create a basic Windows Desktop Application
• Step-by-step Code Examples using the “Windows Forms
App” template will be provided
• You will learn to use the Solution Explorer, use the Toolbox,
use the Designer to create User Interface, set Properties,
create and use Event Handlers, create and use Variables, use
built-in Methods in your code, create a Method and finally
create a Class with Methods
• This will give you the foundation for creating any kind of
Application using Visual Studio and C#
https://www.halvorsen.blog

Introduction

Table of Contents Hans-Petter Halvorsen


Visual Studio
• Visual Studio is an Integrated Development
Environment (IDE) from Microsoft
• You can use it to create Desktop Applications, Web
Applications, etc.
• You can use different Programming Languages (C#,
VB.NET and F#), but C# is the default option
• Visual Studio comes in 3 different editions;
Professional, Enterprise and Community (free)
• https://visualstudio.microsoft.com/
Visual Studio
C#
• C# (pronounced “C-Sharp”) is a Programming Language
• Developed by Microsoft
• It runs on the .NET Framework
• C# is an Object-oriented Programming (OOP) Language
• C# is one of the most popular Programming Languages
today
• Flexible Language:
– Can be used for many different types of Applications; Desktop
Applications, Web Applications, Mobile Apps, ..
– All integrated into the Visual Studio IDE
.NET
• .NET is a free, open-source and cross-platform
application platform supported by Microsoft
• In .NET you can choose between different
Programming Languages, but C# is the most used
and recommended .NET language today
• Basically, .NET is just a huge code library hiding all
the “dirty work” to make it easy to make different
types of Applications, either it is Desktop
Applications, Web Applications or Mobile
Applications
.NET from the beginning till today
4.8.x is the latest version .NET Core and .NET 5 was a
dramatically change in the
Before .NET Framework and C# of .NET Framework. architecture and not compatible with
that was released in 2002, we used
Visual Studio with Visual Basic
4.8.x .NET Framework. That’s why we still
.NET Framework need to choose between the old .NET
Framework, .NET Core and the new
.NET Framework 4.8.x default .NET in Visual Studio today

Finally, .NET Framework and .NET


.NET Framework Core was merged into the new .NET 5 .NET
1.0 (2002) 2.0 3.0 4.0 Open Source and Cross Platform .NET 5 .NET 6, 7, 8, ..

.NET Core
1.0
2002 2020 Today
Windows Forms
• As mentioned, with .NET you can create all kind of Applications,
including Windows Desktop Applications, Web Applications, Mobile
Applications, etc. using the same tool/IDE (Visual Studio) and the
same Programming Language
• When it comes to Windows Desktop Applications you also have many
choices, like Windows Forms Applications, WPF Applications,
Windows Store Applications, etc.
• Windows Forms Applications is probably the most used of alle these
alternatives
• So, here in this tutorial we will focus on Windows Forms Applications
Visual Studio - Create New Project
Use this if you depends
on 3.party libraries and
backward combability
The old .NET Framework Windows
Forms Application Template
.NET Core and .NET 5 was a dramatically
change in the architecture and not
compatible with .NET Framework. That’s
why we still need to choose between the The new default .NET Windows
old .NET Framework, .NET Core and the Forms Application Template
new default .NET in Visual Studio today Use this for all new Applications
https://www.halvorsen.blog

Windows Forms
Application
Table of Contents Hans-Petter Halvorsen
Windows Forms Example
• Use the Toolbox
• Use the Designer to create User Interface
• Set Properties
• Event Handlers
• Create and use Variables
• Use built-in Methods in your code
• Create a Method
• Create a Class with Methods
Windows Forms App
Windows Forms App
Toolbox and Designer
Properties
Create GUI
Create C# Code
Running Application
Improvements
• Change Widows Title from “Form1” to something
meaningful in Properties window
• Disable “Maximize” window in Properties window
• Enable Button if TextBox is empty
• Use If .. Else
• Split into First Name and Last Name
• .. (lots of more improvements can be made)
Updated Application
Updated Code
Final Code
using System.Windows.Forms;

namespace HelloWorld
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

btnClick.Enabled = false;
}

private void btnClick_Click(object sender, EventArgs e)


{
string fullName = txtName.Text;

var subNames = fullName.Split(' ');


string firstName = subNames[0];
string lastName = subNames[1];

string message = "Your First Name is: " + firstName + ". And your Last Name is: " + lastName;

MessageBox.Show(message);
}

private void txtName_TextChanged(object sender, EventArgs e)


{
string name = txtName.Text;

if (name.Length > 0)
btnClick.Enabled = true;
else
btnClick.Enabled = false;
}
}
}
Updated Application
Classes and Methods
We will update our Application:
• We will create a separate Method
• We will create a separate Class and put the
Method inside the Class
Method
Creating the Method:
string SplitFullName(string fullName)
{
var subNames = fullName.Split(' ');
string firstName = subNames[0];
string lastName = subNames[1];

string message = "Hello! Your First Name is " + firstName + " and your Last Name is " + lastName;

return message;
} private void btnClick_Click(object sender, EventArgs e)
{
string fullName = txtName.Text;

string message = SplitFullName(fullName);


Using the Method:
MessageBox.Show(message);
}
Final Code
namespace HelloWorld
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

btnClick.Enabled = false;
}

private void btnClick_Click(object sender, EventArgs e)


{
string fullName = txtName.Text;

string message = SplitFullName(fullName);

MessageBox.Show(message);
}

private void txtName_TextChanged(object sender, EventArgs e)


{
string name = txtName.Text;

if (name.Length > 0)
btnClick.Enabled = true;
else
btnClick.Enabled = false;
}

string SplitFullName(string fullName)


{
var subNames = fullName.Split(' ');
string firstName = subNames[0];
string lastName = subNames[1];

string message = "Hello! Your First Name is " + firstName + " and your Last Name is " + lastName;

return message;
}
}
}
Creating the Class and Method:
namespace HelloWorld
Class
{
public class Person
{
public string SplitFullName(string fullName)
{
var subNames = fullName.Split(' ');
string firstName = subNames[0];
string lastName = subNames[1];

string message = "Hello! Your First Name is " + firstName + " and your Last Name is " + lastName;
private void btnClick_Click(object sender, EventArgs e)
return message;
{
}
} string fullName = txtName.Text;
}
Person person = new Person();
string message = person.SplitFullName(fullName);
Using the Class and Method:
MessageBox.Show(message);
}
Final Code
using System.Windows.Forms;

namespace HelloWorld
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

btnClick.Enabled = false;
}

private void btnClick_Click(object sender, EventArgs e)


{
string fullName = txtName.Text;

Person person = new Person();

string message = person.SplitFullName(fullName);

MessageBox.Show(message);
}

private void txtName_TextChanged(object sender, EventArgs e)


{
string name = txtName.Text;

if (name.Length > 0)
btnClick.Enabled = true;
else
btnClick.Enabled = false;
}
}
}
Summary
• We have used Visual Studio and C# and created a basic
Windows Desktop Application.
• Step-by-step Code Examples using the “Windows Forms
App” template have been provided.
• You have learned to use the Solution Explorer, use the
Toolbox, use the Designer to create User Interface, set
Properties, create and use Event Handlers, create and use
Variables, use built-in Methods in your code, create a
Method and finally create a Class with Methods.
• This will give you the foundation for creating any kind of
Applications using Visual Studio and C#.
Hans-Petter Halvorsen
University of South-Eastern Norway
www.usn.no

E-mail: [email protected]
Web: https://www.halvorsen.blog

You might also like