m_count should match ListView_GetItemCount() 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
the_transltr
In need of some credit
In need of some credit
Posts: 9
Joined: Sun Feb 12, 2012 6:17 pm

m_count should match ListView_GetItemCount()

Post by the_transltr » Fri Mar 02, 2012 6:38 am

A minimal example of inserting data into a wxListCrtl. But i got error when i click the button. Here is the code.

Code: Select all





#include <wx/wx.h>
#include<wx/listctrl.h>


using namespace std;
 
class MyApp : public wxApp
{
   virtual bool OnInit();
};
 
IMPLEMENT_APP(MyApp)
 
class MyFrame : public wxFrame
{
public:
   MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
protected:
	void GetData(wxCommandEvent &event);
private:
	wxListCtrl * listview;
	enum{BUTTON = 1, LIST};
};
 
bool MyApp::OnInit()
{
  
	   MyFrame *frame = new MyFrame(wxT("Hello World"), wxPoint(50,50),
     wxSize(450,350));
     frame->Show(TRUE);
     SetTopWindow(frame);
     return TRUE;
}
 
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
	: wxFrame((wxFrame*)NULL, -1, title, pos, size)
{
  
	wxPanel * panel = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxDefaultSize);

	wxButton * button = new wxButton(panel, BUTTON, wxT("Select"), wxPoint(10, 20), wxSize(30, 20));

	
	listview = new wxListCtrl(panel, LIST, wxPoint(50, 20), wxSize(800, 600),wxLC_REPORT);



	listview->InsertColumn(0, wxT("Test"), wxLIST_FORMAT_LEFT );
	listview->SetColumnWidth(0, 40);


	

	button->Connect(BUTTON,wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(MyFrame::GetData));


}




void MyFrame::GetData(wxCommandEvent &event){


	int count_column = 7;

	

	wxString result = wxString::Format(wxT("%i"), count_column);

	listview->InsertItem(0, result);
}
 


Need Help. Thanks in advance.

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2677
Joined: Sun Jan 03, 2010 5:45 pm

Re: m_count should match ListView_GetItemCount()

Post by PB » Fri Mar 02, 2012 7:18 am

the_transltr wrote:

Code: Select all

MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
	: wxFrame((wxFrame*)NULL, -1, title, pos, size)
{
  
...
	button->Connect(BUTTON,wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(MyFrame::GetData));
}
You're connecting the event in a wrong way. Use either

Code: Select all

Connect(BUTTON, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(MyFrame::GetData));
or

Code: Select all

button->Connect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(MyFrame::GetData), NULL, this);
If possible (i.e. you're using wxWidgets 2.9), use Bind instead of Connect, much more bugproof.

Also if possible, use a topic title matching the actual topic. Thanks ;)

the_transltr
In need of some credit
In need of some credit
Posts: 9
Joined: Sun Feb 12, 2012 6:17 pm

Re: m_count should match ListView_GetItemCount()

Post by the_transltr » Fri Mar 02, 2012 1:50 pm

Actually the connect method is working if i remove the code

Code: Select all


int count_column = 7;

wxString result = wxString::Format(wxT("%i"), count_column);

listview->InsertItem(0, result);

inside MyFrame::GetData and change it to something like wxMessageBox(...) if i click the button, the messagebox will appear. But that's not what i need. What i need is to add item/s in wxListCtrl on button click, but i am getting error and the error is the title.

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2677
Joined: Sun Jan 03, 2010 5:45 pm

Re: m_count should match ListView_GetItemCount()

Post by PB » Fri Mar 02, 2012 2:13 pm

No it is not working. Have you at least tried what I told you to do?

You are making a common beginner mistake
the_transltr wrote:

Code: Select all

 button->Connect(BUTTON,wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(MyFrame::GetData));
vs
wxWidgets Reference Manual wrote:void wxEvtHandler::Connect (int id, wxEventType eventType, wxObjectEventFunction function, wxObject * userData = NULL, wxEvtHandler * eventSink = NULL)
Leaving eventSink at default NULL value means that you're telling Connect that the event handler is in button (button->this). wxWidgets happily oblige your wish and execute MyFrame::GetData() in which this is actually an instance of wxButton - button. You can easily verify that, comparing addresses of button in MyFrame::MyFrame() and this in MyFrame::GetData().
So if you try to touch anything that supposedly belongs to MyFrame there, like listctrl, your app is going to crash bad (your wxMessageBox call probably didn't use anything MyFrame specific so the crash was avoided).

the_transltr
In need of some credit
In need of some credit
Posts: 9
Joined: Sun Feb 12, 2012 6:17 pm

Re: m_count should match ListView_GetItemCount()

Post by the_transltr » Sat Mar 03, 2012 2:56 am

Worked like a charm! Thank you very much!

Post Reply