Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
(ebook) Visual Studio .NET Mastering Visual Basic.pdf
Скачиваний:
136
Добавлен:
17.08.2013
Размер:
15.38 Mб
Скачать

THE SCROLLBAR AND TRACKBAR CONTROLS 283

Listing 6.17: Programming the ScrollBar Control’s Scroll Event

Private Sub redBar_Scroll(ByVal sender As System.Object, _

ByVal e As System.Windows.Forms.ScrollEventArgs) Handles redBar.Scroll If e.Type = ScrollEventType.EndScroll Then ColorBox1()

End Sub

Private Sub redBar_ValueChanged(ByVal sender As Object, _

ByVal e As System.EventArgs) Handles redBar.ValueChanged ColorBox2()

End Sub

The ColorBox1() and ColorBox2() subroutines update the color of the two PictureBox controls and their listings are shown in Listing 6.18. The code creates a new color value based on the values of the three scroll bars and uses it to set the BackColor property of the appropriate control.

Listing 6.18: Updating the Color of the Two TextBox Controls

Sub ColorBox1() Dim clr As Color

clr = Color.FromARGB(redBar.Value, greenBar.Value, blueBar.Value) PictureBox1.BackColor = clr

End Sub

Sub ColorBox2() Dim clr As Color

clr = Color.FromARGB(redBar.Value, greenBar.Value, blueBar.Value) PictureBox2.BackColor = clr

End Sub

The TrackBar Control

The TrackBar control is similar to the ScrollBar control, but it lacks the granularity of ScrollBar. Suppose you want the user of an application to supply a value in a specific range, such as the speed of a moving object. Moreover, you don’t want to allow extreme precision; you need only a few settings, such as slow, fast, and very fast. A TrackBar control with just a few stops, such as the one shown in Figure 6.12, will suffice. The user can set the control’s value by sliding the indicator or by clicking on either side of the indicator.

Note Granularity is how specific you want to be in measuring. In measuring distances between towns, a granularity of a mile is often adequate. In measuring (or specifying) the dimensions of a building, the granularity could be on the order of a foot or an inch. The TrackBar control lets you set the type of granularity that’s necessary for your application.

Similar to the ScrollBar control, SmallChange and LargeChange properties are available. SmallChange is the smallest increment by which the Slider value can change. The user can only change the slider by the SmallChange value by sliding the indicator (unlike the ScrollBar control, there are no arrows at the two ends of the Slider control). To change the Slider’s value by LargeChange, the user can click on either side of the indicator.

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com

284 Chapter 6 BASIC WINDOWS CONTROLS

VB.NET at Work: The Inches Project

Figure 6.15 demonstrates a typical use of the TrackBar control. The form in the figure is an element of a program’s user interface that lets the user specify a distance between 0 and 10 inches in increments of 0.2 inches. As the user slides the indicator, the current value displays on a Label control above the TrackBar. If you open the Inches application, you’ll notice that there are more stops than there are tick marks on the control. This is made possible with the TickFrequency property, which determines the frequency of the visible tick marks.

Figure 6.15

This TrackBar control lets users specify a distance.

You may specify that the control has 50 stops (divisions) but that only 10 of them will be visible. The user can, however, position the indicator on any of the 40 invisible tick marks. You can think of the visible marks as the major tick marks and the invisible ones as the minor tick marks. If the TickFrequency property is 5, only every fifth mark will be visible. The slider’s indicator, however, will stop at all tick marks.

Tip When using the TrackBar control on your interfaces, you should set the TickFrequency property to a value that helps the user select the desired setting. Too many tick marks are confusing and difficult to read. Without tick marks, the control isn’t of much help. You might also consider placing a few labels to indicate the value of selected tick marks, as I have done in this example.

The properties of the TrackBar control in the Inches application are as follows:

Minimum = 0

Maximum = 50

SmallChange = 1

LargeChange = 5

TickFrequency = 5

The TrackBar needs to cover a range of 10 inches in increments of 0.2 inches. If you set the SmallChange property to 1, you have to set LargeChange to 10 (there’s a total of 10 intervals of 0.2 inches in 10 inches). Moreover, the TickFrequency is set to 5, so there will be a total of 10 divisions in every inch. The numbers below the tick marks were placed there with properly aligned Label controls.

