Listbox Checkboxes by Erick Tejkowsi
05-30-03




Someone on the REALbasic NUG email lists today wanted to know how to use checkboxes in a Listbox control. To help him out, this week's tutorial will show you how to display checkboxes in a listbox and how to find out if a particular checkbox is checked or not.

Build the Interface
Fire up REALbasic and add a Listbox, an Editfield, and a Pushbutton to Window1. You can arrange the interface however you want, but mine looks like this: (note - the Listbox is on top)

5-30-03_img1.jpg (18k)

Add the Code
The code for this example looks like most Listbox code, with a few exceptions. First, we'll designate our Listbox as having three columns that display headings. Then, we need to assign a type to each column using the columnType property. There are a few choices:

We'll set the first column (i.e. column 0) as a checkbox, the second column as non-editable text, and the third column as editable text. Finally, we populate the Listbox with some dummy values just to demonstrate how this all works. Use the cellCheck property to set the initial value of each checkbox. The cellCheck property requires two parameters: a row and a column (just like the Cell property). Stick the following code in the Open event of Listbox1.

  //set up the listbox columns and headings
  me.columncount = 3
  me.hasheading = TRUE
  me.heading(0) = "On/Off"
  me.heading(1) = "Name"
  me.heading(2) = "Skill"
  
  //set the type for each column
  me.columnType(0) = 2//checkbox
  me.columnType(1) = 1//text 
  me.columnType(2) = 3//editable text 
  
  //populate Listbox1
  me.addrow ""
  me.cellCheck(me.lastIndex,0) = FALSE
  me.cell(me.lastIndex,1)="Erick"
  me.cell(me.lastIndex,2)="Rope tricks"
  
  me.addrow ""
  me.cellCheck(me.lastIndex,0) = TRUE
  me.cell(me.lastIndex,1)="Michael"
  me.cell(me.lastIndex,2)="Cooking 'possum"
  
  me.addrow ""
  me.cellCheck(me.lastIndex,0) = FALSE
  me.cell(me.lastIndex,1)="Dan"
  me.cell(me.lastIndex,2)="Fire-eating"
  
  me.addrow ""
  me.cellCheck(me.lastIndex,0) = TRUE
  me.cell(me.lastIndex,1)="Cletus"
  me.cell(me.lastIndex,2)="Long distance hopping"
  
  me.addrow ""
  me.cellCheck(me.lastIndex,0) = FALSE
  me.cell(me.lastIndex,1)="Jeremy"
  me.cell(me.lastIndex,2)="Yugo car repair"

To find out if a checkbox is checked or not, all you have to do is look at the Boolean value of cellCheck. Place the following code in the Action event of PushButton1. It loops through each row in the Listbox and grabs that row's name column if its checkbox is checked.

  
  dim i as integer
  //clear the editfield 
  editfield1.text=""
  //loop through items in listbox1
  //and look for rows that are checked
  for i=0 to listBox1.listcount-1
    if listBox1.cellCheck(i,0) then
      editfield1.text=editfield1.text+listBox1.cell(i,1)+chr(13)
    end if
  next
  

That's all there is to it. Run the project and click some of the chechboxes in the Listbox. Then, click the Pushbutton to see what happens. Simple, eh? Here's what the final result looks like:

5-30-03_img2.jpg (26k)

Conclusion
You can download the code for this week's project here. See you next week!