'C++ Win32 Getting the integer value from a text field
I've been writing a simple Stock Market program to help me learn Win32 programming, but I am struggling to learn how to retrieve integers from an edit control. I've learned how to retrieve strings from an edit control using GetWindowText. According to the MSDN documentation I need to use the GetDlgItemInt function.
The code below I have the user select a company(which I got working for strings) to show company stock information. I have another edit control to have the user type in the amount of shares they'd want to purchase. I want to take this value to calculate the cost of shares and display the result to the user.
HWND hEditSearchCompanies;
HWND mainWindow; //Parent Window
RECT rect;
BOOL fRelative;
BOOL fError;
rect.left = 200;
rect.right = 600;
rect.top = 50;
rect.bottom = 400;
case WM_COMMAND:
{
case IDC_CALCULATETYPESHARES:
{
int sharesInputed = GetDlgItemInt(hEditSearchCompanies, IDC_EDITTYPESHARES, &fError, fRelative);
swprintf_s(SharesInputedBuffer, 20, L"%d", sharesInputed);
InvalidateRect(mainWindow, &rect, FALSE);
}
break;
case IDC_COMPANYINFOBUTTON:
{
int len = GetWindowTextLength(GetDlgItem(hwnd, IDC_COMPANYCODEDIT));
if (len > 0)
{
wchar_t wcEditBuffer[256];
GetWindowTextW(hEditSearchCompanies, wcEditBuffer, len + 1);
Company SearchForCompany = SearchStrings(hwnd, wcEditBuffer);
PrintCompanySearchResults(ChildHDC, SearchForCompany, inputRect);
}
}
}
break;
case WM_PAINT:
{
TextOutW(ChildHDC, x, y, sharesInputedBuffer, wcslen(sharesInputedBuffer));
}
I then want to use the sharesInputed value to display the numbers like so:
int purchaseCost = sharesInputed * company.PricePerShare;
...//Display result to the user
However sharesInputed is always returning zero. I'll put in, say 50 shares. If a company's price per share cost is $63.13, my program should be printing $3,156.50, but sharesInputed is always equal to zero whenever I hit my button to calculate the cost.
MSDN documentation doesn't really have any troubleshooting tips for GetDlgItemInt, and have debugged my program numerous times, but still get 0. Looked around and found a few reasons for this but none have worked for me.
I am not getting any error messages or warnings based on my sharesInputted value.
Hopefully I provided enough code. My control ids match for the windows and buttons.
Thanks!
Solution 1:[1]
Paul Sanders has correct answer. GetDlgItemInt wants the parent window. I was giving the window of my edit control instead, and now I'm getting the correct values.
Cheers!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | DaBigBoy |
