Search code examples
wxwidgets

How to get the user's new page selection in a Notebook control under Windows?


In my app, when a user has made changes to the data on one page of my notebook control, I want to prompt them to save or discard their changes when they switch to a different page. I have bound the EVT_BOOKCTRL_PAGE_CHANGING event for this, and have created a handler method.

However, I can't tell what page the user is switching to. According to the wxBookCtrlEvent docs,

under Windows, GetSelection() will return the same value as GetOldSelection() when called from the EVT_BOOKCTRL_PAGE_CHANGING handler and not the page which is going to be selected.

Is there a workaround?


Solution

  • I guess as a workaround, you could use a mouse handler checking for when the left button is clicked. In a handler for that event you could do a hit test to see where the click was made and store the value of the tab that was clicked. Something like this:

    void MyFrame::OnLeftDown( wxMouseEvent& event )
    {
        long flags;
        int ht = m_notebook1->HitTest( wxPoint(event.GetX(),event.GetY()), &flags);
    
        if( (flags & wxBK_HITTEST_NOWHERE) == 0 )
        {
            //store the value of ht somewhere 
        }
    
        event.Skip();
    }
    
    void MyFrame::OnNotebookPageChanging( wxNotebookEvent& event )
    {
        //use the stored value of ht here 
    }