Common C# Windows Forms Controls and Development Patterns

Keyboard Shortcuts and Form Basics

Press F7 to switch from Form Designer to Code View, and Shift+F7 to return to the designer.

To prevent users from resizing a form after initialization, set the FormBorderStyle property to FixedSingle. For a borderless form, use None. The background color can be customized using the color picker.

Event Management

To quickly remove an event handler from a control, open the Properties window, switch to Events view, right-click the event, and select Reset. This removes both the event registration and the associated event handler code.

In button click events, the sender paramter can be accessed as follows:

string buttonText = ((Button)sender).Text;

Control Configuration

PictureBox

For standard image display, set the SizeMode property to StretchImage.

Tab Order

To configure tab order on a form:

  1. Select the form
  2. Go to View → Tab Order
  3. Click controls in the desired order (controls not clicked are excluded)
  4. Exit Tab Order mode by selecting View → Tab Order again

TextBox Password Input

Set UseSystemPasswordChar to true for password fields. Typically, also set BorderStyle to FixedSingle.

MenuStrip

The MenuStrip control can be accessed via the keyboard shortcut Alt+M. To add keyboard shortcuts to menu items, append (&X) where X is the desired key. To add images to menu items, right-click and select Image. To add separator lines, use the same context menu.

Menu items under a dropdown are commnoly prefixed with tsmi.

Adding Images to Controls

Use the Image property or the ImageList component to quickly add images to controls.

Transparent Backgrounds

To make a control's background transparent, set BackColor to Transparent in the Web color section.

DateTimePicker Format

To display dates in "2019-05-04" format:

  1. Set Format to Custom
  2. Set CustomFormat to "yyyy-MM-dd"

Embedding Forms

To embed a child form into a container control:

foreach (Control child in this.splitContainer1.Panel2.Controls)
{
    if (child is Form)
    {
        Form existingForm = (Form)child;
        existingForm.Close();
    }
}

FrmAddStudent newForm = new FrmAddStudent();
newForm.TopLevel = false;
newForm.WindowState = FormWindowState.Maximized;
newForm.FormBorderStyle = FormBorderStyle.None;
newForm.Parent = this.splitContainer1.Panel2;
newForm.Show();

DataGridView Configuration

Recommended Settings

  • Disable built-in adding, editing, and deleting functionality
  • Set BackgroundColor to ButtonFace
  • Set the last column's AutoSizeMode to Fill
  • For column header height to auto-adjust with font size, set ColumnHeaderHeightSizeMode to AutoSize and enable EnabledResizing first
  • Center column headers by setting Alignment to MiddleCenter in ColumnHeaderDefaultCellStyle
  • To customize column header appearance, set EnableHeaderVisualStyles to false

Data Binding

Use the DataSource property to load data in bulk rather than populating cells individually. When binding to a Student class with four properties, ensure the DataGridView columns are named identically to the class property names.

UI Design Patterns

Labels

Set the font to Microsoft YaHei for a modern appearance.

Buttons and CheckBoxes

For a flat design, set FlatStyle to Flat. To customize button borders, use FlatAppearance to adjust border color and width.

Borderless Form Dragging

When a form has no border, implement custom drag functionality:

private Point dragPosition;
private bool isDragging;

private void FrmMain_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        dragPosition = new Point(-e.X, -e.Y);
        isDragging = true;
    }
}

private void FrmMain_MouseMove(object sender, MouseEventArgs e)
{
    if (isDragging)
    {
        Point newPosition = Control.MousePosition;
        newPosition.Offset(dragPosition.X, dragPosition.Y);
        Location = newPosition;
    }
}

private void FrmMain_MouseUp(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        isDragging = false;
    }
}

Button with Image

To place an image on the left side of a button, use the ImageAlign property set to Left. Text alignment can also be set to Left. Add leading spaces before text for additional spacing.

Form Design Templates

Login Form

  1. Create a new form with no border
  2. Add a PictureBox with appropriate SizeMode
  3. Apply flat style to buttons
  4. Customize text box borders

Main Application Form

  1. Create a new borderless form
  2. Add header panels with background colors or images
  3. Configure SplitContainer controls with appropriate properties
  4. Add navigation buttons in the sidebar with custom styling

Tags: C# Windows Forms UI Controls WinForms Desktop Development

Posted on Thu, 07 May 2026 19:29:22 +0000 by nazariah