O'Reilly logo

REALBasic: TDG, 2nd Edition by Matt Neuburg

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Basic ListBox Techniques

This section consists of some simple examples illustrating the basic techniques for manipulating a ListBox’s contents and formatting, column display, pictures, and selection.

Contents and Formatting

Let’s start by creating the one-column ListBox at the left of Figure 14-1. We could do this by setting the InitialValue in the IDE; but populating a ListBox in code is more common, more challenging, and more important to understand. Let’s populate the ListBox in its Open event handler. The list being initially empty, it suffices to add each row:

me.addrow "Apple"
me.addrow "Pear"
me.addrow "Orange"
me.addrow "Strawberry"
me.addrow "Lemon"
me.addrow "Grapefruit"
me.addrow "Mango"
me.addrow "Banana"
me.addrow "Cranberry"

This lacks elegance and generality, and it is more usual to loop through some other data structure. Here, we start with a delimited string:

dim i,u as integer
dim s as string
s = "Apple,Pear,Orange,Strawberry,Lemon,Grapefruit,Mango,Banana,Cranberry"
u = countFields(s,",")
for i = 1 to u
    me.addrow nthfield(s,",",i)
next

Imagine now that we wish every other row to be in boldface. That can’t be accomplished in the IDE; in code, it’s straightforward. We’ll use CellBold, remembering to compensate for the fact that the row index, unlike a string field, is zero-based:

dim i,u as integer dim s as string s = "Apple,Pear,Orange,Strawberry,Lemon,Grapefruit,Mango,Banana,Cranberry" u = countFields(s,",") for i = 1 to u me.addrow nthfield(s,",",i) if i mod 2 = ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required