-
It is a little easier to display
characters in an edit box when the string to display is of type CString.
-
In the Object IDs list, click
IDC_BTN_CLERK. In the Messages list,
click BN_CLICKED.
-
Click the Edit Code button.
-
Edit the function to display as follows:
void CStringsDisplayDlg::OnClerk()
{
m_Clerk = "Josiane Blister";
UpdateData(FALSE);
}
-
To test your application, press Ctrl + F5.
-
Once the dialog application is running, click the Clerk button. After
viewing the result, to close the dialog box, click Exit.
-
To display a string in an edit box that is derived from a CEdit
class, change the next function as follows:
void CStringsDisplayDlg::OnCustomer()
{
m_Customer.SetWindowText("Timothee Sanfield");
}
-
To test your application, press Ctrl + F5.
-
Once the dialog application is running, click the Customer button.
After viewing the result, to close the dialog box, click Exit.
-
Now, we will learn how to display a float value in an edit box; we
need some gymnastics to get the right result.
-
To manipulate the value of a food
item, we need to assign a double value to each check box.
If necessary, expand the ClassView in the Workspace section.
Right-click CStringDisplayDlg and click Add Member Variable.
-
In the Variable Type edit box, type double
-
In the Variable Name edit box, type ChickenWalnut
-
In the Access section, click the Protected radio butto
-
Click OK.
-
In the same way, create a protected member variable named SeafoodNest
-
Create one more protected member variable named ShrimpCashew
protected:
double ShrimpCashew;
double SeafoodNest;
double ChikenWalnut;
- In the CStringDisplayDlg::OnInitDialog()
function, initialize these variables as follows:
// TODO: Add extra initialization here
ShrimpCashew = 0;
SeafoodNest = 0;
ChikenWalnut = 0;
return TRUE; // return TRUE unless you set the
focus to a control
-
From the WorkSpace window, expand the CStringDisplayDlg
folder and double-click the OnChickenWalnut()
function to access its implementation.
In coding our check boxes, since
each of our check boxes has a variable attached to it, we first need to transfer
the current content of that variable to the control:
UpdateData(TRUE);
Then, we need to evaluate the control's variable by cheking its
boolean state: if the box is checked, we assign it a variable, in this
case the price of the Walnut Chiken; if the box is not checked, we keep it as zero:
void CStringsDisplayDlg::OnChickenWalnut()
{
UpdateData(TRUE);
if( m_ChickenWalnut==TRUE )
ChikenWalnut = 8.95;
else
ChikenWalnut = 0;
}
- Code the other two functions
accordingly. When you finish, your functions should look like this:
void CStringsDisplayDlg::OnCSeafoodNest()
{
UpdateData(TRUE);
if( m_SeaNest == TRUE )
SeafoodNest = 14.95;
else
SeafoodNest = 0;
}
void CStringsDisplayDlg::OnCShrimpCashew()
{
UpdateData(TRUE);
if( m_ShrimpCashew == TRUE )
ShrimpCashew = 9.95;
else
ShrimpCashew = 0;
}
- We are now ready to display the
result of the user's choice(s). Based on the boxes that are checked, we
first sum up the corresponding values to the boxes that are checked or
not checked, since we assigned a double number to each. Then we will use the
CEdit::Format() function to format that number in order to display it in
an Edit box. Finally, we transfer the new result to the screen display
of IDC_BTN_ADD:
void CStringsDisplayDlg::OnAddition()
{
double Sum;
Sum = ChikenWalnut + SeafoodNest + ShrimpCashew;
m_Display.Format( "$ %.2f", Sum );
UpdateData(FALSE);
}