Standard Controls


The basic unit of the user interface in WinForms is the control. Everything that interacts directly with the user in a region defined by a container is an instance of an object that derives, directly or indirectly, from the System. Windows .Forms.Control class.

Non-Container Controls

Non-container controls are those that don't contain other controls.

Label

The Label control, shown in Figure D.13, holds literal text that is meant to be informative to the user. For example, in a typical application, labels are displayed near text boxes to inform the user what the text box contains. Text inside a label wraps to the width of the label. The label text can be aligned to any side or corner of the control.

 
 ' Label label1.Text = "This is some test text..." lable1.TextAlign = ContentAlignment.TopCenter 
Figure D.13. A Label Control in Action

LinkLabel

LinkLabel objects, shown in Figure D.14, are just like labels but allow for one or more links to be embedded into the label. These links are clickable elements that trigger events.

Figure D.14. A LinkLabel Control in Action

This control is commonly used to allow users to click on links to Web sites from Windows Forms applications. You can add text to the link label in the same way as any other label:

 
 ' Will automatically parse common URLs linkLabel1.Text = "http://sellsbrothers.com" 

To make the link work, you must handle the LinkClicked event:

 
 Sub linkLabel1_LinkClicked(sender As Object, _   e As LinkLabelLinkClickedEventArgs)   ' Start IE with the URL   System.Diagnostics.Process.Start(CStr(e.Link.LinkData)) End Sub 
TextBox

TextBox objects, shown in Figure D.15, are used to display user-editable text. The text box allows for either single or multiline display and editing of text. The most common thing you'll do with a text box is retrieve the text within it:

 
 MessageBox.Show(textBox1.Text) 
Figure D.15. A TextBox Control in Action

Button

Button objects, shown in Figure D.16, are used to trigger actions on forms. When a button is pressed, the Click event is triggered:

 
 Sub button1_Click(sender As Object, e As EventArgs)   MessageBox.Show("Ouch!") End Sub 
Figure D.16. A Button Control in Action

In addition, buttons can be designated as a form's AcceptButton or CancelButton. These designations specify that the button is automatically clicked when the user presses the Enter key (AcceptButton) or the ESC key (CancelButton).

CheckBox

CheckBox objects, shown in Figure D.17, are most often used to indicate the answer to a yes/no question. Check boxes normally have two states: checked or unchecked. Testing the state of the check box is as simple as retrieving the value of the Checked property:

 
 If checkBox1.Checked Then MessageBox.Show("Check box checked!") 
Figure D.17. A CheckBox Control in Action

Check boxes also support a mode in which they have three states: checked, unchecked, and indeterminate. When this mode is enabled, a check box starts in an indeterminate state and reacts to user input to toggle between checked and unchecked.

RadioButton

RadioButton controls, shown in Figure D.18, are similar to check boxes in that they can have a checked and an unchecked state, but RadioButton controls are normally used in a series to indicate one of a range of options. When more than one radio button is placed in a container (a form or one of the container controls listed later), the radio buttons allow only one button at a time to be selected. You can test radio buttons in the same way you check check boxes:

 
 If radioButton1.Checked Then MessageBox.Show("Radio button checked") 
Figure D.18. A RadioButton Control in Action

PictureBox

The PictureBox control's one and only function is to display images to the user, as shown in Figure D.19. The picture box supports most bitmap formats (.bmp, .jpg, .gif, and so on) and some vector formats (.emf and .wmf). Here is an example of setting an image into a PictureBox control:

 
 pictureBox1.Image = New Bitmap("c:\windows\zapotec.bmp") 
Figure D.19. A PictureBox Control in Action

ListBox

ListBox, shown in Figure D.20, holds multiple items that can be selected by a user. You manipulate items in a ListBox using the Items collection property. A list box supports selection of one or more items in the list by the traditional Ctrl-clicking of items. You can find out the selected item by using the SelectedItem property:

 
 MessageBox.Show("Selected Item is: " & listBox1.SelectedItem.ToString()) 
Figure D.20. A ListBox Control in Action

In addition, you can handle the SelectedIndexChange event whenever the selection changes:

 
 Sub listBox1_SelectedIndexChanged(sender As Object, e As EventArgs)   ' Item changed, so let the user know which one is selected   MessageBox.Show("Selected Item is: " & _ listBox1.SelectedItem.ToString()) End Sub 
CheckedListBox

A checked list box, shown in Figure D.21, is an extension of the list box that allows selection of multiple items in the list by checking boxes. In all other ways the checked list box is identical to the standard list box.

Figure D.21. A CheckedListBox Control in Action

ComboBox

The ComboBox control, shown in Figure D.22, is a hybrid of a list box and a text box. The text box part of the control allows you to enter data directly into the control. When the user clicks on the down button, a list of items is shown, and users can pick items from this list. Like a text box, a combo box can be configured to allow free-form entry of information or to allow users to select only items that are in the list of items within the control. Because the control is part text box and part list box, it's not surprising that it can do a little of both. As with the text control, the most common task is usually retrieving the text:

 
 MessageBox.Show(comboBox1.Text) 
