Refer to parent object

If you are using wxDev-C++ for your wxWidgets design, please ask your questions here instead of in IDE Related.
Post Reply
Tobi
Earned a small fee
Earned a small fee
Posts: 10
Joined: Mon May 15, 2006 6:08 am

Refer to parent object

Post by Tobi » Fri May 19, 2006 6:20 am

This might be easy, but is a big problem for me, anyhow:

I have a dialog form, from which I call a second one.

Code: Select all

   Dlg2* NewDlg = new Dlg2(this);
    if (NewDlg->ShowModal() == wxID_OK)
      {
      }
   NewDlg->Destroy();
This works fine, but when I am now in the second dialog, how can I refer to values from objects in the first dialog (or even set them)?

I tried something like:

Code: Select all

parent->wxStaticText1->GetLabel();
But this does not work ("parent undeclared"). Any suggestions?

Peterj
Knows some wx things
Knows some wx things
Posts: 38
Joined: Mon Nov 14, 2005 6:48 pm
Location: Australia

Post by Peterj » Fri May 19, 2006 6:33 am

Tobi,
You can set the values in the child dialog with something like the following:

Code: Select all

Dlg2* NewDlg = new Dlg2(this);

    NewDlg->WxEdit1->SetValue(_T("sometext"));

    if (NewDlg->ShowModal() == wxID_OK)
      {
      }
   NewDlg->Destroy();
Using Win XP, Dev C++ 4.9.9.2 wx-beta 6.9

HeReSY
Earned some good credits
Earned some good credits
Posts: 120
Joined: Fri Sep 17, 2004 8:58 pm
Location: Germany

Post by HeReSY » Fri May 19, 2006 7:18 am

A cast could solve your problem.

Code: Select all

(Dlg1*)parent->wxStaticText1->GetLabel();
HeReSY

Tobi
Earned a small fee
Earned a small fee
Posts: 10
Joined: Mon May 15, 2006 6:08 am

Post by Tobi » Fri May 19, 2006 8:23 am

Thanks for your answers. unfortunately the cast has not solved the problem. parent is still unknown. And do not want to set the values from the parent to the child (this is no problem), but FROM within the child I want to change a value of the parent dialog. This must be possible, right?

buildere
Super wx Problem Solver
Super wx Problem Solver
Posts: 358
Joined: Thu Oct 28, 2004 3:45 pm
Location: Costa Rica

Post by buildere » Fri May 19, 2006 2:43 pm

Do you have the "Dlg1" header file listed on the NewDlg .cpp file?

PhoenixPl
I live to help wx-kind
I live to help wx-kind
Posts: 179
Joined: Wed Feb 15, 2006 9:26 am

Post by PhoenixPl » Fri May 19, 2006 2:53 pm

Try to add to your second dialog

Code: Select all

#include "dlg1.h"
Then in the code just:

Code: Select all

Dlg1 *parent = GetParent();
parent->wxStaticText1->GetLabel();

Tobi
Earned a small fee
Earned a small fee
Posts: 10
Joined: Mon May 15, 2006 6:08 am

Post by Tobi » Tue May 23, 2006 12:26 am

Yes yes, the dlg1.h header file is included, but it still not works.

The idea looks great and simple, but unfortunately the GetParent() method returns a wxWindow* and this is not able to convert into a Dlg1* (compiler message).
So how do I get the wxStaticText out of the wxWindow ??? (I think, this is even the wxWindow* of my Dlg2 Box and not of the Dlg1)

Is it really so hard, has noone else tried this before???

micros
Super wx Problem Solver
Super wx Problem Solver
Posts: 317
Joined: Sat Mar 18, 2006 10:41 am
Location: Ustek, Bohemia

Post by micros » Tue May 23, 2006 4:36 am

Tobi wrote:...unfortunately the GetParent() method returns a wxWindow* and this is not able to convert into a Dlg1* (compiler message)
Noone (not even the compiler) should be able to stop you from converting a thing to anything else :P
I guess your Dlg1 is a descendant of wxDialog, isn't it? Unlike conversion to a base class, conversion to a derived class isn't implicit in C++. You'll have to force it:

Code: Select all

// just like C++ static_cast, only that in debug build it actually
// checks whether the conversion is valid and issues an error if not
Dlg1* parent = wxStaticCast(GetParent(), Dlg1);
BTW, you don't even need to know the parent dialog. The ID of the control you need to access would suffice:

Code: Select all

wxStaticText* label = wxDynamicCast(GetParent()->FindWindow(ID_STATIC1), wxStaticText);
if (label) {
  label->SetLabel(wxT("modified from the other dialog"));
}
See RTTI functions for some info on the cast functions.

PhoenixPl
I live to help wx-kind
I live to help wx-kind
Posts: 179
Joined: Wed Feb 15, 2006 9:26 am

Post by PhoenixPl » Tue May 23, 2006 8:28 am

Or you can just cast it like this:

Code: Select all

Dlg1 *parent = (Dlg1*) GetParent();

Post Reply