-
April 10th, 2001, 03:25 PM
#1
Adding item to Listbox fails
I added a Button and a listbox to a view used with a splitter
class CViewSico : public CFormView
{
protected:
CViewSico(); // protected constructor used by dynamic creation
DECLARE_DYNCREATE(CViewSico)
// Form Data
public:
//{{AFX_DATA(CViewSico)
enum { IDD = IDD_FORM_SICO };
CListBox m_Contentlist;
//}}AFX_DATA
// Attributes
public:
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CViewSico)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
virtual ~CViewSico();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
// Generated message map functions
//{{AFX_MSG(CViewSico)
afx_msg void OnButton1();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
I'm adding a string to the listbox like this
void CViewSico::OnButton1()
{
m_Contentlist.AddString( "file" );
}
something is going wrong. The debugger holds becouse m_hWnd = NULL in AFXWIN2.INL
_AFXWIN_INLINE int CListBox::AddString(LPCTSTR lpszItem)
{ ASSERT(::IsWindow(m_hWnd)); return (int)::SendMessage(m_hWnd, LB_ADDSTRING, 0, (LPARAM)lpszItem); }
Did I forgot something? (I used the listbox the same way in other programs witout any problems)
Any suggestions are welcome.
thnx
[i]Wimel
Axxes
Belgium
-
April 10th, 2001, 03:44 PM
#2
Re: Adding item to Listbox fails
This is declared as protected:
afx_msg void OnButton1();
Maybe it should be public:
???
++Jyvilian
-
April 10th, 2001, 03:46 PM
#3
Re: Adding item to Listbox fails
Hi,
Did u include this into DoDataExchange?
DDX_Control(pDX, IDC_LIST1, m_Contentlist);
Regards,
soundar
-
April 10th, 2001, 03:52 PM
#4
Re: Adding item to Listbox fails
Yes, the listbox was added with the classwizard
void CViewSico:oDataExchange(CDataExchange* pDX)
{
CFormView:oDataExchange(pDX);
//{{AFX_DATA_MAP(CViewSico)
DDX_Control(pDX, IDC_LIST2, m_Contentlist);
//}}AFX_DATA_MAP
}
Any other suggestions ?
[i]Wimel
Axxes
Belgium
-
April 10th, 2001, 03:56 PM
#5
Re: Adding item to Listbox fails
This sound very stange to me
But I tried
without succes.......
[i]Wimel
Axxes
Belgium
-
April 10th, 2001, 03:57 PM
#6
Re: Adding item to Listbox fails
Are you calling OnButton1 directly from constructor or before CDialog::OnInitDialog?
I fail to see another reason unless DDX fail.
There is no such a thing like silly question, but there are many stupid answers.
I don't do it for ratings. However, rating tells me how my solution worked and that is important.
Good luck in your journey in a C++ land.
There are only 10 types of people in the world:
Those who understand binary and those who do not.
-
April 10th, 2001, 04:01 PM
#7
Re: Adding item to Listbox fails
Hi,
put this code before AddString
if ( m_Contentlist.m_hWnd == NULL )
m_Contentlist.SubclassDlgItem(IDC_LIST2, this);
m_Contentlist.AddString("kdsf");
Regards,
soundar
-
April 10th, 2001, 04:05 PM
#8
Re: Adding item to Listbox fails
I'm using a formview . The functioncall is working fine. The problem is filling the listbox.
m_Contentlist.AddString("something");
sending the message gives the problem
_AFXWIN_INLINE int CListBox::AddString(LPCTSTR lpszItem)
{ ASSERT(::IsWindow(m_hWnd)); return (int)::SendMessage(m_hWnd, LB_ADDSTRING, 0, (LPARAM)lpszItem); }
Is there something I shoud do with the splitter message handling ?
[i]Wimel
Axxes
Belgium
-
April 10th, 2001, 04:07 PM
#9
Re: Adding item to Listbox fails
This works perfect.
Thanks !
But I don't understand why the subclasing is needed....
[i]Wimel
Axxes
Belgium
-
April 10th, 2001, 04:10 PM
#10
Re: Adding item to Listbox fails
Hi,
Sometime DDX doesn't work properly. I don't know why it is?
Regards,
soundar
-
April 10th, 2001, 04:12 PM
#11
Re: Adding item to Listbox fails
I know why !
VC is from Microsoft
*LOL*
[i]Wimel
Axxes
Belgium
-
April 10th, 2001, 04:16 PM
#12
Re: Adding item to Listbox fails
yes,
If u cannot do anything , just u want to Rebuild ur project in VC++.
If u cannot do anything in Windows, just u want to Restart in Windows.
Regards,
soundar
-
April 10th, 2001, 04:26 PM
#13
Re: Adding item to Listbox fails
It is not sending message itself; it is a fact that you would not be able to send message to a non-window entity or window that does not exist.
MFC, as a m_hWnd wrapper makes provisions for it; each MFC member function call requires sending message to other window directly or indirectly (by calling API function).
That is why ::IsWindow call is made before further processing.
MFC class' window object may exist without any window attached to it. Window is attached after calling Create member, or subclassing window (replacing windows’ procedure with MFC object’s window procedure).
(Subclassing window has nothing to do with deriving class from base; it is wrongly - in my opinion - nowadays substituting this term introducing confusion.)
As for controls in a MFC dialog the same approach is used to subclass controls using DDX mechanism.
That is the only reason I’ve asked if you are calling OnButton1 directly before certain events happen. Calling it directly from constructor or before CDialog::OnInitDialog will cause this very problem.
Your case is showing me that you have instantiated control but it was not subclassed, therefore it does not have valid m_hWnd and assertion fails.
Please, let me know how far from the truth I am; this will allow me to correct my logic if is wrong.
There is no such a thing like silly question, but there are many stupid answers.
I don't do it for ratings. However, rating tells me how my solution worked and that is important.
Good luck in your journey in a C++ land.
There are only 10 types of people in the world:
Those who understand binary and those who do not.
-
April 10th, 2001, 04:48 PM
#14
Re: Adding item to Listbox fails
Why is this insufficiant ?
void CViewSico:oDataExchange(CDataExchange* pDX)
{
CFormView:oDataExchange(pDX);
//{{AFX_DATA_MAP(CViewSico)
DDX_Control(pDX, IDC_LIST2, m_Contentlist);
//}}AFX_DATA_MAP
}
soundar's solution works fine but I don't understand why I need to do it.
void CViewSico::OnButton1()
{
if (!m_Contentlist.m_hWnd) m_Contentlist.SubclassDlgItem(IDC_LIST2, this);
m_Contentlist.AddString("file");
}
Can u explane why m_Contentlist.m_hWnd is NULL the first time I call the function ?
[i]Wimel
Axxes
Belgium
-
April 11th, 2001, 04:27 AM
#15
Re: Adding item to Listbox fails
What he did is the same thing that is done by DDX. You should not have to do it if all were OK.
I am trying to find out why, but you did not answer my question…
Did you call OnButton1 directly and when?
I am going to be in Atlanta today, therefore I will be able to answer you late in the evening if you post answer or send me sample of your code.
There is no such a thing like silly question, but there are many stupid answers.
I don't do it for ratings. However, rating tells me how my solution worked and that is important.
Good luck in your journey in a C++ land.
There are only 10 types of people in the world:
Those who understand binary and those who do not.
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
|