C# Windows Forms Tutorial - Learn C# GUI Programming With Windows Forms
C# Windows Forms Tutorial - Learn C# GUI Programming With Windows Forms
C# Windows Forms tutorial teaches the basics of GUI programming with C# & Windows Forms. In
our tutorial, we will build our applications manually; we will not use Form designers.
Windows Forms
Windows Forms, sometimes abbreviated as Winforms, is a graphical user interface application
programming interface (API) included as a part of Microsoft's .NET Framework.
Windows Forms allows to create graphically rich applications that are easy to deploy and update.
The applications are more secure than traditional Windows-based applications.
Tweet
Like 6 Share
In December 2018, Microsoft announced releasing Windows Forms as open source project on
GitHub. It is released under the MIT License. With this release, Windows Forms has become
available on the .NET Core framework. Windows Forms is available on Windows only.
A new template for the Windows Forms application is created with the dotnet new winforms
command.
$ dotnet run
We create the template of the Windows Forms application. The command also generates
Form1.Designer.cs and Form1.cs files. We will not use them and they can be safely deleted.
Program.cs
using System;
using System.Windows.Forms;
using System.Drawing;
namespace First
{
public class MyForm : Form
{
public MyForm()
{
InitComponents();
}
[STAThread]
static void Main()
{
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.Run(new MyForm());
}
}
}
The example displays a main window on the screen. The window is centered.
using System;
using System.Windows.Forms;
using System.Drawing;
public MyForm()
{
InitComponents();
}
Text and Size are properties of a form. Changing these properties, we modify our form control.
The first line displays text "First application" in the titlebar of the form control. The second line
sets the size of the client area of the form. The CenterToScreen() method centers the form on the
screen.
[STAThread]
static void Main()
{
...
}
The Main() method is an entry point to the application. Windows Forms applications must declare
the [STAThread] attribute; otherwise, the controls might not work correctly. This tells to use
single-threaded apartment model instead of multi-threaded.
Application.SetHighDpiMode(HighDpiMode.SystemAware);
With the SetHighDpiMode() method, we ensure that our application looks good on any display
resolution.
Application.EnableVisualStyles();
The EnableVisualStyles() method enables visual styles. The application will use the built-in
Windows theming to style controls instead of the classic Windows look and feel.
Application.Run(new MyForm());
The Run() method starts the application. It begins running a standard application message loop on
the current thread, and makes the specified form visible.
Program.cs
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Tooltips
{
public class MyForm : Form
{
private FlowLayoutPanel flowPanel;
public MyForm()
{
InitComponents();
}
flowPanel.Dock = DockStyle.Fill;
flowPanel.BorderStyle = BorderStyle.FixedSingle;
flowPanel.Controls.Add(button);
flowPanel.Controls.Add(button2);
Controls.Add(flowPanel);
CenterToScreen();
}
[STAThread]
static void Main()
{
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.Run(new MyForm());
}
}
}
The code example creates a tooltip for two controls: one Button control and the Form control.
We place two buttons on the FlowLayoutPanel. It dynamically lays out its contents horizontally or
vertically. (The default dimension is vertical.)
We create a new tooltip. With the SetToolTip(), we assign the tooltip to the FlowLayoutPanel
control.
flowPanel.Dock = DockStyle.Fill;
flowPanel.Controls.Add(button);
flowPanel.Controls.Add(button2);
Controls.Add(flowPanel);
The buttons are added to the flow panel and the flow panel is added to the form.
Ad
Program.cs
using System;
using System.Windows.Forms;
using System.Drawing;
namespace QuitButton
{
class MyForm : Form
{
private FlowLayoutPanel flowPanel;
public MyForm()
{
InitComponents();
}
flowPanel.Dock = DockStyle.Fill;
flowPanel.BorderStyle = BorderStyle.FixedSingle;
button.Text = "Quit";
button.AutoSize = true;
button.Click += new EventHandler(OnClick);
flowPanel.Controls.Add(button);
Controls.Add(flowPanel);
CenterToScreen();
}
[STAThread]
static void Main()
{
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.Run(new MyForm());
}
}
}
The example creates a Quit button control; the application terminates when we click on the button.
The button has some margin around its borders. We add some space to the left and above the
button control.
We plug an event handler to the Click event. When we click on the button, the OnClick() method
is called.
namespace LabelEx
{
public class MyForm : Form
{
public MyForm()
{
InitUI();
}
Text = "Label";
AutoSize = true;
CenterToScreen();
}
[STAThread]
static void Main()
{
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.Run(new MyForm());
}
}
}
The label control is created. It is located at the x=10, y=10 coordinate on the form.
Text = "Label";
AutoSize = true;
CenterToScreen();
The main window is automatically sized to fit the lyrics.
Program.cs
using System;
using System.Windows.Forms;
using System.Drawing;
namespace CheckBoxEx
{
class MyForm : Form
{
private FlowLayoutPanel FlowPanel;
private CheckBox cb;
public MyForm()
{
InitUI();
}
cb = new CheckBox();
cb.Margin = pad;
cb.Parent = this;
cb.Text = "Show Title";
cb.AutoSize = true;
cb.Checked = true;
FlowPanel.Controls.Add(cb);
Controls.Add(FlowPanel);
CenterToScreen();
}
[STAThread]
static void Main()
{
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.Run(new MyForm());
}
}
}
The code example shows or hides the title of the window depending on its state.
var pad = new Padding(20);
cb = new CheckBox();
cb.Margin = pad;
cb.Parent = this;
cb.Text = "Show Title";
cb.AutoSize = true;
cb.Checked = true;
When the application starts, we show the title. And we set the CheckBox control to checked state.
Depending on the value of the Checked property, we toggle the title of the window.
namespace MenuEx
{
class MyForm : Form
{
public MyForm()
{
Text = "Simple menu";
ms.Items.Add(fileMenuItem);
MainMenuStrip = ms;
ClientSize = new Size(450, 300);
CenterToScreen();
}
[STAThread]
static void Main()
{
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.Run(new MyForm());
}
}
}
In our example, we have a menubar and one menu. Inside a menu there is one menu item. If we
select the menu item, application is closed.
The application can be closed also by using the Ctrl+X shorcut or by pressing Alt, F, E keys.
MenuStrip creates a menu system for our form. We add ToolStripMenuItem objects to the
MenuStrip that represent the individual menu commands in the menu structure. Each
ToolStripMenuItem can be a command for your application or a parent menu for other submenu
items.
The exit menu item is added to the drop down items of the menu object.
ms.Items.Add(fileMenuItem);
MainMenuStrip = ms;
The MenuStrip is plugged into the form. In other words, the menubar is added to the main
window of the application.
Program.cs
using System;
using System.Drawing;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace RectanglesEx
{
class Program : Form
{
public Program()
{
InitUI();
}
private void InitUI()
{
Text = "Rectangles";
Paint += new PaintEventHandler(OnPaint);
[STAThread]
static void Main()
{
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.Run(new Program());
}
}
}
Graphics g = e.Graphics;
In order to paint on the form, we must get the Graphics object. Painting on a form is actually
calling various methods of the Graphics object.
The FillRectagle() method fills a specified rectangle with a brush. A brush can be a colour or a
pattern. There are some predefined colours available. We can get them from the Brushes
enumeration. The last four values are the x, y values of the topleft point and the width and height of
the rectangle.
In this tutorial, we have created simple GUI applications in C# and Windows Forms.