Want to group RadioButtons?

| | Comments (4)

One thing I've seen people stumble on every once in a while is the proper way to group radio buttons in REALbasic. If you're not familiar with the problem, here's a little example.

Let's say that you have two "sections" of radio buttons that you want to display on the same window. The first section has three options: red, green and blue. The second section has five options: square, circle, triangle, line and point. Obviously, these radio buttons sections need to operate independently of one another. You want to be able to have two of the radio buttons selected at any given point in time.

So let's look at a simple way of laying these out. Here's the first design that comes to mind:

Simple Approach

You'll notice when you run your application that you can only select one button amongst all of the radio buttons! That's not the behavior you want though. So what's going on here?

In REALbasic, radio buttons are grouped by their parent. In the above example, the parent is the window, and since all the radio buttons have the same parent, they all belong to the same group. So let's modify the layout a little bit to give each group a new parent by using the GroupBox control. Now our application will look something like this:

Simple Approach

You'll see that you can have two separate RadioButton controls that are set instead of just one. That's because each group now has different parents. But that doesn't get you the same UI that you wanted from the first example. How would you get that UI to work?

Simple! Use a Canvas control as the parent of the RadioButton groups. Since the Canvas doesn't have a visual represenation unless you draw one yourself, it's the perfect "invisible" parent. So in the window editor, lay the canvases out like this:

Simple Approach

This basically sets up the invisible parents for each of the groups of controls. Then, when you run the application, you'll get the desired results:

Simple Approach

And that is how you properly group RadioButtons in REALbasic when you don't want to have a visual parent. Of course, I don't recommend this for a user interface because it goes against UI guidelines, but that's a story for another time.

4 Comments

Thanks! I figured this was a pretty easy topic, but it seems to catch people from time to time.

That reminds me of one question I have about GroupBoxes. If I want to put existing controls inside a group box, they don't get "binded" to the group box, I need to delete the controls and recreate them by dragging them inside the group box. Is there a way to put existing controls in a group box?

Certainly -- you need to set the ControlOrder for the control so that it is created *after* the group box (so its control order > the control order of the group box).

You can do this from the properties list, or from the control order dialog.

Leave a comment

Disclaimer

I'm currently an employee of REAL Software. My blog is mine. The opinions represented in this blog are mine as well and may not represent my employer's opinions. All original material is copyrighted and property of the author.

REALbasic® is a registered trademark of REAL Software, Inc. REAL SQL Server™ and Lingua™ are pending trademarks of REAL Software, Inc. All rights reserved.