CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Join Date
    Mar 2000
    Location
    Belgium
    Posts
    118

    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

  2. #2
    Join Date
    Oct 1999
    Location
    South on the Water
    Posts
    466

    Re: Adding item to Listbox fails

    This is declared as protected:

    afx_msg void OnButton1();

    Maybe it should be public:

    ???

    ++Jyvilian

  3. #3
    Join Date
    Nov 2000
    Location
    Colorado Springs,USA
    Posts
    491

    Re: Adding item to Listbox fails

    Hi,
    Did u include this into DoDataExchange?
    DDX_Control(pDX, IDC_LIST1, m_Contentlist);

    Regards,
    soundar


  4. #4
    Join Date
    Mar 2000
    Location
    Belgium
    Posts
    118

    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

  5. #5
    Join Date
    Mar 2000
    Location
    Belgium
    Posts
    118

    Re: Adding item to Listbox fails

    This sound very stange to me
    But I tried


    without succes.......



    [i]Wimel
    Axxes
    Belgium

  6. #6
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    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.

  7. #7
    Join Date
    Nov 2000
    Location
    Colorado Springs,USA
    Posts
    491

    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


  8. #8
    Join Date
    Mar 2000
    Location
    Belgium
    Posts
    118

    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

  9. #9
    Join Date
    Mar 2000
    Location
    Belgium
    Posts
    118

    Re: Adding item to Listbox fails

    This works perfect.
    Thanks !
    But I don't understand why the subclasing is needed....

    [i]Wimel
    Axxes
    Belgium

  10. #10
    Join Date
    Nov 2000
    Location
    Colorado Springs,USA
    Posts
    491

    Re: Adding item to Listbox fails

    Hi,
    Sometime DDX doesn't work properly. I don't know why it is?

    Regards,
    soundar


  11. #11
    Join Date
    Mar 2000
    Location
    Belgium
    Posts
    118

    Re: Adding item to Listbox fails

    I know why !
    VC is from Microsoft
    *LOL*

    [i]Wimel
    Axxes
    Belgium

  12. #12
    Join Date
    Nov 2000
    Location
    Colorado Springs,USA
    Posts
    491

    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


  13. #13
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    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.

  14. #14
    Join Date
    Mar 2000
    Location
    Belgium
    Posts
    118

    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

  15. #15
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    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.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured