TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Windows Forms

These C# tutorials use Windows Forms, which provides a graphical user interface. They cover the System.Windows.Forms namespace.

Windows Forms supports native Windows applications.

These programs have high-quality user experiences. We gain buttons, text boxes, tabs. Often a GUI is important. Programs are easier to use.

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.

TextBox MaskedTextBox RichTextBox

Based on:

.NET 4

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. 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. 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. 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. 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. 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. 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. 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.)

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. 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.

Button

Chart

Domain

UpDown ErrorProvider EventLog FileSystemWatcher HelpProvider ListView NotifyIcon NumericUpDown PropertyGrid RadioButton SplitContainer TabControl ToolTip TrackBar TreeView WebBrowser

Pointer. There is a Pointer control in Visual Studio. But this does not mean a pointer in the program you are developing. It just is a way to get the pointer back in the VS interface.

Base types. 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. 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.

DataSource Focused ForeColor, BackColor TagText

IntegralHeight. This is an important property. When IntegralHeight or IntegralWidth are set to true, controls show only complete sub-controls. So no partial lines or elements are displayed.

Note: Both settings here have positives and negatives. Using IntegralHeights looks cleaner but may reduce visible area.

Anchor. Anchoring a control attaches it one edge of the containing control. This makes layouts more flexible and easier to develop. We can even anchor directly within a cell of a TableLayoutControl.

Tip: My best advice is to avoid setting pixel coordinates on controls whenever possible.

Tip 2: With no pixel coordinates, we can change many parts of the user interface at once, because they are relative.

Events. 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.

KeyCode PreviewKeyDown TextChanged

Methods. 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.

InitializeComponent MessageBox.Show TextBox.AppendText

Custom. 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 Box Position Windows Single Instance

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

WPF: The Windows Presentation Foundation can be used alongside Windows Forms. We can use these technologies together.

WPF

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.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf