Communication between Main frame and New dialog

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Emerald2240
Knows some wx things
Knows some wx things
Posts: 30
Joined: Tue Dec 11, 2018 1:38 pm

Communication between Main frame and New dialog

Post by Emerald2240 »

Hi. I'm a newbie in Wxwidgets and I recently had this problem
I'm Currently on a project of creating a GPA calculator for college students and I wanted to make it possible for the user to be able to input his or her courses via a settings button I created which opens up a dialog with a framework to change the default values to whatever courses you input. I did this using statictexts and then adding the functions to change them in the new dialog I created. The problem is whenever I try to run the program I get an error saying:
'StaticText1' was not declared in this scope
So please is there a way I can make my new dialog be able to work with functions in my main frame. I use Codeblocks Wxsmith.
Thanks in Advance.
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Communication between Main frame and New dialog

Post by doublemax »

The problem is whenever I try to run the program I get an error saying:
'StaticText1' was not declared in this scope
Without context (and especially code) this doesn't help much.

I'll just guess that you were trying to access a wxStaticText that's defined in the mainframe, from inside the dialog (or vice versa). That would be a bad solution anyway.

The dialog should store all the variables that the user can change/enter.
Then you add some public methods to the dialog through which any other class, e.g. your mainframe, can access these values.

If these are very few values, you can provide a method for each one. If there are many values, this might be cumbersome, and you'd usually define another class (or just a struct) that contains these values. And then you provide only one method that returns a pointer (or reference) to this data.

Code: Select all

MySettingsDialog setting(this, ...);
if( settings.ShowModal() == wxID_OK )
{
  // custom methods that need to be added to MySettingsDialog 
  wxString course1 = settings.GetCourseOne();
  wxString course2 = settings.GetCourseTwo();
  // and so on
}
Alternative:

Code: Select all

struct CourseData {
  wxString course1;
  wxString course2;
}

MySettingsDialog setting(this, ...);
if( settings.ShowModal() == wxID_OK )
{
  // custom method that needs to be added to MySettingsDialog 
  const CourseData &course_data = settings.GetCourseData();   
  // access course_data through struct
}
Code unchecked for syntax errors, but you should get the idea.
Use the source, Luke!
Emerald2240
Knows some wx things
Knows some wx things
Posts: 30
Joined: Tue Dec 11, 2018 1:38 pm

Re: Communication between Main frame and New dialog

Post by Emerald2240 »

Thanks Very Much Sir, You've been a great help to me these past few months.
I'd be lieing if I said I understood your code straight away but I think I get your point. I'll start implementing this right now and I'll return feedback if I encounter any problems along the way.
Thanks Again Sir
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Communication between Main frame and New dialog

Post by doublemax »

Take something like wxFileDialog as an example. In the simplest case, you invoke it and get the selected filename with wxFileDialog::GetPath().

The dialog doesn't need to know anything about the rest of your code.
And your code only needs to know how to get the information from the dialog though its public methods.

Try to model your dialog after this pattern.
Use the source, Luke!
Emerald2240
Knows some wx things
Knows some wx things
Posts: 30
Joined: Tue Dec 11, 2018 1:38 pm

Re: Communication between Main frame and New dialog

Post by Emerald2240 »

Thanks Again Sir for your advice.
I've tried all i can to make it work but to no avail.
I tried creating methods one example:

Code: Select all

Settings dialog.cpp:
Wxstring carrier1;
void settinggsdialog::OnButton1Click(wxCommandEvent&  event)
{
carrier1 = TextCtrl1->GetValue();
}

wxString Getitout = carrier1;

//later I declare wxstring getitout in the .h file. BTW I use Codeblocks's Wxsmith

Mainframe.cpp
wxString carrier2; 
settingsdialog dialog(this);
If (settingsdialog(this).Show Modal() == wxID_OK)
{
wxstring Course1 = settingsdialog(this).Getitout();
carrier2 = Course1;
}
void Main_frame::OnButton2Click(wxCommandEvent& event)
{
StaticText1->SetLabel(_(carrier2));
Layout();
}
.

This program runs but whenever I click the final button it changes my statictext to nothing just " ". I know there's a lot of errors but I wanna learn so....
Thanks in advance
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Communication between Main frame and New dialog

Post by doublemax »

It should look more like this:

Code: Select all

Settings dialog.cpp:

/* not needed
Wxstring carrier1;
void settingsdialog::OnButton1Click(wxCommandEvent&  event)
{
  carrier1 = TextCtrl1->GetValue();
}
*/

wxString settingsdialog::Getitout()
{
  return TextCtrl1->GetValue();
}

//later I declare wxstring getitout in the .h file. BTW I use Codeblocks's Wxsmith

Mainframe.cpp

settingsdialog dialog(this);
If (dialog.ShowModal() == wxID_OK)
{
  StaticText1->SetLabel( dialog.Getitout() );
  Layout();
}
Use the source, Luke!
Emerald2240
Knows some wx things
Knows some wx things
Posts: 30
Joined: Tue Dec 11, 2018 1:38 pm

Re: Communication between Main frame and New dialog

Post by Emerald2240 »

Wow! it was so easy...
Thanks for the support sir.
Post Reply