Article Preview
Buy Now
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):
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 usmuch more control over how that information looks when it is displayed.
- The ListBox provides two events for drawing—the
CellBackgroundPaint
event, and theCellTextPaint
event. Both accept agraphics
contextg
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 longSelect Case
statement into thesepaint
events that cover all the different possibilities of what to draw for each object that might be in aRowTag
, but that is horrible and not at all object-oriented. Amuch 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 ListBoxpaint
events can call and pass theirgraphics
contextg
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.