Changing Background Colour of a wxListView Item

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
Knows some wx things
Knows some wx things
Posts: 25
Joined: Tue Jun 25, 2019 3:19 pm

Changing Background Colour of a wxListView Item

Post by gillp28 » Wed Aug 07, 2019 4:03 pm

I am trying to change the background colour of a wxListView Item. So every time the connect button is clicked it should change the item colour but so for some reason it is not working.

Code: Select all

wxPanel* mainPanel;

const long MachineLearningLinkerFrame::ID_MENUITEM1 = wxNewId();
const long MachineLearningLinkerFrame::idMenuAbout = wxNewId();
const long MachineLearningLinkerFrame::ID_STATUSBAR1 = wxNewId();
const long MachineLearningLinkerFrame::ID_LIST = wxNewId(); 
const long MachineLearningLinkerFrame::ID_CONNECT = wxNewId();
wxListView* list;
wxListCtrl* link; 
wxTreeCtrl* treeList;

BEGIN_EVENT_TABLE(MachineLearningLinkerFrame, wxFrame)

MachineLearningLinkerFrame::MachineLearningLinkerFrame(wxWindow* parent, wxWindowID id)
	wxMenu* Menu1;
	wxMenu* Menu2;
	wxMenuBar* MenuBar1;
	wxMenuItem* MenuItem1;
	wxMenuItem* MenuItem2;
	wxButton* connect; 
	wxBoxSizer* verticalSizer = new wxBoxSizer(wxVERTICAL);
	wxBoxSizer* horizontalSizer = new wxBoxSizer(wxHORIZONTAL);
	wxBoxSizer* horSize2 = new wxBoxSizer(wxHORIZONTAL);

	Create(parent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, _T("wxID_ANY"));
	SetClientSize(wxSize(500, 500));
	mainPanel = new wxPanel(this, wxID_ANY); 
	//mainPanel->Bind(wxEVT_PAINT, &MachineLearningLinkerFrame::on_paint, this);
	verticalSizer->Add(horizontalSizer, 1, wxALL | wxEXPAND, 2);

	MenuBar1 = new wxMenuBar();
	Menu1 = new wxMenu();
	MenuItem1 = new wxMenuItem(Menu1, ID_MENUITEM1, _("Quit\tAlt-F4"), _("Quit the application"), wxITEM_NORMAL);
	MenuBar1->Append(Menu1, _("&File"));
	Menu2 = new wxMenu();
	MenuItem2 = new wxMenuItem(Menu2, idMenuAbout, _("About\tF1"), _("Show info about this application"), wxITEM_NORMAL);
	MenuBar1->Append(Menu2, _("Help"));
	StatusBar1 = new wxStatusBar(this, ID_STATUSBAR1, 0, _T("ID_STATUSBAR1"));
	int __wxStatusBarWidths_1[1] = { -1 };
	int __wxStatusBarStyles_1[1] = { wxSB_NORMAL };
	StatusBar1->SetFieldsCount(1, __wxStatusBarWidths_1);
	StatusBar1->SetStatusStyles(1, __wxStatusBarStyles_1);


	wxBitmap checked;
	checked.LoadFile("C:/Users/pavne/Downloads/redFlag.png", wxBITMAP_TYPE_PNG);
	wxBitmap unchecked; 
	unchecked.LoadFile("C:/Users/pavne/Downloads/greenFlag.png", wxBITMAP_TYPE_PNG);
	wxImageList* images = new wxImageList(checked.GetWidth(), unchecked.GetWidth(), true);


    list = new wxListView(mainPanel, ID_LIST, wxDefaultPosition, wxDefaultSize, wxLC_LIST );
	treeList = new wxTreeCtrl(mainPanel, wxID_ANY, wxDefaultPosition, wxDefaultSize); 
	link = new wxListCtrl(mainPanel, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_VRULES);
	connect = new wxButton(mainPanel, ID_CONNECT, "Connect", wxDefaultPosition, wxDefaultSize); 
	horizontalSizer->Add(list, 1, wxEXPAND | wxALL, 2); 
	horizontalSizer->Add(treeList, 1, wxEXPAND | wxALL, 2);
	horSize2->Add(connect, 1, wxEXPAND | wxALL, 4); 
	verticalSizer->Add(horSize2, 0, wxALIGN_TOP | wxALIGN_CENTER | wxTOP | wxRIGHT | wxBOTTOM, 4);
	verticalSizer->Add(link, 1, wxEXPAND | wxALL, 2); 

	wxListItem itemCol;
	link->InsertColumn(0, itemCol);

	link->InsertColumn(1, itemCol)

	for (int i = 0; i < 3; i++)
		list->InsertItem(i, "Item");


	Connect(ID_MENUITEM1, wxEVT_COMMAND_MENU_SELECTED, (wxObjectEventFunction)& MachineLearningLinkerFrame::OnQuit);
	Connect(idMenuAbout, wxEVT_COMMAND_MENU_SELECTED, (wxObjectEventFunction)& MachineLearningLinkerFrame::OnAbout);
	Connect(ID_LIST, wxEVT_COMMAND_LIST_ITEM_SELECTED, (wxObjectEventFunction)& MachineLearningLinkerFrame::OnItem);
	Connect(ID_CONNECT, wxEVT_COMMAND_BUTTON_CLICKED, (wxObjectEventFunction)& MachineLearningLinkerFrame::onConnect); 
	Connect(wxID_ANY, wxEVT_LEFT_DOWN, wxMouseEventHandler (MachineLearningLinkerFrame::onMouse));


void MachineLearningLinkerFrame::OnQuit(wxCommandEvent& event)

void MachineLearningLinkerFrame::OnAbout(wxCommandEvent& event)
	wxString msg = wxbuildinfo(long_f);
	wxMessageBox(msg, _("Welcome to..."));

void MachineLearningLinkerFrame::on_paint(wxPaintEvent& WXUNUSED(event))
	// you *must* create a wxPaintDC in a paint event handler.
	wxPaintDC dc(mainPanel);

	// dummy drawing
	//dc.DrawLine(wxPoint(10, 10), wxPoint(100, 100)); 

void MachineLearningLinkerFrame::onMouse(wxMouseEvent& event)
	if (event.Dragging())
		wxClientDC dc(mainPanel);
		wxPen pen(*wxRED, 1); // red pen of width 1

void MachineLearningLinkerFrame::OnItem(wxCommandEvent& event)

void MachineLearningLinkerFrame::onConnect(wxCommandEvent& event)
	long tmp = link->InsertItem(0, list->GetItemText(list->GetFocusedItem()));
	link->SetItem(tmp, 1, treeList->GetItemText(treeList->GetFocusedItem())); 
	list->SetItemBackgroundColour(list->GetFocusedItem(), wxColour(*wxRED));

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

Re: Changing Background Colour of a wxListView Item

Post by PB » Wed Aug 07, 2019 5:12 pm

The documentation says that this function only works in report view mode (wxLC_REPORT). You create the list control with wxLC_LIST.

Knows some wx things
Knows some wx things
Posts: 25
Joined: Tue Jun 25, 2019 3:19 pm

Re: Changing Background Colour of a wxListView Item

Post by gillp28 » Wed Aug 07, 2019 6:51 pm

Thanks. I also have a another problem. When i Initially start the program i only see one of the lists, but when i expand it I see all the lists and the buttons how they are supposed to be initially.

Part Of The Furniture
Part Of The Furniture
Posts: 4371
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Changing Background Colour of a wxListView Item

Post by ONEEYEMAN » Wed Aug 07, 2019 7:19 pm

Did you successfully added the controls to the sizers and called Layout()?

Thank you.

Post Reply