[wxCalendarCtrl] GetDate failed on event ... Topic is solved

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
Marco
Experienced Solver
Experienced Solver
Posts: 65
Joined: Wed Aug 17, 2005 2:25 pm
Location: Rennes, France

[wxCalendarCtrl] GetDate failed on event ...

Post by Marco »

Hi everybody,

I try to test wxCalendarCtrl with a small application but this doesn't work at all.

Code: Select all

#include "MainFrame.hpp"

#include "wx/wx.h"
#include "wx/calctrl.h"
#include "wx/datetime.h"

typedef enum wxwidgets__ids
{
	ID_PANEL,
	ID_CALCTRL
} wxwidgets__id_t;

BEGIN_EVENT_TABLE(MainFrame, wxFrame)
	EVT_CALENDAR_SEL_CHANGED(ID_CALCTRL, MainFrame::OnDateSelectedChange)

	EVT_CLOSE(MainFrame::OnClose)
END_EVENT_TABLE()

MainFrame::MainFrame()
:wxFrame (NULL, -1, "Calendar")
{
	wxPanel* p_main_panel = new wxPanel(this, ID_PANEL);

    wxBoxSizer* p_main_box_sizer = new wxBoxSizer(wxVERTICAL);
    p_main_panel->SetSizer(p_main_box_sizer);

    wxCalendarCtrl* p_calendar = new wxCalendarCtrl(p_main_panel, ID_CALCTRL, wxDateTime());
    p_main_box_sizer->Add(p_calendar, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);

    wxStaticText* p_static_text = new wxStaticText(p_main_panel, wxID_STATIC, _(p_calendar->GetDate().FormatISODate()));
    p_main_box_sizer->Add(p_static_text, 0, wxALIGN_CENTER_HORIZONTAL|wxALL|wxADJUST_MINSIZE, 5);
}

void MainFrame::OnDateSelectedChange(wxCalendarEvent& event)
{
	// -> Crash here ...
	p_static_text->SetLabel(p_calendar->GetDate().FormatISODate());
}

void MainFrame::OnClose (wxCloseEvent& WXUNUSED(event))
{
	delete p_static_text;
	delete p_calendar;
	delete p_main_box_sizer;
	delete p_main_panel;
	//free memory
	this->Destroy ();
}
I don't know why but when I click on a new date, the application crashes ...

Could you help me please ?

Marco
benedicte
wxWorld Domination!
wxWorld Domination!
Posts: 1409
Joined: Wed Jan 19, 2005 3:44 pm
Location: Paris, France

Post by benedicte »

Did you try to get the date directly from the event?

Code: Select all

void MainFrame::OnDateSelectedChange(wxCalendarEvent& event) 
{ 
        p_static_text->SetLabel(event.GetDate().FormatISODate()); 
} 
Also try to use local variables to determine which call makes your app crash: GetDate(), FormatISODate(), SetLabel()...
phlox81
wxWorld Domination!
wxWorld Domination!
Posts: 1387
Joined: Thu Aug 18, 2005 7:49 pm
Location: Germany
Contact:

Post by phlox81 »

you initialize local variables in your constructor,
they may overlay membervariables...
So p_static_text is a unvalid pointer in you EventHandler.
Marco
Experienced Solver
Experienced Solver
Posts: 65
Joined: Wed Aug 17, 2005 2:25 pm
Location: Rennes, France

Post by Marco »

Thank you benedicte and phlox81 for your responses,
phlox81 wrote:you initialize local variables in your constructor,
they may overlay membervariables...
So p_static_text is a unvalid pointer in you EventHandler.
I apologize !!! Thank you very much ...

But now I have another problem on line:

Code: Select all

delete p_main_box_sizer;
I can delete other components but I have a crash when I try to delete this wxBoxSizer ... Why ? Can someone help me please ?

Thank you in advance again,

Marco
phlox81
wxWorld Domination!
wxWorld Domination!
Posts: 1387
Joined: Thu Aug 18, 2005 7:49 pm
Location: Germany
Contact:

Post by phlox81 »

wxWidgets does the deletion of all childwindows and sizers by it self.
Actually when ever you hand over a Pointer to wxWidgets,
it mostly will handle its deletion. If you want to delete a pointer
your self, that is of wxWidgets (Window,sizer etc.) itself,
than call Destroy instead.

phlox
Post Reply