2 different windows Topic is solved

If you are using wxDev-C++ for your wxWidgets design, please ask your questions here instead of in IDE Related.
Post Reply
farso92
Earned a small fee
Earned a small fee
Posts: 19
Joined: Fri Jun 06, 2014 3:54 pm

2 different windows

Post by farso92 »

hi guys, sorry for my bad english....i'd like to create a program with 2 different windows (frames)....when i push a buttom in the first window something have to change in the second so i have to work to the connection between this 2 frames....i have created everything but when i try to start, the program crash...the program is a little long so i describe it very fast.....

the first window is called mainWindow and the second secondWindow
in mainWindow there is a buttom called btm that change a wxStatictext in secondWindow called text
everything works fine but the only problem is when i try to refer the text in this way

Code: Select all

secondWindow->text->SetLabel(p);
can somebody help me???
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: 2 different windows

Post by PB »

Are both secondWindow and text valid pointers?

Such problems often arise when there is a variable with the same name declared both at a class scope and a local scope (in the constructor). That means that the class variable is overshadowed and invalid even when the control was properly created using the local variable.

In another words, something like this

Code: Select all

class MyFrame : public wxFrame
{
    wxStaticText* text;
public:
    MyFrame()
        : wxFrame(NULL, wxID_ANY, _("Test"))
    {
        wxStaticText* text; // the class member gets overshadowed here
        
        text = new wxStaticText(... // class member variable is not initialized
    }	    
};
farso92
Earned a small fee
Earned a small fee
Posts: 19
Joined: Fri Jun 06, 2014 3:54 pm

Re: 2 different windows

Post by farso92 »

nothing...i changed the name of all elements but there is crash again...but it is strange because when the program starts both windows (main and second) are created without problem...only if i push button it crash...the problem is only in the code that i wrote
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: 2 different windows

Post by PB »

farso92 wrote:only if i push button it crash...the problem is only in the code that i wrote
How did you connect the event? Crash on button click is often caused by using a wrong event sink in the Connect() call. If the event sink is wrong, the application explodes when the event handler attempts to touch any of its member variables, as described e.g. here.
jgrzybowski
Earned some good credits
Earned some good credits
Posts: 113
Joined: Sat Sep 24, 2011 9:32 pm
Location: Poland

Re: 2 different windows

Post by jgrzybowski »

If you are still looking for some solution, I can advise nonmodal wxDialog. First window is main window - wxFrame object, but second window you can use wxDialog object (wxDev->File->New wxDialog). You need to include header of second window inside of first window cpp file (TestTwoWindowsFrm.cpp):

Code: Select all

#include "SecondDlg.h"
Pointer of second window object should be defined as a global variable outside of any function (TestTwoWindowsFrm.cpp):

Code: Select all

SecondDlg *dialogSecondWindow;
Your button function should shows second dialog as a nonmodal to have two windows active and ready to use for user (TestTwoWindowsFrm.cpp):

Code: Select all

/*
 * buttonSecondWindowClick
 */
void TestTwoWindowsFrm::buttonSecondWindowClick(wxCommandEvent& event)
{
    dialogSecondWindow = new SecondDlg(NULL);
    dialogSecondWindow->Show(); //do not use ShowModal()
    return;
}
Inside of second dialog cpp file define function to change the text (SecondDlg.cpp):

Code: Select all

void SecondDlg::ChangeTextInSecondWindow(wxString NewText)
{
    statictextTextInSecondDlg->SetLabel(NewText);
    return;
}
this function above should be declear in header file as a public (SecondDlg.h):

Code: Select all

public:
        void ChangeTextInSecondWindow(wxString NewText);
And now you can define button function in first window to change text in second one (TestTwoWindowsFrm.cpp):

Code: Select all

/*
 * buttonChangeTextSecondWinClick
 */
void TestTwoWindowsFrm::buttonChangeTextSecondWinClick(wxCommandEvent& event)
{
	dialogSecondWindow->ChangeTextInSecondWindow(wxT(" New text "));
	return;
}
In my short programm it works. Small problem becomes when you close first main window with out closing second nonmodal window. That is why you should define also public close function for second one and call this function before main window has been destroyed:
(TestTwoWindowsFrm.cpp):

Code: Select all

#include "SecondDlg.h"
SecondDlg *dialogSecondWindow;
//...
void TestTwoWindowsFrm::OnClose(wxCloseEvent& event)
{
	dialogSecondWindow->CloseMe();
    Destroy();
    return;
}

/*
 * buttonCloseClick
 */
void TestTwoWindowsFrm::buttonCloseClick(wxCommandEvent& event)
{
	dialogSecondWindow->CloseMe();
    Destroy();
	return;
}

/*
 * buttonSecondWindowClick
 */
void TestTwoWindowsFrm::buttonSecondWindowClick(wxCommandEvent& event)
{
    dialogSecondWindow = new SecondDlg(NULL);
    dialogSecondWindow->Show();
    return;
}

/*
 * buttonChangeTextSecondWinClick
 */
void TestTwoWindowsFrm::buttonChangeTextSecondWinClick(wxCommandEvent& event)
{
	dialogSecondWindow->ChangeTextInSeconWindow(wxT(" New text "));
	return;
}
(SecondDlg.cpp):

Code: Select all

void SecondDlg::ChangeTextInSeconWindow(wxString NewText)
{
    statictextTextInSecondDlg->SetLabel(NewText);
    return;
}

void SecondDlg::CloseMe()
{
	Destroy();
	return;
}
farso92
Earned a small fee
Earned a small fee
Posts: 19
Joined: Fri Jun 06, 2014 3:54 pm

Re: 2 different windows

Post by farso92 »

thank you jgrzybowski for your answer...i tried as you said but nothing...i'dont understand what is the problem...it seems that when i call

void SecondDlg::ChangeTextInSecondWindow(wxString NewText) (in your example)

i can't make changes in the second window...i can't also create nothing...no panel, no string, nothing...As if i haven't permission :shock: :shock: :shock:
farso92
Earned a small fee
Earned a small fee
Posts: 19
Joined: Fri Jun 06, 2014 3:54 pm

Re: 2 different windows

Post by farso92 »

PB wrote:
farso92 wrote:only if i push button it crash...the problem is only in the code that i wrote
How did you connect the event? Crash on button click is often caused by using a wrong event sink in the Connect() call. If the event sink is wrong, the application explodes when the event handler attempts to touch any of its member variables, as described e.g. here.
i used this

Code: Select all

