Code for Windows Forms Applications

This topic is designed to help you find code that demonstrates how to perform common programming tasks in Windows Forms applications. The topics listed are code-oriented topics. If you are using Visual Studio, you can use the Windows Forms Designer to complete many of these tasks.

The tasks are divided into the following categories:

  • Application creation

  • Form manipulation

  • Dialog boxes

  • Data binding

  • MDI forms

  • Menu manipulation

  • Graphics and GDI+

  • Printing

  • Drag-and-drop operations

  • Handling user input

  • Interacting with COM components

Application Creation

How to: Create a Windows Forms Application from the Command Line

Form Manipulation

How to: Change the Borders of Windows Forms

How to: Resize Windows Forms

How to: Respond to Font Scheme Changes in a Windows Forms Application

Dialog Boxes

How to: Display Dialog Boxes for Windows Forms

Data Binding

How to: Bind a Windows Forms Control to a Type

How to: Handle Errors and Exceptions that Occur with Databinding

How to: Share Bound Data Across Forms Using the BindingSource Component

How to: Ensure Multiple Controls Bound to the Same Data Source Remain Synchronized

How to: Raise Change Notifications Using a BindingSource and the INotifyPropertyChanged Interface

How to: Bind a Windows Forms Control to a Factory Object

MDI Forms

How to: Create an MDI Window List with MenuStrip (Windows Forms)

How to: Add Menu Items to a ContextMenuStrip

How to: Associate a ContextMenuStrip with a Control

How to: Disable ToolStripMenuItems

How to: Hide ToolStripMenuItems

How to: Insert a MenuStrip into an MDI Drop-Down Menu (Windows Forms)

How to: Add Enhancements to ToolStripMenuItems

Graphics and GDI+

How to: Create Graphics Objects for Drawing

How to: Create a Pen

How to: Set the Color of a Pen

How to: Create a Solid Brush

How to: Draw Text on a Windows Form

How to: Draw Text with GDI

How to: Render Images with GDI+

Printing

How to: Create Standard Windows Forms Print Jobs

How to: Capture User Input from a PrintDialog at Run Time

How to: Choose the Printers Attached to a User's Computer in Windows Forms

How to: Print Graphics in Windows Forms

How to: Print a Multi-Page Text File in Windows Forms

How to: Complete Windows Forms Print Jobs

How to: Display Print Preview in Windows Forms Applications

Drag-and-Drop Operations

How to: Add Data to the Clipboard

How to: Retrieve Data from the Clipboard

How to: Perform Drag-and-Drop Operations Between Applications

Handling User Input

How to: Modify Keyboard Input to a Standard Control

How to: Modify Keyboard Input to a Standard Control

How to: Handle Keyboard Input at the Form Level

How to: Distinguish Between Clicks and Double-Clicks

How to: Simulate Mouse and Keyboard Events in Code

How to: Handle User Input Events in Windows Forms Controls

Interacting with COM Components

How to: Support COM Interop by Displaying Each Windows Form on Its Own Thread

How to: Support COM Interop by Displaying a Windows Form with the ShowDialog Method

See Also

Other Resources

Windows Forms Controls Samples

Windows Forms Reference

Getting Started with Windows Forms

Enhancing Windows Forms Applications

Windows Forms Controls