Drawing a picture in REALbasic ListBox control line

In this tutorial we will learn how can we easy draw a picture in REALbasic ListBox control and how can we get the text from double clicked row or column in this control. So run REALbasic and start reading. Watch it, learn it, enjoy it!

ListBox Control Setup

Add to your Window ListBox Control and set the DefaultRowHeight property value to 32. Next prepare a 32×32 picture (called tutspolis) and drag it to your Project Tab.

Add a picture to REALbasic Project Tab

Adding text to ListBox control

In code editor go to ListBox Sub Open () and insert this code:

1
2
3
4
5
6
7
8
9
10
Dim i As Integer
 
for i = 1 to 5
  ListBox1.AddFolder "REALbasic City"
  ListBox1.Cell(Me.LastIndex, 1) = "F"
  ListBox1.AddFolder "REALbasic City 2"
  ListBox1.Cell(Me.LastIndex, 1) = "F"
next
 
 Me.ColumnAlignmentOffset(0)= 35

Drawing a graphic in ListBox

In REALbasic code editor go to ListBox Function CellTextPaint () and use this code:

1
2
3
4
5
6
7
if column = 0 then
  if Me.cell(row,1) = "F" then
     g.DrawPicture(tutspolis,2,2)
  else
     g.DrawString "Fake Line", 29,9
   end if
end if

ListBox DoubleClick event

Using this code in the ListBox Sub DoubleClick () event you can get the string, number of the cell, row that was double-clicked.

1
2
3
4
5
6
Dim row, column As Integer
 
row=Me.RowFromXY(System.MouseX - Me.Left - Self.Left, System.MouseY - Me.Top - Self.Top)
column=Me.ColumnFromXY(System.MouseX - Me.Left - Self.Left, System.MouseY - Me.Top - Self.Top)
 
MsgBox "You double-clicked on "+ Me.list(row)

draw a picture in realbasic list box

Author: Jakub Pawlak

I'm interested in all. Currently working on the development of TUTSPOLIS and I hope that in near feature the project will be successful.

5 Responses to “Drawing a picture in REALbasic ListBox control”

  1. Arjun says:

    Really its good article. This is one I am looking for. Saved my time. Thanks and keep rocking!!

  2. A real time saver. Cheers. – Steve

  3. Jakub Pawlak says:

    Thank you Steve and Arjun for nice word!

  4. JT says:

    Cool and very useful article.

    Re: Drawing a graphic in ListBox
    What if you wanted to have a dynamically loaded image?

    I have a picture that loads at runtime, how can I get that picture painted in the cell ?
    And if the user wants to change that picture with another during runtime?

    Thanks again Jakub — JT

  5. Jakub Pawlak says:

    Remove the ListBox1.Cell function and in ListBox Function CellTextPaint () enter:

    if column = 0 the
    g.DrawPicture(game4,2,2)
    end if

Leave a Reply

Copyrights (c) TUTSPOLIS | Powered by Wordpress MU | Inspired at Elegant Themes designed by qbap (HTML5 & CSS3)