Prevent a Tab Panel Change Event from Changing Tabs

I have a situation where I need to check a condition after the user clicks on a tab before the tab changes. Another way of putting it is I need a “Before Change” event for the Tab Panel. There’s a in-depth discussion from the Nugs Archive 17 years ago

that addresses the very thing I’m trying to do but unfortunately never came to a working conclusion. Using Mouse Down in the Tab Panel comes close (albeit you have to check to see if the user clicked within the coordinates of the tabs themselves) but it doesn’t seem to return control to the point where the user clicked the tab.

Does anybody have any idea how I can do this? If the answer is a sub-classed Tab Panel with a new event (“Before Change”) can someone give me a head start on how to do this (I’ve created simple sub-classed controls but this one is out of my pay grade :disappointed:)

Thanks in advance for any assistance!
… Don

Windows only I’m afraid.


In checking your condition, are you expecting that as a result of that you might or might not end up allowing the tab change, or it it just to do some action before changing tab?

You can do both, the event is called before the change, if you return True it will cancel the action, akin to some other events in xojo but you can change it to whatever you want, the code is there mainly for the detection of the hit.

Precisely, Tim. I have a routine that checks for “all data entered” and one that compares two fields to assure they equal each other on Tab 0. All of the calculations and other functionality on all the tabs after the Tab 0 depend on those conditions being met before they can yield the proper information.

I do this in my Prefs tabpanel when the user changes a pref and tries to change tab without having saved or reverted the change. I’ll have a look to remind myself of what I do to effect that (IIRC, it’s also different under Linux).

No problem with Windows … that’s what I’m using and why I posted on the Windows forum section. Your sub-classed control worked PERFECTLY for the situation I have. Thank you SO MUCH!!!

And thanks to you, Tim, for jumping in. I’d be interested in seeing what you do to prevent tab change on condition.

1 Like


If the user does something such as change a pref, I set a flag and make a note of which the current tab is. If the user tries to change tab, then in the tabpanel’s Change event, if the flag is NOT set, I just return (thus allowing the change to the tabpanel to stand). I also just return if the user has clicked the tab that’s actually showing.

Undoing the change: For Win/macOS, just set the tabpanel’s SelectedPanelIndex back to the saved value. For Linux this doesn’t work, I have to set a timer which runs for one msec, and reset the SelectedPanelIndex in the timer’s Action event handler.

Thanks so much for sharing your methodology with me, Tim. The setup I had before I posted this question was almost identical to yours with the exception that I had a message dialog in the Change event that informed the user that all data was not present and therefore control was returning to the calling tab. The problem I had was that the tab would actually change, then the message showed. Control would return properly to the calling tab upon the user’s confirmation of the message (e.g., clicking “OK” to the dialog). The problem there was I didn’t want the tab to change (I wanted it to stay on the calling tab) when data on the calling tab was not complete.

The difference that stands out between what you do and what I tried is the presence of a message dialog in my code. I’m going to try your method to see if the lack of a message dialog interrupting the flow makes a difference. Now, my curiosity is killing me!

Thanks again, Tim … I sincerely appreciate your time taken to try and help me,

I have a message area on the tabpanel where I put out a message telling the user that they have unsaved changes.

Does the tab actually change before that message appears?

A BeforeChange (return True to cancel the change that would follow it) event would be very useful, and could potentially be introduced for other controls, in their contexts, logically.

Someone, please, file a Feature Request.


No, it gets written by the Change event handler to the message area of the tab you don’t want to change from.

Unfortunately, when creating all this, I made the tabpanel use the entire window it’s in, thus forcing me to put a copy of the message area on each tab. Well - perhaps not so serious, I needed a copy of the OK and Cancel/Revert buttons on each tab anyway, as each has to do different things.

Perhaps for PagePanel too?

Done, <>


I would love to see a Before Change event (return true to cancel) for the listbox … I’ve had to create workarounds for that a number of times. There’s a few more controls I’d add to that list.

Perhaps for many things that have a Change Event. Initially they can start with those paged ones, later introduce for more in their contexts.

Agreed! Most of my problems have come about with the paged controls. I would take that one feature add in a heartbeat.

1 Like

There’s many controls that would benefit from additional events but I feel that Xojo likes to keep things simple. It’s a shame that they don’t implement them behind an advanced feature checkbox if they wanted to keep barrier to entry nice and low.

This is simple as hell (unless the underlying OS nuts and bolts used by Xojo, like macOS or Linux, made it hard in some fashion way I can’t forecast) and also very useful.

1 Like