Figure D.22. A ComboBox Control in Action

As with the list box, you can handle the event when the selected index changes:

 
 Sub comboBox1_SelectedIndexChanged(sender As Object, e As EventArgs)   ' Item changed, so let the user know which one is selected   MessageBox.Show("Selected Item is: " & _ comboBox1.SelectedItem.ToString()) End Sub 
ListView

The ListView control, shown in Figure D.23, is similar to the list box in that it shows multiple items that can be selected either individually or as multiple selections. The chief difference is that the list view supports views much like Windows Explorer's view of files. ListView supports a large icon view, a small icon view, a list view, or a details view. The details view supports more than one piece of information per item and allows you to define columns to show for the list of items. You can change the view by changing the View property:

 
 listView1.View = View.SmallIcon 
Figure D.23. A ListView Control in Action

As with the list box, you can trap the change in the selected index:

 
 Sub listView1_SelectedIndexChagned(sender As Object, e As EventArgs)   ' Show the first of the selected items   MessageBox.Show("Selected Item is: " & _       listView1.SelectedItems(0).ToString()) End Sub 
TreeView

The TreeView control, shown in Figure D.24, is used to show hierarchies. The tree is made up of nodes. Each node can contain a nested list as exposed via the Node property collection, which is what provides the hierarchy. To create nodes in the tree view, you use code such as this:

 
 ' Create Tree Items Dim topNode As TreeNode = treeView1.Nodes.Add("Top Item") ' Add child nodes in the top node topNode.Nodes.Add("Child Node") topNode.Nodes.Add("Another Child Node") 
Figure D.24. A TreeView Control in Action

In addition, the control supports events for expanding nodes, something that allows you to lazily load the tree view as the user looks down the hierarchy.

DataGrid

The primary function of the DataGrid control, shown in Figure D.25, is to allow binding to data sets and other multidimensional data sources. It allows you to store three dimensions of data. Although grids are often thought of as containing two-dimensional data (rows and columns), the data grid also allows the display of multiple tables, and that provides the third dimension. Chapter 13: Data Binding and Data Grids explains data grids and data binding in detail.

Figure D.25. A DataGrid Control in Action

MonthCalendar

The MonthCalendar control, shown in Figure D.26, is used to show or select specific dates. You can retrieve the selected date this way:

 
 ' Get all the Dates chosen ' SelectionStart is beginning Date ' SelectionEnd is last date ' SelectionRange will return all the dates MessageBox.Show(String.Format("Date(s): {0} - {1}",   monthCalendar1.SelectionStart.ToShortDateString(),   monthCalendar1.SelectionEnd.ToShortDateString())) 
Figure D.26. A MonthCalendar Control in Action

The look and feel of the calendar can be changed to blend in with your applications. In addition, you can show multiple months simultaneously by specifying the CalendarDimensions of the control. You can also add boldface to an array of specific dates or yearly dates on the calendar. This is especially useful for creating holiday and vacation calendar applications. The user can select multiple dates or a range of dates, although the maximum number of days selected is limited by the MaxSelectionCount property.

DateTimePicker

The purpose of the DateTimePicker control, shown in Figure D.27, is to display a user-editable date or time or both. To help control the dates and times that are displayed, the control allows for specifying a minimum and maximum date and time. To specify whether you want a date or a time, you choose a format for the text in the control. Short and long specify different date formats, and time specifies a time format. The drop-down arrow on the control shows a calendar control to let users pick specific dates. Usually, if you are using the control for times, you will want to enable the up and down buttons by specifying true for ShowUpDown, as shown in Figure D.28.

Figure D.27. A DateTimePicker Control in Action

Figure D.28. A DateTimePicker with ShowUpDown Enabled

To retrieve the date or time from the control, you get the Value of the control:

 
 ' Show the Date (or time) picked MessageBox.Show(dateTimePicker1.Value.ToShortDateString()) 
HScrollBar

The HScrollBar control, shown in Figure D.29, is a horizontal scrollbar. Although most controls that use a scrollbar do so automatically, you can use this control to specify a scrollbar for subtle uses such as specifying a range of large values. You can specify the minimum and maximum range using the Minimum and Maximum properties:

 
 hScrollBar1.Minimum = 0 hScrollBar1.Maximum = 10 
Figure D.29. An HScrollBar Control in Action

The ValueChanged event communicates when the value has changed, and the Value property exposes the current scroll value:

 
 Sub hScrollBar1_ValueChanged(sender As Object, e As EventArgs)   MessageBox.Show("Current scroll value: " & _       hScrollBar1.Value.ToString()) End Sub 
VScrollBar

The VScrollBar control, shown in Figure D.30, is a vertical scrollbar. It is just like the HScrollBar but is drawn vertically instead of horizontally.

Figure D.30. A VScrollBar Control in Action

DomainUpDown

