c++wxwidgetswxnotebook

Programmatically Add/Remove tabs on wxNotebook by PageText


I need to be able to programmatically add and remove tabs on a wxNotebook by the text/label that is displayed on each tab.

In windows, using a tab control and tab pages, I would be able to reference each tab by a key. The tab control has a map of tab pages keyed on the text of each tab.


Solution

  • Use the following helper method to convert from the tab label/text to the corresponding index of the wxNotebookPage. After you have the index of the wxNotebookPage, then you can use all of the wxNotebook's methods that expect the page index as an argument.

    int TabTestFrame::GetIndexForPageName( wxString tabText)
    {
    
         int end = Notebook1->GetPageCount();
    
         wxString selectedtabText = "";
    
         for ( int i = 0; i < end; i++)
         {
    
            selectedtabText = Notebook1->GetPageText(i);
    
            if (tabText == selectedtabText)
                return i;
    
         }
    
         return -1;
     }