France,
Toulouse,
Wednesday, October, 27th, 2004,
4:52 pm.
Hello World,
Here's the second piece of my post.
Now, we go further.
- Quit your application to come back to REALbasic.
You are face to face with the "Window1 Source Code" and the "ListBox2"'s
"Open" event handler is selected.
- Select the "CellClick" event handler of our "ListBox" control called
"ListBox2".
The right content of the code editor window displays:
Function CellClick(row As Integer, column As Integer, x As Integer, y As
Integer) As Boolean
End Function
Don't be afraid by the declaration of this function. The most important
things to keep in mind are the "row" and "column" parameters.
- Type the following code in the "CellClick" event handler:
Function CellClick(row As Integer, column As Integer, x As Integer, y As
Integer) As Boolean
MsgBox "You select cell(" + Str(row) + ", " + Str(column) + ") !"
End Function
Note:
The "Str()" method allows us to convert a integer to string.
We use this method since the "MsgBox()" method deal only with string.
- Run your application thank to the "Debug -> Run" menu command.
The application starts and the main window is displayed with our three
controls.
- Move the mouse cursor over the string "Cell 0 0" displayed by the
first row and first column of our two based column "ListBox" control.
- Press the mouse button.
A dialog box window appears with a message similar to "You select cell(0, 0)
!".
If you click on another cell, you will get the same message but the
"coordinates" will be different.
Note:
As I explained before, the "ListBox.Cell()" is an enhanced version or a two
dimensional version of the "ListBox.Lis(...)" property.
Likewise, the "CellClick()" event handler is an enhanced or a two
dimensional version of the "Change()" event handler.
I think the things should be now clearer for you.
At this point, you are able to read, write and manage basic user
interactions with cells from the code.
How to edit in real time a cell, that is to say how a user can edit the
content of cells ?
- Quit your application to come back to REALbasic.
As usual, the "Window1 Source Code" is the foreground window.
In the browser of the code editor window, the "CellClick" event handler of
"ListBox2" is still selected since it's the last place where you typed some
code in.
- Erase the code from this "CellClick" event handler.
- In the browser of the code editor window, select the "Open" event
handler.
The content of this event handler should be:
Sub Open()
Me.AddRow "Cell 0 0"
Me.Cell(0, 1) = "Cell 0 1"
Me.AddRow "Cell 1 0"
Me.Cell(1, 1) = "Cell 1 1"
Me.AddRow "Cell 2 0"
Me.Cell(2, 1) = "Cell 2 1"
End Sub
- At the end of this code, add the following instruction:
Me.CellType(2, 1) = 3
The body of the "Open" event handler should look like this:
Sub Open()
Me.AddRow "Cell 0 0"
Me.Cell(0, 1) = "Cell 0 1"
Me.AddRow "Cell 1 0"
Me.Cell(1, 1) = "Cell 1 1"
Me.AddRow "Cell 2 0"
Me.Cell(2, 1) = "Cell 2 1"
Me.CellType(2, 1) = 3 // The new instruction to add
End Sub
Only the last line of code is new.
- Run your application by choosing the "Debug -> Run" menu command.
Your application is launched and our main window appears with our three
controls.
- Move the mouse over the content of the two based column "ListBox"
named "ListBox2".
If the mouse cursor is over the first cell of the first row, that is to say
"Cell 0 0", the mouse cursor is still an arrow. Idem for "Cell 0 1", "Cell 1
0", "Cell 1 1" and "Cell 2 0".
But, over "Cell 2 1", the shape of the mouse cursor change to become an
I-beam (an insertion text) cursor.
- Click on the cell that content "Cell 2 1".
An text field appears automatically allowing you to edit the content of the
clicked cell.
Play with it like you do with EditField controls: type in new characters,
select, copy and past, etc.
- Press the "Return" key of your keyboard.
The edit field disappears and the content of the cell is updated.
What happened ?
It's simple: by writing the last line of code in the "Open" event handler of
the "ListBox" control called "ListBox2", you tell REALbasic that the cell
located at the coordinates (2, 1) changes its nature: the new nature or
status is to be editable (technically, the integer value "3"). By doing
this, you tell REALbasic: "Ok, this cell is editable, so if the user try to
interact with this cell you worry about the appropriate actions".
That means by specifying "3", you automate the management of editable cells:
change the appearance of the mouse cursor, display an edit field after a
mouse click, and so on.
To summarize, thank to the "ListBox.CellType(...)" property, REALbasic will
work for you. Pretty cool for beginners, no ?
Now, you may have a question: I have a "ListBox" control with thousand
cells. Must I set the "ListBox.CellType(...)" property thousand time to
enable the editing mode (i.e. the integer value 3) of these cells ?
(By using a "For ... To ... Next" loop, for exemple.)
- Quit your application to come back to REALbasic.
The "Window1 Source Code" window is in front of your screen.
The "Open" event handler of the "ListBox" control called "ListBox2" is still
selected.
- At the end of the "Open" event handler body, remove the last line of
code.
Now, the body of the "Open" event handler looks like this:
Sub Open()
Me.AddRow "Cell 0 0"
Me.Cell(0, 1) = "Cell 0 1"
Me.AddRow "Cell 1 0"
Me.Cell(1, 1) = "Cell 1 1"
Me.AddRow "Cell 2 0"
End Sub
- At the end of this "Open" event handler body, add the following line
of code:
Me.ColumnType(1) = 3
Hence the body of the "Open" event handler looks like this:
Sub Open()
Me.AddRow "Cell 0 0"
Me.Cell(0, 1) = "Cell 0 1"
Me.AddRow "Cell 1 0"
Me.Cell(1, 1) = "Cell 1 1"
Me.AddRow "Cell 2 0"
Me.Cell(2, 1) = "Cell 2 1"
Me.ColumnType(1) = 3
End Sub
- Run your application by choosing the "Debug -> Run" menu command.
Your application is launched and our main window appears with our three
controls.
- Move the mouse cursor over a cell that belong to the second column.
The mouse cursor become an I-beam cursor telling you that the cell under
your mouse cursor is editable.
That works the same for every cells of the second column.
Like you did it in the previous step, you can edit every cells that belong
to the second column (remind: technically the second column is numbered 1).
So, instead of setting the type of cells, cell by cell, you can set a entire
column of cell quickly. If you enable the editing mode of one column, every
cells of this column become editable.
Now, I would like to know how to be informed by REALbasic that the user has
validated (or cancel) the edit field of an editable cell, no matter if the
content has changed or not.
- Quit your application to come back to REALbasic.
The "Window1 Source Code" window is in front of your screen.
The "Open" event handler of the "ListBox" control called "ListBox2" is still
selected.
- Select the "CellAction" event handler.
The right side of the code editor window change to display the following
code:
Sub CellAction(row As Integer, column As Integer)
End Sub
- Type the following code in this event handler:
Sub CellAction(row As Integer, column As Integer)
MsgBox "The edit field has been activated at cell(" + Str(row) + ",
" + Str(column) + ") !!"
End Sub
- Run your application by choosing the "Debug -> Run" menu command.
Your application is launched and our main window appears with our three
controls.
- Click on the "Cell 2 1" cell.
The edit field appears.
- If you wish, type some text in. But it's optional.
- Press return of click outside the edit field area.
The edit field disappears and a dialog box window is displayed on screen
with the following message:
"The edit field has been activated at cell(2, 1) !!"
So, the "CellAction" event occurs when the edit field disappears.
Now you know when a user decide to validate (or cancel) editing of an
editable cell.
Now, we are going to see the last part of this post.
- Quit your application to come back to REALbasic.
The "Window1 Source Code" window is in front of all other windows.
The "CellAction" event handler of the "ListBox" control called "ListBox2" is
still selected.
- In the browser of the code editor window, select the "Open" event
handler of the "ListBox" control called "ListBox2".
The body of this event handler looks like this:
Sub Open()
Me.AddRow "Cell 0 0"
Me.Cell(0, 1) = "Cell 0 1"
Me.AddRow "Cell 1 0"
Me.Cell(1, 1) = "Cell 1 1"
Me.AddRow "Cell 2 0"
Me.Cell(2, 1) = "Cell 2 1"
Me.ColumnType(1) = 3
End Sub
At the end of the body of this event handler, replace the last line of
code by the following instruction:
Me.Cell(1, 2) = "The Invisible Cell"
The result looks like this:
Sub Open()
Me.AddRow "Cell 0 0"
Me.Cell(0, 1) = "Cell 0 1"
Me.AddRow "Cell 1 0"
Me.Cell(1, 1) = "Cell 1 1"
Me.AddRow "Cell 2 0"
Me.Cell(2, 1) = "Cell 2 1"
Me.Cell(1, 2) = "The Invisible Cell"
End Sub
- Run your application by choosing the "Debug -> Run" menu command.
Your application is launched and our main window appears with our three
controls.
You are likely wondering if something took place.
You are right.
Note that REALbasic doesn't complain (I will explain later what means this
remark).
- Quit your application to come back to REALbasic.
The "Window1 Source Code" window is in front of your screen.
The "Open" event handler of the "ListBox" control called "ListBox2" is still
selected.
- Close this window.
The window that represents the content of our "Window1" window is in
foreground.
- Double-click on our "PushButton" that "Window1" hosts.
The "Window1 Source Code" window appears and the "Action" event handler of
the "PushButton" control called "PushButton1" is selected by default. The
"Action" event handler of the "PushButton" control called "PushButton1"
shows the following code:
Sub Action()
MsgBox ListBox1.Cell(0, 1)
End Sub
- Replace this code as follow:
Sub Action()
ListBox2.ColumnCount = 3
End Sub
- Run your application by choosing the "Debug -> Run" menu command.
Your application is launched and our main window appears with our three
controls.
As before, nothing happened.
- While you are looking at the content of the multiple column "ListBox"
(i.e. "ListBox2"), press your "PushButton" control.
The "ListBox" control now hosts three columns, and the third cell
(technically the column numbered 2) of the second row (technically the row
numbered 1) displays the text "The Invisible Cell".
Note:
The "ListBox" control is probably not large enough to display entirely the
content of this cell. The text is truncated.
With this step you learn two thinks:
- The easiest idea to understand is the fact that the
"ListBox.ColumnCount" property allow you to change the number of columns of
"ListBox" control.
- A "ListBox" control seems to host invisible columns.
So, keep in mind that "ListBox.ColumnCount" is the number of VISIBLE"
columns and that "ListBox" host non visible but available columns.
Note:
By fill in a invisible cell, REALbasic could complain (no "out of bound"
error message). It's not the case and now you know why.
Note:
Invisible columns are useful to store temporary data like id number when you
write an address book program.
The id number could read from a database.
Et voilà !
I hope this "short" but detailed "post-tutorial" (or "tutorial-post) will
help you to understand the basic features of "ListBox" controls and to
approach other features of "ListBox" controls quietly to extend the
behaviors of your "ListBox" controls hosted by your application.
Let's me know if you have any questions, if you want a web page version of
these posts with snapshot and some other enhancements and if there are
misspellings (I'm sure they are).
Kindest Regards,
Nils
----
----
P.S.:
- Sorry for the delay.
- Sorry for my English.
I promise you to enhance my English with the time: it's a matter of time.
However, I would be very tolerant if you speak an approximate French. ;-)
Thank you for your patience.
----
----
"On ne voit bien qu'avec le coeur. L'essentiel est invisible pour les yeux."
"We only see well with our hearts. What is truly important is invisible to
our eyes."
Le Petit Prince, Antoine de Saint Exupéry
----
----
"Ce n'est pas l'homme qui doit s'adapter à la machine mais la machine qui
doit répondre aux besoins de l'homme."
"It is not the man who should adapt himself to the machine but the machine
which should meet the needs of the man."
Nils Frisch
----
----
Nils Frisch
Toulouse,
France.
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>
Search the archives of this list here:
<http://www.realsoftware.com/listarchives/lists.html>
|