The Common Controls Toolbox

The Common Controls Toolbox category is essentially a "Miscellaneous" category. It contains the controls that you will typically find in every single Windows Forms application such as buttons, text boxes, check boxes, radio buttons, and so on. This section provides an overview of each of these controls and what they do, as well as how and when they should be used.

The Button Control

The button is the de facto standard for user interactivity. If a user wants to confirm changes, open a new window, start or complete a task, or virtually any other major task, that task is often represented by a Button of some kind. Buttons can be standard buttons or they can have a flat appearance or even be made up of images. Most of the work done by buttons is accomplished through the Click event.

The CheckBox Control

A check box is a graphical expression of a Boolean value. If the box is checked, the underlying value is true. If the box is not checked, the underlying value is false. You can respond to events such as when the checked status of the box changes, and so on. Check boxes can be data-bound directly to any Boolean value.

The CheckedListBox Control

The CheckedListBox control is an extremely powerful and handy tool. It displays a list of items in much the same way as a ListBox control, and when an item is selected, the CheckBox control associated with that item becomes checked. You can obtain the list of items currently selected in the control with the SelectedItems property. If you need to present the user with several checkboxes and you want those to be formatted in an organized list, this control will help.

The ComboBox Control

The ComboBox is another control that you will see in just about every Windows Forms application somewhere. Its responsibility is to present the user with a list of options and allow her to select one. You can set the ComboBox's DropDownStyle to control its behavior. The Simple mode allows the user to manually type in the text portion with the list portion of the ComboBox remaining visible. The DropDown mode is the same as Simple, except that the user must click the down-arrow to reveal the item list. Finally, the DropDownList mode is one of the most common modes and doesn't allow the user to manually type anything; she must select an item from the list. If a user needs to select a single value (or provide his own) from a list of values, the ComboBox control is the right tool for the job.

The DateTimePicker Control

The DateTimePicker is a handy control that looks similar to a ComboBox on the surface, but when you click the down arrow on this control, a calendar appears. This calendar allows you to navigate month by month or advance or reverse years. When the user clicks on a specific date, that date becomes the Value property of the control, which is of type DateTime. Today's date shows up highlighted with a box around it. The programmer can also choose the format in which the selected date appears for maximum flexibility. Figure 35.1 shows a DateTimePicker in action.

Figure 35.1. A DateTimePicker control.

The Label Control

Labels are pretty simple controls. If you want to include some text on a form that is just there for decoration and doesn't do much beyond that, the Label is the control you need. Just put it where you want, set the margins, padding, justification, foreground color, background color, and the Text property, and you're ready to go.

The LinkLabel Control

The LinkLabel control works very much like the standard Label control, except that it renders like an HTML hyperlink. The default foreground color is the same blue that most browsers use as the default hyperlink color. The LinkLabel has a LinkClicked event that you can use to respond to when a user clicks on the active text of the label. The following few lines of code are used as an event handler for a LinkLabel with the Text property of "Go to SAMS Publishing":

private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) {     Process p = new Process();     p.StartInfo =       new ProcessStartInfo("");     p.Start(); } 

In three simple lines of code, you can have a user click a link and use the current default browser to open a link to any URL. You don't have to use the LinkLabel for opening web pages, however. It is ideally suited for supplying pop-ups of help information and launching small subforms.

The ListBox Control

The ListBox is pretty self-explanatory. It is a box that holds a list of items. You can load the list of items programmatically, at design time, or through any of the data-binding features of Windows Forms. You can access the list of items through the Items property, and you can get the selected item or items with the SelectedItem and SelectedItems properties respectively. You can also obtain the numerical indices of selected items using the SelectedIndices property. You typically use this control if you need to present the user with a list of options and allow them to select one or more of those items.

The ListView Control

The ListView takes the concept of a ListBox and adds quite a bit of additional functionality. There is so much power in this control that in previous versions of Windows Forms, developers often preferred this control over the DataGrid for displaying grids of data. The ListView supports multiple display modes that the developer can switch programmatically or the user can select them (if the developer provides that ability). These modes are: LargeIcon, Details, SmallIcon, List, and Tile. The LargeIcon and SmallIcon modes are graphical display modes that display an icon as well as the text of the list item. The Details mode displays the list in a set of rows and columns in tabular fashion. The first column contains the list item's Text property, while each additional column represents one of that item's subitems. List mode just displays the list items in a simple list format with no additional information. You can specify the images used for the ListView using the SmallImageList, StateImageList, and LargeImageList properties.

One of the new features of the ListView that was added for the 2.0 version of Windows Forms is the use of groups. The ListView control will now allow you to group list items and display group headers in the list. The LargeIcon, SmallIcon, Tile, and Details modes support the display of list item groups.

The MaskedTextBox Control

The MaskedTextBox control is another new addition with the 2005 set of controls. This TextBox allows the programmer to define a mask that indicates the allowed input. When a user starts typing in a masked text box, the input will simply be rejected if it doesn't match the mask. This means that developers can use the MaskedTextBox to allow only numeric data or only phone numbers that match a specified format.

When you set the Mask property of the MaskedTextBox control, you will see the Input Mask dialog shown in Figure 35.2. This dialog lets you select from a number of useful pre-created masks (such as phone number, e-mail address, and so on), or you can pick the <Custom> mask and define your own pattern using mask rules.

Figure 35.2. The Input Mask dialog for a MaskedTextBox control.

The MonthCalendar Control

The MonthCalendar control is essentially like a DateTimePicker except that there is no text-entry portion. The calendar is always visible and users can use the controls on the calendar to navigate forward and backward in time. Today's date is highlighted just as it is with the DateTimePicker control.

