Range Controls

Team-Fly    

 
Application Development Using Visual Basic and .NET
By Robert J. Oberg, Peter Thorsteinson, Dana L. Wyatt
Table of Contents
Chapter 8.  Using Controls


There are several types of range controls in VB.NET. These controls allow the user to pick some value (or display some value) within a specified range. The numeric range controls include the ProgressBar , ScrollBar , TrackBar , and NumericUpDown controls. All of the numeric range controls have certain properties in common, including the following:

  • Minimum represents the minimum value in the range of numbers .

  • Maximum represents the maximum value in the range of numbers.

  • Value represents the control's current value (between minimum and maximum).

This section examines each of the range controls and the properties, methods , and events that make them unique.

ProgressBar

The ProgressBar is a control that allows you to display the progress of an operation towards completion. It is an output-only controlthat is, the user does not directly interact with it. It displays a value by filling in a specified percentage of its client area. The Step property determines the amount of increase in the progress bar's position when the PerformStep method is called.

graphics/codeexample.gif

The following program, found in SimpleProgress\Version 1 , displays a progress bar as it moves toward completion of a task. See Figure 8-13.

Figure 8-13. Using the ProgressBar control.

graphics/08fig13.jpg

In this simple example, we have a For loop that executes 32,000 times. It writes the square root of 1 to 32,000 to a file. The progress bar illustrates completion in steps of 10 percent. In other words, the progress control moves after every 3,200 iterations. Table 8-4 defines the properties of the progress control.

Table 8-4. Property Values for the ProgressBar
Property Value
Minimum
Maximum 100
Step 10
Value

This program uses file I/O so that the progress bar moves slowly enough that it can be seen moving. The code for the Click event of the Start and Reset buttons is shown below.

