Monday, September 17, 2012

Creating Web Dialog Boxes On-The-Fly Redux

As previously mentioned by Geoff in his post "Creating Web Dialog Boxes On-The-Fly", Real Studio web edition works a little differently than the desktop in how you set up a dialog for use.

It's handy to be able to drag an instance onto a window  and deal with its events and everything the same as any other control.

But, as Geoff noted there are times when you don't want to do it that way because you need to reset the dialog to a default state.

You might want to have a different dialog based on what browser is being used or other criteria. In such cases, you might want to use a dialog superclass and have several different implementations that vary in appearance for iOS, Android, or for different browsers.

This is easily possible using the same technique Geoff illustrated for dynamically creating a dialog.

For example, suppose you need a dialog that looks one way for Safari and another one for all other browsers- we could just as easily extend this to Firefox or make it appear different for iOS vs Android.

Instead of dragging one of each dialog onto your layout, you could have both derive from a common superclass. With only 1 or 2 variations it's easy to drag them all out, but if you need many more that could be tedious and makes adding a new one more difficult than it needs to be, and you'll end up having a lot events to handle.

If you have a common parent with subclasses your code can do:

dim theDialog As GenericMobileDialog // this is defined as the common parent class type
if plat = WebSession.BrowserType.Safari or plat = WebSession.BrowserType.SafariMobile then
theDialog = new SafariVersionOfDialog
else
theDialog = new NotSafariVersionOfDialog
end if
AddHandler theDialog.Dismissed, AddressOf LogInDismissed
theDialog.Show

And now you can set up as many as you want AND handle them all with a very few lines of code.

A sample project that demonstrates this can be found here.

No comments: