-
Dialog Issues in VC++
I'm trying to populate a list box on the main dialog box with data selected from a different dialog box which contains radio buttons. My code compiles, but the program bombs when I run it. What am I doing wrong?
CProject1Dlg is the main dialog box.
void CProject1Dlg::OnButtonDrink()
{
DRINK dlg_Drink(this);
// Initialize dialog data
int nRetCode = dlg_Drink.DoModal();
if(nRetCode = IDOK)
{
CString strMessage, strDType, strDSize;
UpdateData();
// Determine which button is selected and confirm addition.
GetDlgItem(IDC_RADIO_LG + dlg_Drink.m_DSize) -> GetWindowText(strDSize);
GetDlgItem(IDC_RADIO_D_POP + dlg_Drink.m_DType) -> GetWindowText(strDType);
strMessage = strDSize +" "+ strDType + " is selected";
}
-
Code:
// Determine which button is selected and confirm addition.
GetDlgItem(IDC_RADIO_LG + dlg_Drink.m_DSize) -> GetWindowText(strDSize);
GetDlgItem(IDC_RADIO_D_POP + dlg_Drink.m_DType) -> GetWindowText(strDType);
The above code is very suspect. The api is as follows:
CWnd* GetDlgItem( int nID ) const;
void CWnd::GetDlgItem( int nID, HWND* phWnd ) const;
Return Value
A pointer to the given control or child window. If no control with the integer ID given by the nID parameter exists, the value is NULL.
You're adding some value to the nID... Is there a reason for that?
My guess is ther is not. But since you don't check the return but rather assume you've got a valid pointer in temporary storage and attempt to dereference it with GetWindowText... bad ju-ju