C# Windows Forms

Array Collections File Keyword String .NET Cast Class Compression Data Directive Enum Exception If Interface Lambda LINQ Loop Method Number Process Property Regex Sort StringBuilder Struct Switch Time Windows WPF

Window: Windows Forms

A program may be text-based. It may require no graphical user interface. But often a GUI is important. The Windows Forms platform supports native Windows applications. These applications have high-quality user experiences.

TextBox

This Windows Forms program uses C# code to set the properties of a TextBox control on the Load event. We run the program. The TextBox will then show the string "Dot Net Perls" in a large font style.

Here:I present text entry controls, including TextBox, MaskedTextBox and RichTextBox.

TextBoxMaskedTextBoxRichTextBox
Based on:

.NET 4.0

Windows Forms program with TextBox: C#

using System;
using System.Drawing;
using System.Windows.Forms;

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

	private void Form1_Load(object sender, EventArgs e)
	{
	    this.textBox1.Font = new Font("Consolas", 15.0f);
	    this.textBox1.Text = "Dot Net Perls";
	}
    }
}

Threading

BackgroundWorker

The easiest way to add a background thread to your program is with BackgroundWorker. In addition to this, you can use a ProgressBar to report that thread's progress on the user interface.

BackgroundWorker

ProgressBar:Has the program crashed or frozen? A ProgressBar helpfully informs the user it has not (unless it did crash after all).

ProgressBar

Boxes

In Windows Forms, boxes such as CheckBox provide an interface for users to select options. And controls like ListBox provide even more advanced selection and visualization options.

CheckBoxCheckedListBoxComboBoxGroupBoxListBox

Menus, toolbars

Concept

On the desktop, menus and toolbars are used throughout successful user interfaces. Users know how to interact with these elements. And for this reason they are worth considering in every desktop program.

ContextMenuStripDropDownItemsMenuStripStatusStripToolStripContainer

Calendars

Calendar: June 15

It is possible to implement your own calendar in Windows Forms. But this would be much more work than using a DateTimePicker or MonthCalendar control. And usually in interface design the simplest option is best.

DateTimePickerMonthCalendar

Images

Image

These articles describe how to use images within Windows Forms programs. With ImageList, we store a list of images that are not directly displayed. Instead they are used in other controls.

ImageListPictureBox

Panels

TableLayoutPanel

With panels in Windows Forms, you can easily arrange sub-controls in your form. Controls can then be positioned automatically as the window resizes, making for a better user experience.

FlowLayoutPanelPanelTableLayoutPanel

Labels

You can also use Label controls to insert text or other elements into your Windows Forms programs. We demonstrate the Label and LinkLabel controls—which may be harder to use than they are first appear.

LabelLinkLabel

Dialogs

DialogResult example

A dialog is a window that must be dismissed before further action can be taken by the user. There are several examples on dialog boxes. It is easier to use the packaged dialogs instead of creating your own.

ColorDialogDialogResultFolderBrowserDialogFontDialogOpenFileDialogSaveFileDialog

DataGridView

DataGridView

Windows Forms provides the useful DataGridView control, which allows you to display structured data from memory or from any database in an automated way. In Windows Forms, you do not need to develop your own grid logic.

DataGridView

Also:There are some related DataGridView tutorials. In these we provide step-by-step instructions.

DataGridView TutorialList to DataTable

Here:This method (Form1_Load) adds one column and then three rows based on that column to a DataGridView instance.

Code that uses DataGridView: C#

private void Form1_Load(object sender, EventArgs e)
{
    // One column.
    dataGridView1.Columns.Add("Name", "Name");
    // Three rows.
    var rows = dataGridView1.Rows;
    rows.Add("Dot");
    rows.Add("Net");
    rows.Add("Perls");
}

Result
    (Windows Forms program with one column and three rows.)
DataTable

Columns. You can specify the columns in your DataGridView using a variety of different approaches. This can be done programmatically or declaratively in Visual Studio. We show how to use the Edit Columns dialog.

Columns

Rows:One really neat effect is to have the row colors alternate. This can make the visual output easier to read.

DataGridView ColorsDataGridView Add Rows

Controls

Examples

There are many different controls in Windows Forms, and many of these are detailed here. These articles follow the same format. They contain a walkthrough for common tasks and some descriptions of relevant details.

ButtonChartDomainUpDownErrorProviderEventLogFileSystemWatcherHelpProviderListViewNotifyIconNumericUpDownPointerPropertyGridRadioButtonSplitContainerTabControlToolTipTrackBarTreeViewWebBrowser

Base types

Base keyword

The Windows Forms framework relies heavily on base types. All controls are derived from the Control type. All forms we develop are derived from the Form type. This helps us handle objects in a unified, consistent way.

ControlForm

Properties

Property

There are also many properties you can change on controls and forms in Windows Forms with the C# language and through the Visual Studio designer. We cover aspects of properties in this platform.

AnchorDataSourceFocusedForeColor, BackColorIntegralHeightTagText

Events

Lightning bolt

Windows Forms is event-driven. You can trigger behaviors in your controls when another action happens, such as when the user clicks the mouse. These articles cover certain events. Many of the other articles here also cover events.

KeyCodePreviewKeyDownTextChanged

Methods

Method call

Many methods are available in Windows Forms. Most are clear and need no explanation. But we touch on specific methods in some detail. The InitializeComponent method is generated automatically by Visual Studio.

InitializeComponentMessageBox.ShowTextBox.AppendText

Custom

Segoe UI

When using Windows Forms, sometimes you cannot use a pre-made or built-in solution to a specific problem. We describe ways you can solve problems using customized solutions, such as new controls or techniques for a specific requirement.

Customized Dialog BoxPosition WindowsSingle InstanceLINQ: keywords

LINQ:We can use LINQ extensions to quickly search controls. Queries and extension methods (like OfType) are helpful.

Query Controls

NGEN:With NGEN, we can optimize the startup of programs. An installer class helps with this process.

NGEN Installer Class

Summary

Web-based user interfaces gain prominence in the business world. But Windows programs hold their place. These programs do not rely on network connectivity. And they may be more secure and reliable.

C#