Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Creating An Updown Control

The testing department is on the phone again, with an issue about the Print dialog box in your program, SuperDuperTextPro. Why is there a scroll bar next to the Number Of Copies To Print box in the Print dialog box? Well, you say, that’s in case the user wants to increment or decrement the number of copies to print. There’s a better control than a scroll bar for that, they say—what about using an updown control?

What’s an updown control? It’s a control made up of two buttons next to each other, and each button holds an arrow (each pointing away from the other button). You can use an updown when values should be incremented and decremented, and you want to give the user an easy way to do that.

Adding an updown control to a program is easy; just follow these steps:

1.  Select the Project|Components menu item, and click the Controls tab in the Components box that opens.
2.  Select the Microsoft Windows Common Controls-2 item.
3.  Close the Components box by clicking on OK.
4.  The Updown tool appears in the toolbox at this point. Add an updown to your form in the usual way.
5.  Set the updown’s Orientation property as you want it: cc2OrientationVertical (the default) or cc2OrientationHorizontal.
6.  Set the updown’s Min and Max values as you want them.
7.  Add the code you want to the updown’s event you want to work with (Change, UpClick, or DownClick). For example, here we add code to report the setting of the updown control in a text box when the user changes it in the updown’s Change event:
     Private Sub UpDown1_Change()
     Text1.Text = 'New setting: " & Str(UpDown1.Value)
     End Sub

The result of this code appears in Figure 9.13.


Figure 9.13  Using an updown control.


TIP:  Updown controls can have buddy controls that are clicked when you click the updown. To make a control an updown’s “buddy,” place that control’s name in the updown’s BuddyControl property, and set the updown’s SyncBuddy property to True. This will align the updown next to the buddy property; for example, if you make an updown the buddy of a command button, that command button is clicked each time the user clicks the updown’s up/right arrow. Or, you can increment or decrement a value in a text box by making an updown the buddy of the text box, setting the updown’s SyncBuddy property to True, and setting the updown’s Min and Max properties to the minimum and maximum value you want the user to be able to increment and decrement to in the text box.

Setting An Updown Control’s Minimum And Maximum

The default maximum value for an updown control is 10, and the default minimum is 0. How can you change those?

Just set the updown’s Max and Min properties as you want them. For example, here’s how we set those properties in an updown when it loads:

Private Sub Form_Load()
    UpDown1.Min = 0
    UpDown1.Max = 100
End Sub

That’s all there is to it. To handle the updown control’s events, take a look at the next topic.

Handling Updown Events

You’ve added an updown control to your program—but how do you connect it to your code? There are three main events you can use: the Change event, the UpClick event, and the DownClick event.

The Change event occurs when the user clicks either of the two buttons in the updown. Here’s an example; we can report the new setting of an updown when the user clicks a button by catching that action in a Change event handler:

Private Sub UpDown1_Change()
...
End Sub

We can display the updown’s new value in a text box, Text1, this way, using the updown’s Value property:

Private Sub UpDown1_Change()
    Text1.Text = "New setting: " & Str(UpDown1.Value)
End Sub

Besides the Change event, you can also attach event handlers to the updown’s UpClick and DownClick events to handle Up/Right button clicks and Down/Left button clicks. Being able to work with the individual buttons this way makes the updown a more versatile control.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.