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
gillp28
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;

//(*IdInit(MachineLearningLinkerFrame)
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)
//(*EventTable(MachineLearningLinkerFrame)
	EVT_MOTION(MachineLearningLinkerFrame::onMouse)
//*)
END_EVENT_TABLE()

MachineLearningLinkerFrame::MachineLearningLinkerFrame(wxWindow* parent, wxWindowID id)
{
	//(*Initialize(MachineLearningLinkerFrame)
	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);
	mainPanel->SetSizer(verticalSizer);
	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);
	Menu1->Append(MenuItem1);
	MenuBar1->Append(Menu1, _("&File"));
	Menu2 = new wxMenu();
	MenuItem2 = new wxMenuItem(Menu2, idMenuAbout, _("About\tF1"), _("Show info about this application"), wxITEM_NORMAL);
	Menu2->Append(MenuItem2);
	MenuBar1->Append(Menu2, _("Help"));
	SetMenuBar(MenuBar1);
	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);
	SetStatusBar(StatusBar1);

	

	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);

	images->Add(checked);
	images->Add(unchecked);


    list = new wxListView(mainPanel, ID_LIST, wxDefaultPosition, wxDefaultSize, wxLC_LIST );
	list->AssignImageList(images,0);
	treeList = new wxTreeCtrl(mainPanel, wxID_ANY, wxDefaultPosition, wxDefaultSize); 
	link = new wxListCtrl(mainPanel, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_VRULES);
	treeList->AddRoot("Pavneet"); 
	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;
	itemCol.SetText("Item");
	itemCol.SetImage(-1);
	link->InsertColumn(0, itemCol);

	itemCol.SetText("Linkage");
	itemCol.SetAlign(wxLIST_FORMAT_CENTRE);
	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));
	//*)
}

MachineLearningLinkerFrame::~MachineLearningLinkerFrame()
{
	//(*Destroy(MachineLearningLinkerFrame)
	//*)
}

void MachineLearningLinkerFrame::OnQuit(wxCommandEvent& event)
{
	Close();
}

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.SetBrush(*wxYELLOW);
	//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
		dc.SetPen(pen);
		dc.DrawPoint(event.GetPosition());
		dc.SetPen(wxNullPen);
	}
	*/
}

void MachineLearningLinkerFrame::OnItem(wxCommandEvent& event)
{
	list->Disable(); 
}

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));
	list->Enable(); 
}

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2075
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.

gillp28
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.

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 3527
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

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

Thank you.

Post Reply