< C++ .NET Windows Form 3 | Main | C++ .NET Windows Form 5 >



Windows Forms 4




  1. Using Controls

  2. Controls and Events

  3. Label

  4. Button


Using Controls


Now that you've seen the basics of adding controls to forms and handling events, let's look at some of the controls that are available for you to use in Windows Forms applications. This section will look at the most fundamental controls, and Module 22 will deal with other controls in more detail. All these controls inherit from the Component and Control classes, which gives them a very large number of methods and properties in common.


Controls and Events


Although some controls such as icons and labels can be used in a purely decorative way or to present information to the user, many controls are used to enable the user to interact with the application. Controls fire events when the user interacts with them in some way, such as clicking a button, moving a scroll bar, or choosing a date from a calendar control. Exactly what and how many events can be fired varies from control to control. Buttons, for example, only tend to fire Click events when the user clicks them. A complex control such as a tree control can fire many different events because there are many ways in which a user can interact with the tree. When using a control, you need to consult the .NET Framework documentation to find out what events can be fired and what handler functions you need to provide. You can also create your own controls to use on forms, but that is outside the scope of this book.




A label is a control that is used to provide descriptive text on a form. Users don't normally interact with label controls, and these controls don't usually receive the input focus, so it's unusual for programs to handle events originating from labels. Using labels mainly consists of creating them and then setting their properties. The following table shows the most commonly used properties of the Label class.


Label Property



Determines whether the label automatically resizes itself to fit the text.

BackColor (inherited from Control)

Represents the background color of the label.


Gets or sets the style of the label border: three-dimensional, single, or none.


Determines whether the label has a flat appearance.

Font (inherited from Control)

Represents the font for the control.

ForeColor (inherited from Control)

Represents the foreground color of the label.


Gets or sets the image associated with the label.


Represents the alignment of the image on the label. The default is centered.


The index of the image to be used from the associated ImageList.


The ImageList to be used as a source of images for this label.


Gets the height necessary to display one line of text.


Gets the width necessary to display the current text.


Determines whether the container's background will be displayed as the background to the label.


Determines whether the user can tab to this control.


Represents the text of the label.


Gets or sets the text alignment. The default is left aligned.


Determines whether ampersand (&) characters in the text should be interpreted as access keys.


Table 5


There are a couple of items in the table worth explaining in more detail. Labels do participate in the tab order on forms but don't usually receive the focus. If you want to give the focus to a label for some reason, set the TabStop property to true. In addition, ampersand (&) characters normally aren't interpreted as access keys as they are in menus, where the presence of an ampersand underlines the following character rather than displays the ampersand. If you want ampersands to be interpreted as access keys, set the UseMnemonic property to true. Labels can display an image as well as (or instead of) text, and the Image property represents the image currently associated with the label. You can also use images stored in ImageList controls, via the ImageList and ImageIndex properties. The following exercise shows you how to add a label to a form and how to manipulate the label's properties.


25.     Continue using the CppForm project. Open the Toolbox, and drag a Label onto the form. You can leave the control Name as label1 (default).


Adding a label to Windows Form


26.     Set the Text of the label to Your_Name Consulting, Inc., replacing Your_Name to your nick name and set its AutoSize property to true. If you have

          carried straight on from the previous exercise, you will need to press the Properties button at the top of the Properties editor so that it displays properties instead    

          of  events.


Invoking Label control Property page






The following Table summarizes the properties and respective values.







'' My Consulting, Inc














20, 20





Table 6


27.     Make sure that there are three spaces before the word Your_name; you are going to display an image at the left of the label, and you need to leave space so that it doesn't overlap with the start of the text. Setting the AutoSize property to true means that the label will resize itself to contain whatever text is assigned to it.


28.     Now set up the font details for the label. Check that the ForeColor property is set to ControlText, which defaults to black. Then find the Font property, and click the plus sign (+) to the left to expand the Font property items. Use the drop-down list to set the Name to Verdana, set Italic to true, and set the Size to 16.


29.     Set the Location of the label1 to be 20, 20.


30.     Now add an image to the label. Find the Image property in the Properties editor, and use the Browse button to find a suitable bitmap for display. You can use the following bitmap of a floppy disk, called Floppy.bmp,floopy disk image. When you have selected the image, set the ImageAlign property to MiddleLeft, which specifies middle alignment vertically and left alignment horizontally. When you click the down-arrow button to the right of the ImageAlign value, you will be given a graphical way to choose the alignment.


Setting Label visualcplusdotnetchap21Align property


31.     Build and run the program, and you'll see a label containing text and an image being displayed at the top of the form, as shown in the following figure.


Windows Form with Label and visualcplusdotnetchap21 on it







You've already met the Button class earlier in the module, and you've seen how to add buttons to forms. A Button represents a Windows button on a form and, next to Label, it's probably the simplest of the commonly used Windows controls. The most frequently used properties of the Button class are listed in the following table.


Button Property



Represents the value that is returned to the parent form when the button is clicked.

FlatStyle (inherited from ButtonBase)

Determines whether the button is drawn with a flat style.

Image (inherited from ButtonBase)

Gets or sets the image displayed on the button.

ImageAlign (inherited from ButtonBase)

Gets or sets the image alignment. The default value is MiddleCenter.

IsDefault (inherited from ButtonBase)

Determines whether the button is the form's default button.

TextAlign (inherited from ButtonBase)

Gets or sets the alignment of the text on the button.


Table 7


A button on a form can be designated the default button, in which case it is displayed with a darker border than other buttons on the form. If the user presses Enter, it's taken to be equivalent to clicking the default button. Forms that are used as dialog boxes use buttons to close the dialog box and return a value to the caller. The DialogResult property can be used to assign a result code (such as OK or Cancel) to the form, and clicking a button that has a DialogResult set will close the parent form without you having to hook up any event handlers.


Setting the DialogResult property value



Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6



< C++ .NET Windows Form 3 | Main | C++ .NET Windows Form 5 >