Sue D. Nymme
Sue D. Nymme

Reputation: 946

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?

Upvotes: 1

Views: 203

Answers (3)

VZ.
VZ.

Reputation: 22688

No, there is no workaround (if there were a reliable way to do it, wxWidgets would have been already doing it), the underlying native control simply doesn't provide this information.

You can either ask whatever you need to ask the user about in any case, independently of the page they're switching to, or ask them after they will have already switched -- which is, of course, going to look weird if you then decide to switch back.

If you really, really need this functionality, you might use non-native wxAuiNotebook instead.

Upvotes: 2

ravenspoint
ravenspoint

Reputation: 20462

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.

So, call GetSelection from EVT_BOOKCTRL_PAGE_CHANGED to get the new page.

Upvotes: 3

New Pagodi
New Pagodi

Reputation: 3554

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 
}

Upvotes: 3

Related Questions