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:
- Select the form
- Go to View → Tab Order
- Click controls in the desired order (controls not clicked are excluded)
- 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:
- Set
FormattoCustom - Set
CustomFormatto"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
BackgroundColortoButtonFace - Set the last column's
AutoSizeModetoFill - For column header height to auto-adjust with font size, set
ColumnHeaderHeightSizeModetoAutoSizeand enableEnabledResizingfirst - Center column headers by setting
AlignmenttoMiddleCenterinColumnHeaderDefaultCellStyle - To customize column header appearance, set
EnableHeaderVisualStylestofalse
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
- Create a new form with no border
- Add a PictureBox with appropriate
SizeMode - Apply flat style to buttons
- Customize text box borders
Main Application Form
- Create a new borderless form
- Add header panels with background colors or images
- Configure SplitContainer controls with appropriate properties
- Add navigation buttons in the sidebar with custom styling