	Connect(wxID, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(MainWindow::buttonWindowClick));
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: 2 different windows

Post by PB »

i used this

Code: Select all

	Connect(wxID, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(MainWindow::buttonWindowClick));
[/quote]

I belive this is correct, assuming it is called from the MainWindow's method.
jgrzybowski
Earned some good credits
Earned some good credits
Posts: 113
Joined: Sat Sep 24, 2011 9:32 pm
Location: Poland

Re: 2 different windows

Post by jgrzybowski »

Before you call: void SecondDlg::ChangeTextInSecondWindow(wxString NewText)
you need to open second window (SecondDlg). You can also check if it is opened:

Code: Select all

/*
 * buttonChangeTextSecondWinClick
 */
void TestTwoWindowsFrm::buttonChangeTextSecondWinClick(wxCommandEvent& event)
{
   if(dialogSecondWindow)   // check if dialog is opened
      dialogSecondWindow->ChangeTextInSecondWindow(wxT(" New text "));
   return;
}
Have you designed some text control (statictextTextInSecondDlg) in designer tool (SecondDlg.wxform)? This control have to be created in this function:
void SecondDlg::CreateGUIControls(). Because this function is called when whole dialog is created (when you call dialogSecondWindow->Show();)
Regards, Jarek
farso92
Earned a small fee
Earned a small fee
Posts: 19
Joined: Fri Jun 06, 2014 3:54 pm

Re: 2 different windows

Post by farso92 »

ok now it works....i have no idea why... :shock: :shock: :shock:
but the important is that it works....take this opportunity to ask how i can put the second window on the top
jgrzybowski
Earned some good credits
Earned some good credits
Posts: 113
Joined: Sat Sep 24, 2011 9:32 pm
Location: Poland

Re: 2 different windows

Post by jgrzybowski »

Select designer tab of second dialog (SecondDlg.wxform). Then in properties window choose properties tab and from combo box select object of dialog: SecondDlg::wxDialog. In the table with properties of this object find “Dialog Style” or “Frame Style”, and shows all possible styles. Try to set up “STAY ON TOP” on TRUE value. Regards Jarek
Attachments
DialogStyle.png
DialogStyle.png (24.77 KiB) Viewed 5604 times
Post Reply