Push Buttons and Edit Boxes

Goal

Create a form view application that converts temperatures from Celsius to Fahrenheit.

The user enters a Celsius temperature in the Celsius edit box and clicks the Submit button. The application converts the temperature to Fahrenheit and displays it in the Fahrenheit edit box.

 

Part 1. Create a FormView Project

  1. Create an AppWizard project as usual. Name the project TempConverter. However, instead of deriving the form class from CView, select CFormView from which to derive the form class.

  2. A form view project contains a form on which to place controls. To see it, look in the ResourceView under Dialogs.

 

Part 2. Add Controls to the Form

  1. Make sure the Controls Toolbox is visible. If the Controls Toolbox is not visible, make it visible by selecting Tools from the main menu and Customize. Choose the Toolbars Tab and check Controls. Click on Close.

  2. Find the Edit Box control on the Controls Toolbox. (The Edit Control looks like ab|. Position the mouse cursor over the control to view the tooltip, which shows "Edit Box."

  3. Click on the Edit Box control. Then move the mouse cursor to the position on the form, press the mouse button and draw the rectangle defining the border of the control. Release the mouse button when finished.

  4. Right click on the control you just drew and select Properties. Enter the ID of the control: IDC_CELSIUS. ("IDC means ID for Control.")

  5. Add a Label control to the form. (A Label control looks like Aa.) Change its caption to "Celsius" in the Properties Window. Add these additional controls: an Edit Box control with ID IDC_FAHRENHEIT, a Label Control with caption "Fahrenheit.", and a Button control with ID IDC_SUBMIT and caption "submit."

 

Part 3. Add Event Handler for Submit Button

  1. Use the Class Wizard to add a message map in the form view class for the submit button (IDC_SUBMIT) and the message BN_CLICKED. Leave the name of the function as OnConvert.

 

Part 4. Write Code for Event Handler

There are three methods for obtaining the Celsius value from the edit box, converting it to Fahrenheit, and displaying it in the Fahrenheit edit box.

 

Method A. Use GetText and SetText Methods

Add this code to the OnConvert method in the view class:

char cel_string[10], fahr_string[10];
CWnd *c, *f;
int cel, fahr;

c = GetDlgItem(IDC_CELSIUS);
f = GetDlgItem(IDC_FAHRENHEIT);
c -> GetWindowText(cel_string, 9);
cel = atoi(cel_string);
fahr = 9 * cel / 5 + 32;
sprintf(fahr_string, "%d", fahr);
f -> SetWindowText(fahr_string);

 

Method B. Use GetDlgItemInt and SetDlgItemInt Methods

Add this code to the OnConvert method in the view class. After following the directions for each method, compile and run the application.

int cel, fahr;

cel = GetDlgItemInt(IDC_CELSIUS);
fahr = 9 * cel / 5 + 32;
SetDlgItemInt(IDC_CELSIUS, fahr);

 

Method C. Use Dynamic Data Exchange

  1. Invoke the Class Wizard. Choose the Member Variables Tab.

  2. Add the following member variables corresponding to the edit boxes in the view class:

    Control ID Type Variable Name
    IDC_CELSIUS int m_cel
    IDC_FAHRENHEIT int m_fahr

  3. Add this code to the OnConvert method in the view class:

    // Move data from controls to member variables.
    UpdateData(TRUE);

    // Perform temperature conversion.
    fahr = 9 * cel / 5 + 32;

    // Move data from member variables to controls.
    UpdateData(FALSE);