The label at the bottom needs to be updated as the TrackBar’s value changes. This is signaled to the application with the Change event, which occurs every time the value of the control changes, either through scrolling or from within your code. The ValueChanged event handler of the TrackBar control is shown next:

Private Sub TrackBar1_ValueChanged(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles TrackBar1.ValueChanged lblInches.Text = “Length in inches = “ & Format(TrackBar1.Value / 5, “#.00”)

End Sub

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com

THE SCROLLBAR AND TRACKBAR CONTROLS 285

The Label controls below the tick marks can also be used to set the value of the control. Every time you click one of the labels, the following statement sets the TrackBar control’s value. Notice that all the Label controls’ Click events are handled by a common handler:

Private Sub Label_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles Label1.Click, Label2.Click, _ Label3.Click, Label4.Click, Label5.Click, Label6.Click, _ Label7.Click, Label8.Click, Label9.Click

TrackBar1.Value = sender.text * 5 End Sub

VB.NET at Work: The TextMargin Project

To see the TrackBar control in use, let’s review a segment of another application, the RTFPad application, which will be covered in Chapter 7. The Form shown in Figure 6.16 contains a RichTextBox control and two sliders. The RichTextBox control will be explained in Chapter 7. All you need to know about the control to follow the code is that the RichTextBox control is similar to a TextBox control, but provides many more editing and formatting options. Two of the control’s properties we’ll use in this example are the SelectionIndent and SelectionHangingIndent properties, and their functions are as follows:

Figure 6.16

The two TrackBar controls let the user format the paragraphs in

a RichTextBox control.

SelectionIndent Specifies the amount by which the currently selected paragraphs are indented from the left side of the control.

SelectionHangingIndent Specifies the amount of the hanging indentation (that is, the indentation of all paragraph lines after the first line).

The two TrackBar controls above the RichTextBox control let the user manipulate these two indentations. Because each paragraph in a RichTextBox control is a separate entity, it can be

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com

286 Chapter 6 BASIC WINDOWS CONTROLS

formatted differently. The upper slider controls the paragraph’s indentation, and the lower slider controls the paragraph’s hanging indentation.

You can open the TextMargin application in this chapter’s folder on the CD and check it out. Enter a few paragraphs of text and experiment with it to see how the sliders control the appearance of the paragraphs.

To create the form shown in Figure 6.16, the left edge of the RichTextBox control must be perfectly aligned with the TrackBar control’s indicators at their leftmost position. When both sliders are at the far left, the SelectionIndent and SelectionHangingIndent properties are zero. As the indicators are scrolled, these two properties change value, and the text is reformatted instantly. All the action takes place in the Slider controls’ Scroll event.

Listing 6.18 presents the code of the Scroll event handlers for the two TrackBars: TrackBar1, on top, determines the paragraph’s indentation, and TrackBar2, on the bottom, controls the hanging or “first-line” indentation of the current paragraph.

Listing 6.18: Scroll Event Handlers of the TrackBars

Private Sub TrackBar1_Scroll(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles TrackBar1.Scroll RichTextBox1.SelectionIndent = _

CInt(RichTextBox1.Width * (TrackBar1.Value / TrackBar1.Maximum)) TrackBar2_Scroll(sender, e)

End Sub

Private Sub TrackBar2_Scroll(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles TrackBar2.Scroll RichTextBox1.SelectionHangingIndent = CInt(RichTextBox1.Width * _

(TrackBar2.Value / TrackBar2.Maximum)) - RichTextBox1.SelectionIndent End Sub

The paragraph’s hanging indentation is not the distance of the text from the left edge of the control, but the distance from the leftmost character of the first line. That’s why every time the paragraph’s indentation changes, the program calls the Scroll event of the second TrackBar control to adjust the hanging indentation, even though the second control hasn’t been moved. The hanging indentation is expressed as a percentage, and we get the ratio of the difference between the two controls and their maximum value.

Every time you move the pointer in another paragraph in the text, the two TrackBar controls must be set to reflect the margins of the current paragraph. The selection of a new paragraph is reported to the application through the SelectionChanged event of the RichTextBox control. In the event handler shown in Listing 6.19 are two lines that set the Value property of the two TrackBar controls to the appropriate values.

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com