-
August 15th, 2009, 05:28 AM
#1
[RESOLVED] CDialog:ListBox in Doc/View
Instantiating a dialog in a Doc function, I can update an Edit box in the dialog, but when I try to use AddString on a Listbox (using "simple text"), I get an Assertion Failure.
Using VC++6; to illustrate the problem in the simplest app, I created an SDI Doc/View app, added a Dialog class, added a new menu item to invoke the dialog, then added an Editbox (with member variable)and a CListbox (with control member variable) to the dialog. The fact that I can write to the Editbox suggests that the invocation of the Dialog is ok, but the Assertion Error for the List box is a puzzle. I can add data using AddString in an InitDialog function, but this is useless, since I want to add data to the ListBox at runtime. Must be a simple omission somewhere........
-
August 15th, 2009, 08:25 AM
#2
Re: CDialog:ListBox in Doc/View
More than likely you're calling AddString before the control has been created or after it's been destroyed. What is the ASSERT code telling you?
-
August 15th, 2009, 10:27 AM
#3
Re: CDialog:ListBox in Doc/View
Debug assertion failure message shows:
File: afxwin2.inl
Line: 669
In the .inl file at Line 668/669 is:
[668] _AFXWIN_INLINE int CListBox::AddString(LPCTSTR lpszItem)
[669] { ASSERT(::IsWindow(m_hWnd)); return (int)::SendMessage(m_hWnd, LB_ADDSTRING, 0, (LPARAM)lpszItem); }
As I wrote earlier, a statement which writes to an Edit Box on the Dialog form is successful, whereas a trying to AddString to the List Box at the same point in the function which instantiates the Dialog causes the Assertion failure. Looks to me like the CListBox::AddString command is expecting to find the Dialog in a standard place, rather than actually getting it from the instantiated Dlg. (Excuse my poor jargonese).
-
August 15th, 2009, 11:47 AM
#4
Re: CDialog:ListBox in Doc/View
May be you are missing DDX entry for the control or string. Please check if the variable name is present in your MESSAGE_MAP macro. Also, see to it that OnInitialUpdate() function calls the base class version in your view class.
Regards,
Bhushan
-
August 15th, 2009, 11:48 AM
#5
Re: CDialog:ListBox in Doc/View
That supports what I said in post #2. The Listbox window doesn't exist at the point you're trying to add strings to it.
-
August 15th, 2009, 12:44 PM
#6
Re: CDialog:ListBox in Doc/View
Thanks for reply, bhushan.
The Dialog class has a DDX entry for the ListBox (c_list):
void CDlg:oDataExchange(CDataExchange* pDX)
{
CDialog:oDataExchange(pDX);
//{{AFX_DATA_MAP(CDlg)
DDX_Control(pDX, IDC_LIST1, c_list);
DDX_Text(pDX, IDC_EDIT1, m_edit1);
//}}AFX_DATA_MAP
}
The MESSAGE_MAP for the entire Dialog class is empty. I would never guess in a million years what to put there, nor why.
I also cannot guess what to put in View::OnInitialUpdate.
This c**p drives me mad.
-
August 15th, 2009, 02:25 PM
#7
Re: CDialog:ListBox in Doc/View
I should add that I want to access the ListBox from the Doc: that's where the data is.
I'll also repeat the point: I can write to the EditBox in the dialog from a Doc function, but attempting to access the CListBox of the Dialog causes an assertion error. It is clear that the Doc class doesn't have a clue about the CListBox, but how to get round this? Or any suggestions as to how to present the user with a list of runtime-generated items, from which they can select any or all? Where in any MS info is there any reference to the need to hand crank a CListBox into life (other than when it's embedded in a CDialog-based appplication)? Maybe I need to use the dreaded GetDlgItem(..) ?
-
August 16th, 2009, 04:29 PM
#8
Re: CDialog:ListBox in Doc/View
By moving the
Code:
m_list.AddString(m_pDocument->GetEditString());
after
Code:
CDialog::OnInitDialog();
everything works. Just as GCDEF says. The list control hasn't been initialized(created) that's why you're getting ASSERT on IsWindow.
Or maybe I haven't understood where the problem stands.
Last edited by SkyNetTo; August 16th, 2009 at 04:32 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|