Introduction to Visual Studi
Introduction to Visual Studi
blog
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
.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;
}
string message = "Your First Name is: " + firstName + ". And your Last Name is: " + lastName;
MessageBox.Show(message);
}
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;
btnClick.Enabled = false;
}
MessageBox.Show(message);
}
if (name.Length > 0)
btnClick.Enabled = true;
else
btnClick.Enabled = false;
}
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;
}
MessageBox.Show(message);
}
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