Page 1 of 1

[wxMSW][wx3.0.3] Get menu-item label on click of a dynamically added menu in toolbar

Posted: Mon Aug 12, 2019 7:16 am
by Rudra
Hi,

In my wx application, I have wxITEM_DROPDOWN tool in which I am adding menu-items dynamically as follows,

Code: Select all

m_tooBar = new wxToolBar(m_titlePanel, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTB_NODIVIDER|wxTB_HORZ_TEXT);
m_tooBar->AddTool(wxID_LOAD, wxT("Load"), wxMEMORY_PNG(ICON_LOAD),
				  wxMEMORY_PNG(ICON_LOAD), wxITEM_DROPDOWN,
				  wxEmptyString, wxEmptyString, NULL);

xMenu *loadMenu = new wxMenu;
for(size_t iter = 0; iter < gloadMenuList.size(); ++ iter)
	loadMenu->Append(wxID_ANY, gloadMenuList[iter]);
	
m_tooBar->SetDropdownMenu(wxID_LOAD, loadMenu);
m_tooBar->Realize();
The menu-items count is not fixed so I used wxID_ANY while appending the menu-item.

On click, I get event but unable to figure out which menu-item is clicked. The menu-item label is unique so I want to know it to process further.

Please suggest.

Thanks,
R.

Re: [wxMSW][wx3.0.3] Get menu-item label on click of a dynamically added menu in toolbar

Posted: Mon Aug 12, 2019 3:49 pm
by doublemax
loadMenu->Append() returns a wxMenuItem. With wxMenuItem::GetId() you can retrieve the ID it was assigned. With that ID you can create a logical connection to another piece of information, e.g. through a hashmap.

Re: [wxMSW][wx3.0.3] Get menu-item label on click of a dynamically added menu in toolbar

Posted: Tue Aug 13, 2019 6:57 am
by Rudra
Thanks for the reply. It works for me.