**Create a MFC application:**

-Ok, fire up Visual Studio, and go to File->New->Project

-You will be prompted by a pop up box, highlight “MFC application” and enter a name, for me I choose “Length Converter”, pick whatever makes you happy.

Press ok, another box should pop up. Hit the “Next” button and in the upper left hand corner, choose “Dialog Based” from the radio buttons as shown in the picture:

Ok now click “Finish” you have just created the MFC application.

You should now see an empty dialog box, which we will right click and delete everything off of.

(Short note: If you do not see the dialog box go to View->Resource View. This will bring up a side menu with “Length Converter”, or whatever you called it. Expand it, expand “Length Converter.rc” and finally expand “Dialog”. You should now see IDD_LENGTHCONVERTER, click on that and it will pop up).

Ok here is our empty dialog box:

On your right, you should see a side menu called “Toolbox”, if you do not go to View->Toolbox and it should pop right up. Now let’s select the “Edit Control” and click on your empty form to make a blank box. Make a couple of these like so:

Ok, now let’s put some type of a label by these boxes so we know what we are looking at. Let’s use the “Static Text” from the toolbox on the right. Click and drag them over on the empty dialog box and type in the lengths as such:

This will be a somewhat simple interface, so for the last part here let’s add two buttons on the form one will be called “Calculate”, the other “Clear”:

We have about all of the functionality on this form that we will need, so let us get down to the actual coding aspect of this form. We need to add variables for each of the length measurements, all of which we will make as doubles. So how do you create a variable? Well right click on the “Edit Control” box you created earlier next to “Inches” and select “Add Variable”.

You should get something similar to the following pop up window:

We now need to tell it what we need created, a double variable for inches. So let us do just that. Under the “Category” select “Value” and under the “Variable type select “double”, finally we need to give this variable a name; “inches” will do just fine. Make sure your window looks like mine:

Repeat this process for every edit box, making sure to establish a new and unique name for each of the variables we will create.

Once you have a variable established for each of the edit control boxes, double click on the “Clear” button, it should take you to an empty function definition, which we will fill in.

This is where we will add all of the code necessary for what the “Clear” button is intended to do, clear the edit boxes on the side. So remove the TODO comment, and fill in the function definition with the following code, which sets all of our variables to “0”.

void CLengthConverterDlg::OnBnClickedButton2() { UpdateData(true); inches = 0; centimeters = 0; feet = 0; yards = 0; meters = 0; millimeters = 0; miles = 0; UpdateData(false); }

Now, whenever we click the “Clear” button, all of the fields will be set to “0”.

Now, lets get to adding some functionality to the “Calculate” button which will do all of our dirty work for us. Likewise, double click on “Calculate” and it will bring up a unique empty function definition which we again will fill in.

Now, we need to think for a minute on how everything is going to be implemented, a user is going to enter a length in one of the fields on his right and click “Calculate” which should fill in the remaining fields with an equivalent length measurement. So, we need to make separate conditions, for example if the “inches” is not equal to 0, which means the user filled it in and hits calculate this is what we want it to do. The code would look something like this:

if ( inches != 0 && centimeters == 0 && feet == 0 && yards == 0 && meters == 0 && miles == 0 && millimeters == 0 ){ centimeters = inches * 2.54; feet = inches * 0.0833333333333333; yards = inches * 0.0277777778; meters = inches * 0.0254; miles = inches * 25.4; millimeters = inches * 1.57828283 * pow(10.0,-5.0); }//end if

Notice what this code is doing, it’s very straight forward. If inches has something within its field and somebody hits calculate the other variables are calculated as well, each being multiplied by the equivalent of what 1 inch is. Now we just need to implement this for every case, the code below does that job.

UpdateData(true); if ( inches != 0 && centimeters == 0 && feet == 0 && yards == 0 && meters == 0 && miles == 0 && millimeters == 0 ){ centimeters = inches * 2.54; feet = inches * 0.0833333333333333; yards = inches * 0.0277777778; meters = inches * 0.0254; miles = inches * 25.4; millimeters = inches * 1.57828283 * pow(10.0,-5.0); }//end if if ( centimeters != 0 && inches == 0 && feet == 0 && yards == 0 && meters == 0 && miles == 0 && millimeters == 0 ){ inches = centimeters * 0.393700787; feet = centimeters * 0.032808399; yards = centimeters * 0.010936133; meters = centimeters * 0.01; miles = centimeters * 10; millimeters = centimeters * 6.21371192 * pow(10.0,-6.0); }//end if if ( feet != 0 && inches == 0 && centimeters == 0 && yards == 0 && meters == 0 && miles == 0 && millimeters == 0 ){ inches = feet * 12; centimeters = feet * 30.48; yards = feet * 0.333333333; meters = feet * 0.3048; miles = feet * 304.8; millimeters = feet * 0.000189393939; }//end if if ( yards != 0 && inches == 0 && centimeters == 0 && feet == 0 && meters == 0 && miles == 0 && millimeters == 0 ){ inches = yards * 36; centimeters = yards * 91.44; feet = yards * 3; meters = yards * 0.9144; miles = yards * 914.4; millimeters = yards * 0.000568181818; }//end if if ( meters != 0 && inches == 0 && centimeters == 0 && feet == 0 && yards == 0 && miles == 0 && millimeters == 0 ){ inches = meters * 39.3700787; centimeters = meters * 100; feet = meters * 3.2808399; yards = meters * 1.0936133; miles = meters * 1000; millimeters = meters * 0.000621371192; }//end if if ( miles != 0 && inches == 0 && centimeters == 0 && feet == 0 && yards == 0 && meters == 0 && millimeters == 0 ){ inches = miles * 0.0393700787; centimeters = miles * 0.1; feet = miles * 0.0032808399; yards = miles * 0.0010936133; meters = miles * 0.001; millimeters = miles * 6.21371192 * pow(10.0,-7.0); }//end if if ( millimeters != 0 && inches == 0 && centimeters == 0 && feet == 0 && yards == 0 && meters == 0 && miles == 0 ){ inches = millimeters * 63360; centimeters = millimeters * 160934.4; feet = millimeters * 5280; yards = millimeters * 1760; meters = millimeters * 1609.344; miles = millimeters * 1609344; }//end if UpdateData(false);

Notice that every function begins with

UpdateData(true);and

UpdateData(false);This is an essential piece to add, if it is left out it will not compile properly. Also make a note to “#include<cmath>” for the “pow” function that is being used here if you directly copy this code. Just put it at the top of the file with the functions that you are filling in.

Ok, so now we seem to have every basic case covered. Compile, run, and give it a shot:

Well hope this has shined some light for some of you who have less experience with creating MFC applications, if you have any questions or comments please do not hesitate to contact me via <DIC>.