Click here to Skip to main content
15,886,017 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
int the following code:

C++
BOOL Personal::OnInitDialog()
{
	CPropertyPage::OnInitDialog();

	CComboBox *pCombo2 = (CComboBox *)GetDlgItem(IDC_COMBO4);
	CComboBox *pCombo3 = (CComboBox *)GetDlgItem(IDC_COMBO5);

	int i = 0;
	while(*szSex[i])
	{
		int iPosition = pCombo2->AddString(szSex[i]);//throws an exception at this point.
		pCombo2->SetItemData(iPosition,i);
		i++;
	}
	pCombo2->SetCurSel(0);

	i = 0;
	while(*szStudentKind[i])
	{
		int iPosition = pCombo3->AddString(szStudentKind[i]);
		pCombo2->SetItemData(iPosition,i);
		i++;
	}
	pCombo3->SetCurSel(0);
	
	if(!LoadClassStudents(pStudEdit->m_iClassID))
	{
		return 0;
	}

	INT_PTR iStudentID = pStudEdit->m_Personal.GetStudentID();
	if(!Load(iStudentID))
	{
		return 0;
	}


	CString stTitle;
	stTitle.LoadString(m_hInstance,IDS_PERSONAL,(WORD)LANGUAGE);
	SetWindowText(stTitle);

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}


Each time the program is run, I get an exception that interprets to mean pCombo2 has an invalid window handle, But if I replace IDC_COMBO4 with IDC_COMBO1 the program runs as expected.

The sttrange thing is that, IDC_COMBO1, IDC_COMBO4 and IDC_COMBO5 are all comboboxes on the same template, what could be wrong?
Posted
Updated 2-Jan-15 10:35am
v2
Comments
Richard MacCutchan 2-Jan-15 16:49pm    
Stepping through this code with your debugger will soon show you which variable is not valid.
Gbenbam 2-Jan-15 20:41pm    
Yes, I have done that, the m_hWnd member variable of pCombo2 was not valid, but I see no reason why it should be so.
Richard MacCutchan 3-Jan-15 4:24am    
Then something else is wrong that you are not showing us. If the m_hWnd is invalid, then that object has not been created properly.
Gbenbam 4-Jan-15 22:16pm    
It turned out that that was the case, I use the CPropertyPage::Contruct member function on each property page before call AddPage for each property page in the propertysheet's constructor. I actually saw this approach somewhere. It turns out that using CPropertyPage::Construct efor AddPage did bot allow the ProperyPages to get properly created.

It occured to me to remove it since that is the only new thing in property sheet programming that I have include in the program that was not in the program for other property sheet programs I wrote.

On removing them ( the CONSTRUCT lines) , every thing started working fine.

Thanks a lot.

1 solution

In MFC, GetDlgItem returns a CWnd pointer.

You have bravely cast that to a CComboBox pointer.

Try this:

Add CComboBox member variables to your dialog class.

In your OnInitDialog handler, attach each CComboBox to it's corresponding HWND (use ::GetDlgitem(m_hWnd, IDC_XXXXX).

You may need to Detach each of these controls in the dialog class's OnDestroy.
 
Share this answer
 
Comments
Gbenbam 2-Jan-15 20:39pm    
This could be the way out. Can you kindly explain things a little bit more? I don't seem to picture what you mean. Can you give me code snippets? Thanks.
Gbenbam 2-Jan-15 20:44pm    
How do I detach the controls during OnDestroy? Pls help me with code snippets?
Gbenbam 2-Jan-15 20:58pm    
I guess this is what you mean.

m_Combo1.m_hWnd = ::GetDlgItem(m_hWnd,IDC_COMBO1);
m_Combo2.m_hWnd = ::GetDlgItem(m_hWnd,IDC_COMBO4);
m_Combo3.m_hWnd = ::GetDlgItem(m_hWnd,IDC_COMBO5);

If that's it. I did exactly that, but got the same exception.
Gbenbam 2-Jan-15 21:06pm    
Each time the exception is thrown, visual studio brings out a page of Afxwin2.inl and pints ay this line of code:

AFXWIN_INLINE int CComboBox::AddString(LPCTSTR lpszString)
{ ASSERT(::IsWindow(m_hWnd)); return (int)::SendMessage(m_hWnd, CB_ADDSTRING, 0, (LPARAM)lpszString); }
[no name] 6-Jan-15 14:35pm    
See CWnd::Attach and CWnd::Detach.

http://msdn.microsoft.com/en-us/library/22x4kb1k.aspx

http://msdn.microsoft.com/en-us/library/7zhdchw8.aspx

Also, check your resource IDs. Make sure each dialog resource has a unique number.

It is possible to assign two controls to the same ID. GetDlgItem will only retrieve one of them.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900