dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: How to get a pointer to a List Control inside a dialog box

  1. #1
    Join Date
    Apr 2002
    Posts
    124

    How to get a pointer to a List Control inside a dialog box

    Hello,
    I found this class that supposedly can print the contents of a list control. It is CPrintListCtrl. To use it, it needs a pointer to the list control as in.

    PrintData(CListCtrl* pListCtrl, CString szPageTitle);

    My question is how do you find the pointer to the list control inside a dialog box?

    The dialog box has been created using DoModal and the ListCtrl has been populated in OnInitDialg();

    All I need is to be able to print the contents of the control.

    The dialog box is created as:

    CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CMyDlg::IDD, pParent)

    Thanks...

  2. #2
    Join Date
    Jan 2001
    Location
    Paris, France
    Posts
    20

    Re: How to get a pointer to a List Control inside a dialog box

    Code:
    pListCtrl = &CMyDlg.m_ctrlMyListCtrl;
    Or did I miss the question ?
    ------------ No brain, no fear ------------
    ------------ No risk, no fun ------------

  3. #3
    Join Date
    Apr 2002
    Posts
    124

    Re: How to get a pointer to a List Control inside a dialog box

    Thanks but I am getting a compile error:

    error C2275: 'CMyDlg' : illegal use of this type as an expression in the statement:

    pListCtrl = &CMyDlg.m_ctrlMyListCtrl;

  4. #4
    Join Date
    Jan 2001
    Location
    Paris, France
    Posts
    20

    Re: How to get a pointer to a List Control inside a dialog box

    Code:
    CMyDlg dlg;
    
    ...
    
    pListCtrl = &dlg.m_ctrlMyListCtrl;
    Andre
    ------------ No brain, no fear ------------
    ------------ No risk, no fun ------------

  5. #5
    Join Date
    Apr 2002
    Posts
    124

    Re: How to get a pointer to a List Control inside a dialog box

    Thanks Andre! That worked.

  6. #6
    Join Date
    Apr 2002
    Posts
    124

    Re: How to get a pointer to a List Control inside a dialog box

    Andre,
    The code compiles but the ListCtrl pointer returns NULL after exiting the dialog box.

    Probably has something to do with the scope of the pointer.

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,175

    Re: How to get a pointer to a List Control inside a dialog box

    It has to do with the list control getting destroyed after DoModal() returns.

    Just call the PrintData() function within the CMyDlg::OnOK() handler.

  8. #8
    Join Date
    Jan 2008
    Posts
    32

    Re: How to get a pointer to a List Control inside a dialog box

    Mainly your code should be like this..
    Code:
    class CMyDlg
    {
    public:
      CListCtrl m_ctrlMyListCtrl;
    
    ..........
    };
    void CMyDlg::OnOk()
    {
      UpdateData(true);//if you have used Data Exchange with class wizzard..
      //if you havent used Data Exchange...
      //do operations with ctrlMyListCtrl manually..  
    }
    in the other hand...
    Code:
    CMyDlg dlg;
    CListCtrl* pListCtrl ;
    
    if (dlg.DoModel()==IDOK)
    {
        pListCtrl = &dlg.m_ctrlMyListCtrl;
        PrintData(pListCtrl);
    }
    Have a nice time..

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,175

    Re: How to get a pointer to a List Control inside a dialog box

    As mentioned earlier this won't work because controls in a dialog get destroyed after the call to DoModal. Sure the control variable 'm_ctrlMyListCtrl' is still around but the listctrl it's bound to has been destroyed:

    Quote Originally Posted by Revne
    Code:
    CMyDlg dlg;
    CListCtrl* pListCtrl ;
    
    if (dlg.DoModel()==IDOK)
    {
        pListCtrl = &dlg.m_ctrlMyListCtrl;
        PrintData(pListCtrl);
    }
    To verify:

    this works...

    Code:
    void CChildDlg::OnBnClickedOk()
    {
    	ASSERT( ::IsWindow( m_ListCtrl.GetSafeHwnd( ) ) );
    	OnOK();
    }
    this does not...

    Code:
    void CParentDlg::OnBnClickedOk()
    {
    	CChildDlg dlg( this );
    
    	if( IDOK == dlg.DoModal( ) )
    	{
    		ASSERT( ::IsWindow( dlg.m_ListCtrl.GetSafeHwnd( ) ) );
    	}
    
    
    	OnOK();
    }

  10. #10
    Join Date
    Apr 2002
    Posts
    124

    Re: How to get a pointer to a List Control inside a dialog box

    Thanks for the tips guys.

    I was able to solve the problem in a slightly different way by adding a "Print" button on the dialog.

    void CMydlg::OnPrint()
    {
    CListCtrl* pListCtrl = &m_ctrlMyListCtrl;
    PrintData(pListCtrl);
    }

  11. #11
    Join Date
    May 2008
    Posts
    1

    How to Solve error

    CListCtrl *pListCtrl = &m_List;
    CPrintListCtrl::PrintData(pListCtrl,"AA");

    But built error:-

    error C2352: 'CPrintListCtrl::PrintData' : illegal call of non-static member function

    Pls help to solve if anyone know.. Thanks.

  12. #12
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,555

    Re: How to Solve error

    Quote Originally Posted by gullit
    CListCtrl *pListCtrl = &m_List;
    CPrintListCtrl::PrintData(pListCtrl,"AA");

    But built error:-

    error C2352: 'CPrintListCtrl::PrintData' : illegal call of non-static member function

    Pls help to solve if anyone know.. Thanks.
    You're calling a non-static member as if it were static. You need an instance of CPrintListCtrl and call the function from it.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)