Walkthrough: Getting Started with the Windows Forms Designer
The Windows Forms Designer provides many tools for building Windows Forms applications. This walkthrough illustrates how to build an application using the various tools provided by the designer. Tasks illustrated in this walkthrough include:
Creating a Windows Forms project.
Arranging controls using snaplines.
Accomplishing designer tasks using smart tags.
Setting margins and padding for your controls.
Arranging controls using a TableLayoutPanel control.
Partitioning your control’s layout by using a SplitContainer control.
Navigating your layout with the Document Outline window.
Positioning controls with the size and location information display.
Setting property values using the Properties windows.
When you are finished, you will have a custom control that has been assembled using many of the layout features available in the Windows Forms Designer. This control implements the user interface (UI) for a simple calculator. The following screen shot shows the general layout of the calculator control.
Calculator UI
Prerequisites
In order to complete this walkthrough, you will need:
- Sufficient permissions to be able to create and run Windows Forms application projects on the computer where Visual Studio is installed.
Note
The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.
Creating the Custom Control Project
The first step is to create the DemoCalculator
control project.
To create the custom control project
On the File menu, point to New, and then click Project to open the New Project dialog box.
Select the Windows Control Library project template from the list of Visual C# projects, type DemoCalculatorLib in the Name box, and then click OK.
In Solution Explorer, right-click UserControl1.cs, and then click Rename. Change the file name to DemoCalculator.cs. Click the Yes button when you are asked if you want to rename all references to the code element "UserControl1".
The Windows Forms Designer currently shows the designer surface for the
DemoCalculator
control. In this view, you can graphically design the appearance of your control by selecting controls and components from the Toolbox and placing them on the designer surface. For more information on custom controls, see Varieties of Custom Controls.
Designing the Control Layout
The DemoCalculator
control contains several Windows Forms controls. In this procedure, you will arrange the controls using some of the rapid application development (RAD) features of the Windows Forms Designer.
To design the control layout
In the Windows Forms Designer, change the
DemoCalculator
control to a larger size by clicking the sizing handle in the lower-right corner and dragging it down and to the right. In the lower-right corner of Visual Studio, find the size and location information for controls. Set the size of the control to a width of 500 and a height of 400 by watching the size information as you resize the control.In the Toolbox, click the Containers node to open it. Select the SplitContainer control and drag it onto the designer surface.
The SplitContainer is placed on the
DemoCalculator
control's designer surface.Note
The SplitContainer control sizes itself to the fit the size of the
DemoCalculator
control. Examine the Properties window to see the property settings for the SplitContainer control. Find the Dock property. Its value is Fill, which means the SplitContainer control will always size itself to the boundaries of theDemoCalculator
control. Resize theDemoCalculator
control to verify this behavior.In the Properties window, change the value of the Dock property to None.
The SplitContainer control shrinks to its default size. Its size no longer follows the size of the
DemoCalculator
control.Click the smart tag glyph () on the upper-right corner of the SplitContainer control. Click Dock in parent container to set the Dock property to Fill.
The SplitContainer control docks to the
DemoCalculator
control's boundaries.Note
Several controls offer smart tags to facilitate design. For more information, see Walkthrough: Performing Common Tasks Using Smart Tags on Windows Forms Controls.
Click the vertical border between the panels and drag it to the right, so that most of the space in taken by the left panel.
The SplitContainer divides the
DemoCalculator
control into two panels, with a movable border separating them. The panel on the left will hold the calculator buttons and display, and the panel on the right will show a record of the arithmetic operations performed by the user.In the Properties window, change the value of the BorderStyle property to Fixed3D.
In the Toolbox, click the Common Controls node to open it. Select the ListView control and drag it into the right panel of the SplitContainer control.
Click the ListView control's smart tag glyph. In the smart tag panel, change the View setting to Details.
In the smart tag panel, click Edit Columns.
The ColumnHeader Collection Editor dialog box opens.
In the ColumnHeader Collection Editor dialog box, click the Add button to add a column to the ListView control. Change the value of the column's Text property to History. Click OK to create the column.
In the smart tag panel, click Dock in parent container, and then click the smart tag glyph to close the smart tag panel.
From the Containers node in the Toolbox, drag a TableLayoutPanel control into the left panel of the SplitContainer control.
The TableLayoutPanel control appears on the designer surface with its smart tag panel open. The TableLayoutPanel control arranges its child controls in a grid. For more information, see Walkthrough: Arranging Controls on Windows Forms Using a TableLayoutPanel. The TableLayoutPanel control will hold the
DemoCalculator
control's display and buttons.Click Edit Rows and Columns on the smart tag panel.
The Column and Row Styles dialog box opens.
Click the Add button until five columns are displayed. Select all five columns, and then click the Percent option button in the Size Type box. Set the Percent value to 20. This sets each column to the same width.
In the Show drop-down box, click Rows.
Click the Add button until five rows are displayed. Select all five rows, and the click the Percent option button in the Size Type box. Set the Percent value to 20. This sets each row to the same height.
Click OK to accept your changes, and then click the smart tag glyph to close the smart tag panel.
In the Properties window, change the value of the Dock property to Fill.
Populating the Control
Now that the layout of the control is set up, you can populate the DemoCalculator
control with buttons and a display.
To populate the control
In the Toolbox, double-click the TextBox control icon.
A TextBox control is placed in the first cell of the TableLayoutPanel control.
In the Properties window, change the value of the TextBox control's ColumnSpan property to 5.
The TextBox control moves to a position that is centered in its row.
Change the value of the TextBox control's Anchor property to Left, Right.
The TextBox control expands horizontally to span all five columns.
Change the value of the TextBox control's TextAlign property to Right.
In the Properties window, expand the Font property node. Set Size to 14, and set Bold to true for the TextBox control.
Select the TableLayoutPanel control.
In the Toolbox, double-click the Button icon.
A Button control is placed in the next open cell of the TableLayoutPanel control.
In the Toolbox, double-click the Button icon four more times to populate the second row of the TableLayoutPanel control.
Select all five Button controls by clicking them while holding down the SHIFT key. Press CTRL+C to copy the Button controls to the clipboard.
Press CTRL+V three times to paste copies of the Button controls into the remaining rows of the TableLayoutPanel control.
Select all 20 Button controls by clicking them while holding down the SHIFT key.
In the Properties window, change the value of the Dock property to Fill.
All the Button controls dock to fill their containing cells.
In the Properties window, expand the Margin property node. Set the value of All to 5.
All the Button controls are sized smaller to create a larger margin between them.
Select button10 and button20, and then press DELETE to remove them from the layout.
Select button5 and button15, and then change the value of their RowSpan property to 2. These will be the Clear and = buttons for the
DemoCalculator
control.
Navigating the Control Using the Document Outline Window
When your control or form is populated with several controls, you may find it easier to navigate your layout with the Document Outline window.
To navigate using the Document Outline window
On the View menu, point to Other Windows, and then click Document Outline.
The Document Outline window shows a tree view of the
DemoCalculator
control and its constituent controls. Container controls like the SplitContainer show their child controls as subnodes in the tree. You can also rename controls in place using the Document Outline window.In the Document Outline window, right-click button1, and then click Rename. Change its name to
sevenButton
.Using the Document Outline window, rename the Button controls from the designer-generated name to the production name according to the following list:
button1
tosevenButton
button2
toeightButton
button3
tonineButton
button4
todivisionButton
button5
toclearButton
button6
tofourButton
button7
tofiveButton
button8
tosixButton
button9
tomultiplicationButton
button11
tooneButton
button12
totwoButton
button13
tothreeButton
button14
tosubtractionButton
button15
toequalsButton
button16
tozeroButton
button17
tochangeSignButton
button18
todecimalButton
button19
toadditionButton
Using the Document Outline and Properties windows, change the Text property value for each Button control name according to the following list:
Change the
sevenButton
control text property to 7Change the
eightButton
control text property to 8Change the
nineButton
control text property to 9Change the
divisionButton
control text property to /Change the
clearButton
control text property to ClearChange the
fourButton
control text property to 4Change the
fiveButton
control text property to 5Change the
sixButton
control text property to 6Change the
multiplicationButton
control text property to *Change the
oneButton
control text property to 1Change the
twoButton
control text property to 2Change the
threeButton
control text property to 3Change the
subtractionButton
control text property to -Change the
equalsButton
control text property to =Change the
zeroButton
control text property to 0Change the
changeSignButton
control text property to +/-Change the
decimalButton
control text property to .Change the
additionButton
control text property to +
On the designer surface, select all the Button controls by clicking them while holding down the SHIFT key.
In the Properties window, expand the Font property node. Set Size to 14, and set Bold to true for all the Button controls.
This completes the design of the
DemoCalculator
control. All that remains is to provide the calculator logic.
Implementing Event Handlers
The buttons on the DemoCalculator
control have event handlers that can be used to implement much of the calculator logic. The Windows Forms Designer enables you to implement the stubs of all the event handlers for all the buttons with one double-click.
To implement event handlers
On the designer surface, select all the Button controls by clicking them while holding down the SHIFT key.
Double-click one of the Button controls.
The Code Editor opens to the event handlers generated by the designer.
Testing the Control
Because the DemoCalculator
control inherits from the UserControl class, you can test its behavior with the UserControl Test Container. For more information, see How to: Test the Run-Time Behavior of a UserControl.
To test the control
Press F5 to build and run the
DemoCalculator
control in the UserControl Test Container.Click the border between the SplitContainer panels and drag it left and right. The TableLayoutPanel and all its child controls resize themselves to fit in the available space.
Using the Control on a Form
The DemoCalculator
control can be used in other composite controls or on a form. The following procedure describes how to use it.
Creating the Project
The first step is to create the application project. You will use this project to build the application that shows your custom control.
To create the project
On the File menu, point to Add, and then click New Project to open the New Project dialog box.
Select the Windows Application project template from the list of Visual C# projects, type DemoCalculatorTest in the Name box, and then click OK.
In Solution Explorer, right-click the References folder, and then click Add Reference to open the Add Reference dialog box.
Click the Project Name tab, and then double-click your
DemoCalculatorLib
project to add the reference to the test project.In Solution Explorer, right-click DemoCalculatorTest, and then click Set as StartUp Project.
In the Windows Forms Designer, increase the size of the form to about 700 x 500.
Using Your Control in the Form's Layout
To use the DemoCalculator
control in an application, you need to place it on a form.
To use your control in the form's layout
In the Toolbox, expand the DemoCalculatorLib Components node.
Drag the DemoCalculator control from the Toolbox onto your form. While holding down the mouse button, allow the Toolbox to close, and then move the glyph that represents your control to the upper-left corner of the form. When the glyph is close to the form's borders, you will see snaplines appear. These indicate the distance of the form's Padding property and the control's Margin property. Drop the control at the location indicated by the snaplines.
For more information, see Walkthrough: Arranging Controls on Windows Forms Using Snaplines.
Drag a Button control from the Toolbox and drop it onto the form.
Move the Button control around the
DemoCalculator
control and observe where the snaplines appear. You can align your controls precisely and easily using this feature. Delete the Button control when you are finished.Right-click the
DemoCalculator
control, and then click Properties.Change the value of the Dock property to Fill.
Select the form, and then expand the Padding property node. Change the value of All to 20.
The size of the
DemoCalculator
control is reduced to accommodate the new Padding value of the form.Resize the form by dragging the various sizing handles to different positions. Observe how the
DemoCalculator
control is resized to fit.
Next Steps
This walkthrough has shown how to construct the user interface for a simple calculator. You may want to extend its functionality in the following ways:
Implement the calculator logic. This may seem straightforward, but there are interesting complexities associated with the calculator's state transitions.
Package the calculator application for deployment. For more information, see How to: Publish a ClickOnce Application.
See Also
Tasks
Walkthrough: Arranging Controls on Windows Forms Using a TableLayoutPanel
Walkthrough: Adding Smart Tags to a Windows Forms Component
Walkthrough: Arranging Controls on Windows Forms Using Snaplines
Walkthrough: Automatically Populating the Toolbox with Custom Components
How to: Test the Run-Time Behavior of a UserControl
Troubleshooting Control and Component Authoring
Troubleshooting Design-Time Development