The NotifyIcon Control

The NotifyIcon control is extremely powerful and has even received a bit of a boost in Windows Forms 2.0. This component, when you drag it onto your form, allows you to display an icon in the Windows system tray where you typically see icons for your virus scanner, instant message system, and any other background applications you might be running.

You can set the Icon property of the control at design time or programmatically at runtime to have the icon change depending on the state of your application. You can even create a timer to swap the icon every few milliseconds to create a blinking or flashing icon.

Something new with 2.0 is the ability to work with the Windows XP notification balloons. These balloons should be familiar to anyone who uses Windows XPWindows Update displays them when updates are ready to download. Using the ShowBalloonTip method, you can quickly create a compelling notification from the system tray like the one shown in Figure 35.3.

Figure 35.3. Using balloon tips with the NotifyIcon component.

The NumericUpDown Control

The NumericUpDown is a simple text control that allows users to enter a number or use a combination of up and down arrows to increase and decrease the number. You can set the amount that is incremented by the arrows using the Increment property and you can set the current number using the Value property. When users need to enter numeric data into a text box, this control is often more appropriate than a standard TextBox control or even a MaskedTextBox control.

The PictureBox Control

The PictureBox control is responsible for displaying an image. Wherever you place the control you can have an image displayed. This image can be set dynamically at runtime or it can be loaded from any number of file formats such as bitmap, JPG, PNG, and so on. The developer has control over the alignment, scale/stretch, position, and much more. Images can even be loaded at design time from project resources using just the designerthis task used to require additional coding in previous versions of Windows Forms.

The ProgressBar Control

The ProgressBar is a fairly simple control that graphically displays progress. After you set the Maximum, Minimum, and Value properties, the progress bar will graphically display the percentage of completion. Progress bars are used primarily to provide the user with a graphical indication that something is taking place in the background and could potentially be a long-running task. The new 2.0 ProgressBar uses the familiar Windows XP-style progress bar. As each new item appears within the bar, it animates itself into the bar by spreading out vertically. You can see this if you increase the MarqueeAnimationSpeed property and then slowly change the Value property of the ProgressBar.

The RadioButton Control

A RadioButton is a control that works in a way that is similar to a CheckBox. Radio buttons are small circles that have a dot in them if they are selected and are hollow otherwise. Radio buttons are typically used in groups to allow a user to select only one option from a list of options.

The TextBox Control

The TextBox control is a simple input control that allows users to enter free-form text in either single or multiline input modes. As with all stock .NET controls, the TextBox control supports Unicode input, so users typing in languages that have more than 255 characters, such as Chinese or Hindi, will be able to use all of the .NET controls without the developer having to do any additional work.

The RichTextBox Control

The RichTextBox control works in a way that is very similar to the TextBox control, but allows the text to have additional properties such as varying fonts, colors, sizes, and styles such as bold, italic, or underlined. The RichTextBox control is often underestimated. It is actually a small word processor bundled into a single control. In addition to standard formatting, this control also supports bulleted lists and other more advanced word processing features. You can also save and load the contents of a RichTextBox control. The only real work the developer needs to do is provide the interface that changes the properties of the selection. This is the same method Microsoft Word uses to change text attributes. The RichTextBox control has properties such as SelectionFont and SelectionColor to change the current font and color properties. When you examine the Text property of the control, you will get the raw text without the formatting.

The ToolTip Control

The ToolTip control is a powerful new addition to the set of controls available in Windows Forms. The ToolTip control is one of the "provider" types of components. When you place it on a form, you are defining a template for how tooltips should be displayed, not the tooltip itself. When the ToolTip is on the form, all other controls on that form will gain an additional property that looks like ToolTip on ToolTip1. Using the ToolTip control, you can specify the animation speed of the pop-up tip, you can specify the icon that will be displayed, and you can even customize the foreground and background colors used to display the tooltip. These tooltips are much better looking than the ones that were possible using previous versions of Windows Forms. A tooltip with an icon is displayed in Figure 35.4.

Figure 35.4. The ToolTip control in action.

You can also set tooltips programmatically using the following syntax:

toolTip1.SetToolTip(checkBox1, "This is the tool tip for check box 1"); 

The TreeView Control

The treeView control is a control that is designed to display a hierarchical list of items. There are top-level (referred to as root) nodes and each node can have its own list of child nodes, and so on. treeViews are exceptionally well suited for allowing browsing and navigating through large amounts of data if that data is hierarchical in nature. The ability to expand and collapse nodes allows the user to see only the information important to them at the time. You can bind the nodes of a treeView at runtime to a data source, you can edit the nodes at design time, and you can manually modify the nodes at runtime by accessing the treeView's Nodes property. You also have control over where lines are drawn on the tree, which icons appear next to nodes, and much more. You can even have check boxes appear next to each node, giving you a Boolean value that you can use to allow the user to select multiple nodes within the control.

The WebBrowser Control

The WebBrowser is a new control that allows your application to open local or remote web pages in a powerful and easy-to-use control. The control supports clicking hyperlinks to open other pages, and will even display Flash animations if you have the Flash player installed. All you need to do is set the Url property and the control does the rest of the work for you. It is an extremely powerful control and not only allows your application to view remote websites, but can be used to further blur the line between Windows Forms and Web Forms applications by rendering dynamically generated HTML directly within a Windows Form.

Microsoft Visual C# 2005 Unleashed
Microsoft Visual C# 2005 Unleashed
ISBN: 0672327767
EAN: 2147483647
Year: 2004
Pages: 298 © 2008-2017.
If you may any questions please contact us: