Introduction to the Up-Down Control
|
|
|
Besides the spin control,
Microsoft Visual C++ ships with an ActiveX control that provides the same
functionality. To use this object, you can add it from the Insert ActiveX
Control dialog box and it is named Microsoft UpDown Control. If there are
two versions provided, you should select the latest, which usually has a
higher version number.
|
The Microsoft UpDown control is simply a spin control
but with true visual accessibility and rapid application development (RAD)
concept. Like the spin control, it is equipped with two buttons. Each
button has a small picture (a bitmap) that displays an arrow.
The application we are about to develop is for a CD
publishing small business. This company manufactures compact discs for
self-promoting musicians and small business that want to sell their own
CDs. When taking an order of a new CD, the company charges:
- $20/CD if the customer is ordering less than 20 units
- $15/CD if the customer is ordering up to 50 units
- $12/CD if the customer is ordering up to 100 units
- $8/CD if the customer is ordering up to 500 units
- $5/CD for any order over 500 units
Practical
Learning: Introducing Up-Down Controls
|
|
- To start a new application, on the main menu, click File -> New
Project...
- In the middle, click MFC Application and set the name to
CDDVDPublisher
- Click OK
- In the first page of the wizard, click Next
- In the second page of the wizard, click Dialog-Based
- Click Next
- In the third page of the wizard, change the title to
CD/DVD Publisher
- Click Finish
- On the dialog box, click TODO and press Delete
- Click OK and press Delete
- Click Cancel
- In the Properties window, click Caption and type Close
- Design the dialog box as follows:
|
Control |
Align Text |
Caption |
ID |
Static Text |
|
|
Number of CDs/DVDs: |
|
Edit Box |
|
Right |
|
IDC_QUANTITY |
Static Text |
|
|
Unit Price: |
|
Edit Box |
|
Right |
|
IDC_UNIT_PRICE |
Static Text |
|
|
Total Price: |
|
Edit Box |
|
Right |
|
IDC_TOTAL_PRICE |
Button |
|
|
Close |
IDCANCEL |
|
- Right-click each edit control and click Add Variable...
- Add the member variables as follows:
ID |
Category |
Type |
Name |
IDC_QUANTITY |
Value |
int |
m_Quantity |
IDC_UNIT_PRICE |
Value |
double |
m_UnitPrice |
IDC_TOTAL_PRICE |
Value |
double |
m_TotalPrice |
- In the Solution Explorer, double-click CCDDVDPublisherDlg.cpp
- In the constructor, initialize the edit boxes as follows:
CCDDVDPublisherDlg::CCDDVDPublisherDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(CCDDVDPublisherDlg::IDD, pParent),
m_Quantity(1),
m_UnitPrice(20.00),
m_TotalPrice(20.00)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
- Display the dialog box
To provide an up-down control to your application,
display the Insert ActiveX Control dialog box, select Microsoft UpDown
Control 6.0 and click OK. If you plan to refer to the control in your
code, you can add a variable for it. When you do this, Microsoft Visual
C++ would create and even implement its class, giving all of its code (its
header and source files). You can then find out that the up-down control
is based on the CUpDown class which itself is based on
CWnd, making it convenient to use CWnd
properties and member functions.
Practical
Learning: Adding an Up-Down Control
|
|
- Right-click in the middle of the dialog box and click Insert
ActiveX Control...
- In the list, click Microsoft UpDown Control 6.0 (SP4) (or another
version)
- Click OK
- Move the up-down control to the right of the Number of CDs/DVDs
edit control
- Add a picture control to the dialog box
- Complete the design of the dialog box as follows:
|
Control |
Color |
ID |
Modal Frame |
Picture Control |
|
Etched |
|
True |
UpDown |
|
|
IDC_SET_QUANTITY |
|
|
- Right-click the up-down control and click Add Variable...
- Set the Variable Name to m_SetQuantity
- Click Finish
Characteristics of the Up-Down Control
|
|
Like the spin control, the up-down control cannot
display its value to the user. If you want an accompanying control to play
that role, you should place the new up-down control next to a text-based
control such as an edit box. You have the option of positioning the
up-down control to the left or the right side of its buddy control.
The Orientation of an Up-Down Control
|
|
After placing the up-down control on the parent
window, by default, its arrow buttons point up and down. If you want the
arrows to point left and right, change the value of the Orientation
property.
The Minimum and Maximum Values
|
|
To make the up-down control easier to configure, you
can set its range value visually. The minimum value is set using the Min
edit box the maximum value is set on the Max edit box. To programmatically
change the minimum value, call the SetMin() member
function. In the same way, the maximum value can be changed by calling the
SetMax() member function. Here is an example:
void CDlgSpin::OnConfigureUpDown()
{
// TODO: Add your control notification handler code here
m_UpDown.SetMin(12);
m_UpDown.SetMax(250);
}
To get the minimum value of an up-down control, call
the GetMin() member function. To get the maximum value of
an up-down control, call the GetMax() member function.
After setting the minimum and the maximum values, you
can specify the initial value the up-down control would hold when the
application comes up. This value is set using the Value edit box and it
must be in the range (Min, Max). To change the value with code, call the
SetValue() member function and pass it the desired but
valid value. Here is an example:
void CDlgSpin:: OnConfigureUpDown()
{
// TODO: Add your control notification handler code here
m_UpDown.SetMin(12);
m_UpDown.SetMax(250);
m_UpDown.SetValue(88);
}
To get the value of an up-down control, call its
GetValue() member function.