Monday 19 December 2011

Flipping Pages

I saw in the RealStudio forums that someone wanted to know how to get confirmation to change the page on a PagePanel BEFORE it changed. As there is no way to intercept the Change event before it fires, I thought I would have a crack at solving the issue.

The first step is to create a subclass of the PagePanel. Although we want to change some of the functionality, we still want the majority of the functionality to remain intact, so it makes sense to create a subclass. No need to re-invent the wheel.

Next, we need to add three new event definitions for the class.

CanChange
Change
Open

The Change and Open events are to reintroduce the existing events, because we will need to use them for this subclass.
The other event CanChange is the key to this modification. Ensure that the CanChange event returns a boolean value.

We also need two new private variables. These are an integer variable, called OldPageValue and a boolean variable called DoNotConfirm.

That's it for the setup. Easy right?
Well, now for the hard bit. Well, not really, this class only requires two routines, both of which are located in the event handlers.
First of all, we need to initialise everything. As normal, we do this in the Open event handler and is accomplished with just three lines of code:

The first line simply sets the value of OldPageID to the initial page displayed. Normally, this would be page zero. The next line sets DoNotConfirm to False. This means that we do not want to bypass the Change event.

Now for a slightly trickier piece of code, but still fairly simple:

We put this code in the original Change event of the PagePanel. I shall explain what is happening here line by line:


if not DoNotConfirm Then
If the DoNotConfirm variable is set to False then proceed with this. If the variable is set to True, then we need to ignore this entire routine.

if RaiseEvent CanChange then 
  OldPageID=Value
  RaiseEvent Change
Now, we call the new event handler. If the return value is True, then we want to change the page. As the page has actually already changed, we just store the new value of the PagePanel and then call the new Change event.

else
  DoNotConfirm=True
  Value=OldPageID
  DoNotConfirm=False
If the CanChange event returned False, then we need to return to the previous page. However, we do not want this Change event processing again, so we set the DoNotConfirm variable to True before setting the Value of the PagePanel back to the OldPageID. We also need to reset the DoNotConfirm variable back to False once the Value has been set.

That is it. We now have a new control that we can drag onto our window which will give us a PagePanel that can test to see if the change of page is permitted, either by some internal processing, or user confirmation.

No comments:

Post a Comment