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

Hybrid View

  1. #1
    Join Date
    Mar 2005
    Posts
    172

    Get a pointer to other dialog window

    Hello,
    I have dialog based app. main window opens another 2 dialogs (A and B), i am looking for a way passing data and updating it from event on dialog A to B.


    //pointer to B dialog
    InfoDlg *IWnd = (InfoDlg*)GetParent(); //<-- i dont remember how to set pointer to other window not to parent


    // send data to B
    IWnd->m_number = 1234;
    // update data in B
    IWnd->UpdateData(FALSE);

    Thanks for help...

  2. #2
    Join Date
    Apr 2005
    Posts
    3

    Re: Get a pointer to other dialog window

    Quote Originally Posted by portnov
    //pointer to B dialog
    InfoDlg *IWnd = (InfoDlg*)GetParent(); //<-- i dont remember how to set pointer to other window not to parent
    ...
    //Maybe like this
    InfoDlg *IWnd = new COtherDlg;

  3. #3
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863

    Re: Get a pointer to other dialog window

    Have the parent window handle access to the dialogs,
    I am not sure how you are creating the dialogs in the parent so there are several possibilties.

    so lets say the dialogs class are normal members of your main dialog class. then provide a public accessor

    Code:
    class MyDialog : public CDialog
    {
    
    public:
    DialogB& GET_B() {return b;}
    ...
    ...
    private:
    DialogA a;
    DialogB b;
    
    }
    
    //to make life simpler
    
    __inline DialogB&  GET_DIALOG_B()
    { return static_cast<MyDialog*>(AfxGetMainWnd())->GET_B();}
    then to access b from a, just use

    Code:
    GET_DIALOG_B().KnockYourselfOut();

    if you are creating the dialogs on the heap, then have GET_B() return a pointer instead
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  4. #4
    Join Date
    Mar 2005
    Posts
    172

    Re: Get a pointer to other dialog window

    Quote Originally Posted by souldog
    Have the parent window handle access to the dialogs,
    I am not sure how you are creating the dialogs in the parent so there are several possibilties.
    Well, im my main dialog i have:

    ADlg m_a;
    m_a.Create(IDD_A_DIALOG, this);
    m_a.ShowWindow(SW_SHOW);

    BDlg m_b;
    m_b.Create(IDD_B_DIALOG, this);
    m_b.ShowWindow(SW_SHOW);


    to acsess/change data in main from A or B i can:

    MainDlg *Wnd = (MainDlg*)GetParent();
    ---
    Wnd->whatever();


    However i still have problem creating simple pointer from dialog A to Data in B dialog.

    Thanks,

  5. #5
    Join Date
    Mar 2005
    Posts
    172

    Re: Get a pointer to other dialog window

    Quote Originally Posted by Shadowflip
    //Maybe like this
    InfoDlg *IWnd = new COtherDlg;

    Thnaks for idea but it isnt working.. - compiles fine, but getting : "An unsupported operation was attempted"
    Last edited by portnov; April 16th, 2005 at 07:15 AM.

  6. #6
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863

    Re: Get a pointer to other dialog window

    I assume m_a and m_b are members of your main dialog class.

    Apparently you did not read my post
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  7. #7
    Join Date
    Mar 2005
    Posts
    172

    Re: Get a pointer to other dialog window

    Finally i found simple solution - the easiest way to pass data from A to B is by making A, B's parent window...


    Thanks for your help guys...

  8. #8
    Join Date
    Nov 1999
    Location
    Los Angeles, USA
    Posts
    253

    Re: Get a pointer to other dialog window

    Making ADlg the parent of BDlg might solve your problem in access by using GetParent(), but it might not be the best practice. Modeless dialogs are usually given a parent of NULL if they're to float around the screen or maybe a CView derived class if they're to be embedded within a view. This could be important for redrawing purposes.

    To pass information back and forth between them - keep pointers to those dialogs in the main dialog. Then each modeless dialog can refer to its parent, the main dialog and get reference to each other.
    Code:
    class CMainDlg : public CDialog {
    ...
    // Attributes
    protected:
       CDialog*   m_pDlgA;   // Initialized to NULL in constructor, and assigned a value when ADlg is created
       CDialog*   m_pDlgB;   // Same here
    ...
    // Operations
    public:
       CDialog*   GetDlgA() { return m_pDlgA; }
       CDialog*   GetDlgB() { return m_pDlgB; }
    ...
    };
    Jay

  9. #9
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Get a pointer to other dialog window

    The truly easiest way is to have in dialog B member of appropriate type (a pointer to A class) and set it after creation of B but before DoModal (or Create) call for it.

    Code:
    class A: public CDialog
    {...};
    
    class B: public CDialog
    {
    public:
        A* m_pA;
    ...
    };
    
    void A::OnCreateBClick()
    {
         B dlg;
         dlg.m_pA = this;
         dlg.DoModal();
    }
    Best regards,
    Igor

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