In the Click handler, we have used native VB file I/O instead of the .NET file I/O classes. The FreeFile function returns a file number that can be used to interact with a file. The FileOpen procedure opens a file so that we can write to it. The Print procedure writes to the file. And, finally, the FileClose procedure closes the file and releases the file number for reuse on other files.

 Private Sub btnStart_Click(ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles btnStart.Click  ' Write square roots to a file (file i/o is slow!)   Dim fileNum As Integer = FreeFile()  ' Get a file number   FileOpen(fileNum, "sqrt.txt", OpenMode.Output)   Dim s As Single   Dim outLine As String   Dim index As Integer   For index = 1 To 32000   s = Math.Sqrt(index)   outLine = index.ToString() & "   -   Sqrt = " _   & s.ToString()   Print(fileNum, outLine)  ' Write to the file   ' Update the progress bar  (% completion)   prgFor.Value = (index / 32000) * 100   Next   FileClose(fileNum)  End Sub Private Sub btnReset_Click(ByVal sender As Object, _  ByVal e As System.EventArgs) Handles btnReset.Click    prgFor.Value = 0 End Sub 

ScrollBar

The ScrollBar base class represents a scrollable control that is used to input numeric data within a range. The HScrollBar control is displayed horizontally, and the VScrollBar control is displayed vertically. Useful properties of this control include Minimum , Maximum , and Value that were previously mentioned, as well as these additional properties:

  • LargeChange is the increment or decrement to the value when the user clicks to either side of the scroll bar.

  • SmallChange is the increment or decrement to the value when the user moves the scroll box a small distance.

Events generated by the ScrollBar control include the following:

  • Scroll indicates that the scroll bar was moved via mouse or keyboard activity.

  • ValueChanged indicates whether the value was changed either through mouse or keyboard activity, or by programmatically changing the Value property.

The following program, found in SimpleProgress\Version 2 , uses a horizontal scroll bar to set the number of square root values written to a file (see Figure 8-14). The scroll bar, named hsbRange , was configured with the following properties described in Table 8-5.

Figure 8-14. Using the ScrollBar control.

graphics/08fig14.jpg

Table 8-5. Property Values for the Horizontal ScrollBar
Property Value
LargeChange 1000
Minimum 16000
Maximum 64000
SmallChange 100
Value 32000

The only change to the code for the program from Version 1 to Version 2 is to use the current value of the scroll bar as the upper limit for the loop test.

 Private Sub btnStart_Click(ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles btnStart.Click    Dim fileNum As Integer = FreeFile()    FileOpen(fileNum, "sqrt.txt", OpenMode.Output)    Dim s As Single    Dim outLine As String  Dim limits As Integer = hsbRange.Value  Dim index As Integer  For index = 1 To limits  s = Math.Sqrt(index)       outLine = index.ToString() & " - Sqrt = " _                 & s.ToString()       Print(fileNum, outLine)       prgFor.Value = (index / limits) * 100    Next    FileClose(fileNum) End Sub 

TrackBar

The TrackBar control is similar to the ScrollBar control. It is used to input numeric data within a range and may be displayed vertically or horizontally. In addition to SmallChange and LargeChange previously discussed, the TrackBar control has the following properties:

  • Orientation indicates whether the control is displayed horizontally or vertically.

  • TickFrequency indicates the frequency of ticks .

  • TickStyle indicates whether tick marks are displayed on the bottom or top of the control.

The following program, found in SimpleProgress\Version 3 , uses a TrackBar named trkRange to set the number of square root values written to a file (see Figure 8-15). Table 8-6 details its property values.

Figure 8-15. Using the TrackBar control.

graphics/08fig15.jpg

Table 8-6. Property Values for the TrackBar
Property Value
LargeChange 1000
Minimum 16000
Maximum 64000
Orientation Horizontal
SmallChange 100
TickFrequency 4000
TickStyle Bottom Right
Value 32000

Again, this version of the program uses the current value of the track bar as the upper limit for the loop test.

 Private Sub btnStart_Click(ByVal sender As System.Object, _   ByVal e As System.EventArgs) Handles btnStart.Click    Dim fileNum As Integer = FreeFile()    FileOpen(fileNum, "sqrt.txt", OpenMode.Output)    Dim s As Single    Dim outLine As String  Dim limits As Integer = trkRange.Value  Dim index As Integer  For index = 1 To limits  s = Math.Sqrt(index)       outLine = index.ToString() & " - Sqrt = " _                 & s.ToString()       Print(fileNum, outLine)       prgFor.Value = (index / limits) * 100    Next    FileClose(fileNum) End Sub 

NumericUpDown

The NumericUpDown control is a scrollable control that allows a user to select a value by clicking the up and down buttons of the control. The user can also enter text in the control, unless the ReadOnly property is set to True . Properties of this control include:

  • Increment indicates the amount by which the value is changed when scrolling through the range.

  • DecimalPlaces indicates the number of decimal points shown.

  • ThousandsSeparator indicates whether a thousands separator is used.

  • Hexadecimal indicates whether the value is shown in hexadecimal.

The following program, found in SimpleProgress\Version 4 , uses a NumericUpDown control named nudRange to set the number of square root values written to a file (see Figure 8-16). Table 8-7 details its property values.

Figure 8-16. Using the NumericUpDown control.

graphics/08fig16.jpg

Table 8-7. Property Values for the Numeric Up/Down Control in Simple Progress (Version 4)
Property Value
DecimalPlaces
Hexadecimal False
Increment 2000
Minimum 16000
Maximum 64000
ReadOnly True
ThousandsSeparator True
Value 32000

The only change to the code for the program was to use the current value of the NumericUpDown control as the upper limit for the loop test.

 Private Sub btnStart_Click(ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles btnStart.Click    Dim fileNum As Integer = FreeFile()    FileOpen(fileNum, "sqrt.txt", OpenMode.Output)    Dim s As Single    Dim outLine As String  Dim limits As Integer = nudRange.Value  Dim index As Integer  For index = 1 To limits  s = Math.Sqrt(index)       outLine = index.ToString() & " - Sqrt = " _                 & s.ToString()       Print(fileNum, outLine)       prgFor.Value = (index / limits) * 100    Next    FileClose(fileNum) End Sub 

Team-Fly    
Top
 


Application Development Using Visual BasicR and .NET
Application Development Using Visual BasicR and .NET
ISBN: N/A
EAN: N/A
Year: 2002
Pages: 190

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