Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ MFC dialog convert GUI
Hello The Code Project Community,
 
I'm new to writing code for Visual C++ GUI's. I'm trying to make an application to convert Horsepower to Kilowatts and vise versa.
 
I am able to convert it when I instantiate horsePower into a random number like 300 (double horsePower = 300.0) but what I wanted is that when I press the button.. it would take the value in the edit control box and convert that and display it into the results box. Picture provided below.
 
I had many errors beforehand experimenting from what I searched online and it included converting double into CStrings and vice versa and LPCTSTR and whatnot.
 
Help would be appreciated.
 
void CTry_4Dlg::OnBnClickedButton1()
{
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	double horsePower = 0.0;
	CString hP;							
	horsePower = horsePower * 0.745699872;	// conversion to kilowatts
	hP.Format(_T("%f"),horsePower);
	m_inputVariable.SetWindowText(hP);
	UpdateData(false);
}
 
void CTry_4Dlg::OnBnClickedButton3()
{
    // TODO: Add your control notification handler code here
    UpdateData(true);
    double kiloWatts = 0;                  // initialize kiloWatts
    CString kW;                             
    kiloWatts = kiloWatts * 1.34102209;     // conversion to horsepower
    kW.Format(_T("%f"),kiloWatts);
    m_inputVariable.SetWindowText(kW);
    UpdateData(false);
}
 
}
 
Posted 1-Nov-10 11:25am
Edited 1-Nov-10 11:28am
v3

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You need to add member variables for the two EDIT boxes, and set them to be of type double. Now in your button's click function, call UpdateData(TRUE) and the variables will update to the correct values (and will be of type double).
 
In VS 2010, adding a member variable is as simple as right clicking on the edit control in the designer and selecting Add Variable.
  Permalink  
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 584
1 Maciej Los 275
2 DamithSL 233
3 Sergey Alexandrovich Kryukov 209
4 BillWoodruff 200
0 OriginalGriff 6,803
1 Sergey Alexandrovich Kryukov 6,377
2 DamithSL 5,421
3 Manas Bhardwaj 4,841
4 Maciej Los 4,330


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 1 Nov 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100