CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2010
    Posts
    13

    Assigning Text and Vairable to Text Field

    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);

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Assigning Text and Vairable to Text Field

    Maybe I don't understand your question but why not use
    Code:
    sprintf(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.
    Code:
    #include <sstream>
    stringstream str;
    str << "Your Wage is " << Total;
    SetWindowText(GetDlgItem(hwnd, IDC_TOTAL), str.str().c_str());
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    Join Date
    Apr 2010
    Posts
    13

    Re: Assigning Text and Vairable to Text Field

    Perfect Answer, thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured