Special

Introducing the “Welcome to Xojo” Bundle!

New to Xojo and looking for guidance? We've put together a terrific bundle to welcome you! Xojo Bundle

This bundle includes six back issues of the magazine -- all of year 21 in printed book and digital formats -- plus a one-year subscription (beginning with 22.1) so you'll be learning all about Xojo for the next year. It's the perfect way to get started programming with Xojo. And you save as much as $35 over the non-bundle price!

This offer is only available for a limited time as supplies are limited, so hurry today and order this special bundle before the offer goes away!

Article Preview


Buy Now

Issue 18.4 ('Mac to iOS')
Instant purchase and download via GumRoad!

FEATURE

Better Examples - ListBox Part 2

From Ooops! to OOP with the ListBox

Issue: 18.4 (July/August 2020)
Author: Markus Winter
Author Bio: Markus is a Molecular Biologist who taught himself REALbasic programming in 2003 to let the computer deal with some exceedingly tedious lab tasks. Some call it lazy, he thinks it smart. He still thinks of himself as an advanced beginner at best.
Article Description: No description available.
Article Length (in bytes): 19,186
Starting Page Number: 27
Article Number: 18403
Resource File(s):

Download Icon project18403.zip Updated: 2020-07-01 11:01:47

Related Link(s): None

Excerpt of article text...

In case you are a new subscriber to xDev (or you have forgotten what I wrote in Part 1), here is the executive summary to get you up to speed:

  • We want to use the ListBox in an "object-oriented way." What this means is that we want to simply pass an object (like Employee or Department) to the Listbox and it will take care of the rest, and we will not need to hardcode which information (text or property) goes into which cell (an approach that can get messy rather quickly, is hard to change and maintain, and might slow down the ListBox considerably).

  • Therefore, we do not store our data in the cells of the Listbox, but as an object in a row RowTag. To display the information that we want to display, we will draw that information as this gives us much more control over how that information looks when it is displayed.

  • The ListBox provides two events for drawing—the CellBackgroundPaint event, and the CellTextPaint event. Both accept a graphics context g as parameter into which they draw. Note that these events are only called for visible cells as it makes little sense to draw cells that aren't on screen.

  • We could put a long Select Case statement into these paint events that cover all the different possibilities of what to draw for each object that might be in a RowTag, but that is horrible and not at all object-oriented. A much better approach is for the objects themselves to know how to do the drawing.

  • Consequently, we need to give them a Draw method that the ListBox paint events can call and pass their graphics context g to, akin to passing someone a piece of paper and telling them: "Draw whatever you want on here" (see Fig 1).

...End of Excerpt. Please purchase the magazine to read the full article.