Click to See Complete Forum and Search --> : Assigning Text and Vairable to Text Field


RJK
May 14th, 2010, 04:35 PM
Hey i need help, im still learning WinAPI, but i was wondering how do you set text and a variable to a textfield, so far ive got:

char cHours[5], cPay[5], cTotal[5];
float Hours, Pay, Total;

GetWindowText(GetDlgItem(hwnd, IDC_HOURS), cHours, MAX_PATH);
GetWindowText(GetDlgItem(hwnd, IDC_PAY), cPay, MAX_PATH);

Hours = atof(cHours);
Pay = atof(cPay);

Total = Hours * Pay;

sprintf(cTotal, "%.2f", Total);

SetWindowText(GetDlgItem(hwnd, IDC_TOTAL), cTotal);

It works, but i was wondering how do you set text, like i want it to say (when the user clickes the button) "Your Wage is:"

I was playing arounda little bit, trying of every solution i could think off, but to give you lot a better idea here is something (i know it doesnt work, kinda like pseudo code):

SetWindowText(GetDlgItem(hwnd, IDC_TOTAL), "You Wage is" + cTotal);

S_M_A
May 15th, 2010, 05:28 AM
Maybe I don't understand your question but why not usesprintf(cTotal, "Your Wage is %.2f", Total);
SetWindowText(GetDlgItem(hwnd, IDC_TOTAL), cTotal);
Since this requires that you're responsible for making sure that cTotal can hold enough characters you might prefer switching to use STL instead.#include <sstream>
stringstream str;
str << "Your Wage is " << Total;
SetWindowText(GetDlgItem(hwnd, IDC_TOTAL), str.str().c_str());

RJK
May 15th, 2010, 05:49 AM
Perfect Answer, thank you.