HOME, PRODUCTS, ARTICLESCONTACTGLOSS CONTOUR SUITE™ FOR .NET

DOWNLOAD OR BUY Gloss Contour Suite™GCSuite™ FEEDBACKGCSuite™ SUPPORTC# OR .NET TECHNICAL ARTICLE FEEDBACK

GlossContourButton™

GlossContourButton™.

HOW TO IMPLEMENT EFFICIENT TOGGLE GROUP BEHAVIOR

An elementary but regular topic of UI design is how best to implement toggle group behavior — where clicking a given button leaves it in the Down state, causing the remainder of a group to revert to up. This article provides two examples — a usual tag based pattern, and a preferable generic (type safe) list based pattern, with source for both in C#.

Toggle group behavior: Clicking the button on the right causes it to indicate an application condition by remaining in a Down state — reverting the remainder of the group to up.

TAG STYLE TOGGLE GROUP BEHAVIOR

A typical Toggle Group approach uses a common click delegate to iterate the controls of a common parent container and identify Toggle Group members by a common Tag value, setting NonSenderMember.Down to false and sender.Down to true.

The downsides of this approach are,

  1. that members must be parented by a common container,
  2. that non-member types may require casting or reflection for processing,
  3. and that potentially many non-members can be iterated to ultimately identify relatively few members.

Here, a common click delegate processes a tag value of 12345 to provide this style of toggle group behavior:

C# TAG STYLE TOGGLE GROUP BEHAVIOR EXAMPLE

Copy code to clipboard (JavaScript/IE).

Even in each iteration, this method requires relatively compute-intensive operations which are redundant to the generic list approach we will look at next. Because we are not using a type safe list genericized to the specific type we need to handle, substantial casting is required:

  1. To process the sender argument as our button type, we have to cast the sender to s as the processed type.
  2. We next have to test if s is null, because we cannot later process s if s is null. Nor would there be any purpose in performing the iteration if we cannot process the sender (s).
  3. Then we iterate the controls of the parent container, having to process each member as ( Control )c because this is the generic type of the Controls array.
  4. If c has the tag value we are looking for, then we try to cast c to the class we have to be able to process in the instance g.
  5. Again, we have to test for a null result of this cast to g, because we cannot otherwise process g.
  6. Finally, we set the Down property of g to the result of a comparison of g to s, which of course tests if the currently processed button member is the sender.

Even if we can eliminate a cast or two for a given implementation, we cannot eliminate all casts unless the list type matches our process type. A matched generic list based approach eliminates the casting issues and makes it possible to write a substantially more compact, efficient CommonToggleGroupHandler( ), as we find in the example below.

PREFERABLE GENERIC LIST APPROACH TO TOGGLE GROUP BEHAVIOR

A casting-free approach uses a dedicated generic list to which only a specific toggle group's members belong. The advantages of this approach are,

  1. Only the members of the toggle group are iterated, which contributes to maximum speed.
  2. Members do not have to be parented by the same outer control.
  3. The type safe list eliminates any need for reflection or casting, which further contributes to maximum speed and minimizes the processing footprint.

The following example,

  1. declares and creates a generic list,
  2. dynamically creates toggle group members,
  3. dynamically adds the toggle group members to the generic list, and
  4. dynamically assigns a common click delegate to the click event of each toggle group member.
  5. As in the previous example, a CommonToggleGroupHandler provides the desired toggle group behavior. Here you see however, how the method is streamlined and expedited by using a type safe generic list:

C# GENERIC LIST TOGGLE GROUP BEHAVIOR EXAMPLE

Copy code to clipboard (JavaScript/IE).

As we can easily see by comparing the CommonToggleGroupHandlers, the casting-free, genericized approach is substantially more efficient. As visually created examples require that you manually perform the creation and assignment aspects, this example can be considered to provide coded instructions for the equivalent visual steps.

Resultant Toggle Group implementation.

FIRING FURTHER PROCESSES

As a matter of proper streamlining, these examples purposely exclude other possible forking. In other words, to eliminate branching logic in each iteration, and to make use of existent delegate handling processes to accomplish the same purpose, if a button's click event must invoke further processes, further delegates are attached directly to the button to do so.

STATE COMPREHENSIVE PROCESSING

The usual object of toggle groups is to represent application states to further processes. Branching is usually performed with nested if statements, because multiple state values are not a matter for switches.

C# EXAMPLE

Copy code to clipboard (JavaScript/IE).

RELATED TOPICS

© Copyright 1995-2007, by ADVANCE Information Systems, Inc. ALL RIGHTS RESERVED.Copyright 1995-2007, by ADVANCE Information Systems, Inc. ALL RIGHTS RESERVED.

Firefox™.Best viewed in Mozilla Firefox™.