Introduction
For beginners working with VC++ for the first time, they always find a very silly example called “Hello World” when I first studied C I had it, then I found it again in C++ and now when I read VC++ also.
It’s quite confusing when you start with a very small example while you are not familiar with the surrounding environment _at least this what happened with me_ so I thought about a small example to practice some real things and it do something useful, and here when I had the idea of making the temperature conversion application.
Background
The Idea is simple, create simple dialog box with 2 buttons and 2 Edit controls. Assign one button and one edit box for “Celsius” and the other for “Fahrenheit”. All what you have to do is to write the temperature in the edit box that is assigned for it and press the button to convert and display on the other edit box the equivalent temperature.
Using the code
After you add you controls, its time to play. Make sure that the (Number) is set to true in the edit control properties since we will not deal with other kind of input but numbers.
Add member variables for the edit controls by right clicking on the edit control and select
“Add variable”. Add float member variable to both of them, I added m_nC and m_nF for “Celsius” and “Fahrenheit” respectively.
Then right click on the button of converting from “Celsius” to “Fahrenheit”, select “Add Event Handler…” make sure that in the message type you selected BN_CLICKED. This allows you to make the application react when this button will be clicked. After that click on “Add and Edit” the time for adding conversion equation had come.
Formula should be something similar like this:
Blocks of code should be set as style "Formatted" like this:
void CF2CandC2FDlg::OnBnClickedButtonC2f()
{
UpdateData(TRUE);
m_nF=(m_nC*1.8)+32;
UpdateData(FALSE);
}
void CF2CandC2FDlg::OnBnClickedButtonF2c()
{
UpdateData(TRUE);
m_nC=(m_nF-32)/1.8;
UpdateData(FALSE);
}
Note: I had used VC++8.0 that’s why there is now class wizzard
Points of Interest
I practiced the environment in a very simple application and I learned how to read and write data from and to edit controls using buttons which is very familiar in almost every application these days.
Note: I welcome all the feedbacks and don’t forget that I’m a beginner.