The DomainUpDown control, shown in Figure D.31, allows you to specify a list of items that the arrow buttons will switch between. The functionality is much like that of the combo box, but this control does not support showing the entire list at once. This control is ultimately a text box with the up/down control added so that the user can still type any desired text. Retrieving data from the control is identical to retrieving data from a text box:

 
 MessageBox.Show(domainUpDown1.Text) 
Figure D.31. A DomainUpDown Control in Action

NumericUpDown

Functionally the NumericUpDown control is much like the DomainUpDown control, but the intention of this control is to allow the user to specify a numeric value. The control, shown in Figure D.32, supports minimum value, maximum value, and a step value to allow you to control which number can be selected. You can select the numeric value of the control using the Value property:

 
 MessageBox.Show(numericUpDown1.Value.ToString()) 
Figure D.32. A NumericUpDown Control in Action

TrackBar

The track bar, shown in Figure D.33, allows the user to specify a numeric value with a maximum and a minimum value. The control captures the arrow, Page Up, and Page Down keys to control how the values are moved on the track bar. You can specify the number of positions in the bar, the number of values between each visible tick, and the number of ticks to move on an arrow key move or on the Page Up and Page Down key moves. You can catch the changed event of the track bar this way:

 
 Sub trackBar1_ValueChanged(sender As Object, e As EventArgs)   MessageBox.Show(trackBar1.Value.ToString()) End Sub 
Figure D.33. A TrackBar Control in Action

ProgressBar

The progress bar, shown in Figure D.34, is simply a user feedback control that displays a level of completion. The control allows you to specify the minimum and maximum values, although the control continues to show the blocks shown here. You call the increment method with a number for the amount to move the progress bar. There is no decrement method, but incrementing with a negative value will cause the progress bar to back up:

 
 ' Advance the Progress bar progressBar1.Increment(1) ' Decrement the Progress bar progressBar1.Increment(-1) 
Figure D.34. A ProgressBar Control in Action

RichTextBox

The RichTextBox control, shown in Figure D.35, is used for input and display of text formatted in the rich text format. The control lets you set ranges of text with various fonts, colors, and sizes. You can save the document in the rich text edit control using the SaveFile method:

 
 ' Save the file richTextBox1.SaveFile("myfile.rtf", RichTextBoxStreamType.RichText) 
Figure D.35. A RichTextBox Control in Action

PrintPreviewControl

The PrintPreviewControl, shown in Figure D.36, is used in creating a print preview window, as discussed in Chapter 7: Printing.

Figure D.36. A PrintPreviewControl Control in Action

Splitter

The Splitter control, shown in Figure D.37, is used to allow dynamic resizing of a docked control within a form. Docking and splitting are discussed in detail in Chapter 2: Forms.

Figure D.37. A Splitter Control in Action

ToolBar

A ToolBar, shown in Figure D.38, is similar to a main menu except that toolbars usually are used to specify buttons to press for quicker access to specific functionality. The toolbar is made up of a collection of buttons exposed by the Buttons property. The supported button styles are standard, toggle, separator, and drop-down. The drop-down button allows you to specify a menu to show when the down button is pushed . You can handle toolbar button clicks by handling the ButtonClick event on the toolbar itself and checking the sender to see which button was clicked:

 
 Sub toolBar1_ButtonClick(sender As Object, _   E As ToolBarButtonClickEventArgs)   If sender Is toolBarButton1 Then       ...   ElseIf sender Is toolBarButton2 Then       ...   End If End Sub 
Figure D.38. A ToolBar Control in Action

StatusBar

The StatusBar control, shown in Figure D.39, is used to show the standard status bar on the bottom of a form. The status bar can either show a simple piece of text or show a series of panels, each of which can be either a text panel or an owner-drawn panel. You can change the text in a panel on a status bar like so:

 
 ' Set the text in one of the panels statusBar1.Panels(0).Text = "Working..." 
Figure D.39. A StatusBar Control in Action

Container Controls

Container controls are used to hold other controls. These are commonly used to break complicated forms into manageable sizes or for creating logical groups.

Panel

The Panel control, shown in Figure D.40, is a flat container for other controls to be placed within. The panel can have its frame style changed to suit the design of a particular form.

Figure D.40. A Panel Control in Action

GroupBox

A GroupBox, shown in Figure D.41, is a Panel control that has a label and a frame.

Figure D.41. A GroupBox Control in Action

TabControl

The TabControl control, shown in Figure D.42, is a hybrid of a container and a control. The tabs on the top of the control are buttons that switch between pages. Each of the pages is a separate container for controls. When using a tab control, you design each page's content by dragging and dropping controls onto the tab control's surface as you would if each tab page were a separate dialog. You can programmatically switch tabs by setting the SelectedIndex or SelectedTab property:

 
 ' Change the index to the third page (2 = 3rd page) ' Both lines do the same thing, select the page by index ' or page control name tabControl1.SelectedIndex = 2 tabControl1.SelectedTab = tabPage3 ' Name of page control 
Figure D.42. A TabControl Control in Action



Windows Forms Programming in Visual Basic .NET
Windows Forms Programming in Visual Basic .NET
ISBN: 0321125193
EAN: 2147483647
Year: 2003
Pages: 139

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net