I know that some of you got problem with understanding how to create hierarchical Listbox in REALbasic. This tutorial will discover a method to create a TreeView using Listbox Control in two easy to follow steps. Watch it, learn it, enjoy it!
Enabling Listbox hierarchical property in REALbasic will allows us to disclosure triangles for rows added via the AddFolder method. On Windows, plus and minus signs are used instead of disclosure triangles.
Add to Window1 a Listbox Control and go to it Sub Open () event available in REALbasic Code Editor. Few things will happen here so look into the code comments. Before this I will explain you what NthField Function can do. The NthField function returns the field value from the source that precedes the fieldNumber occurrence of the separator in the source – NthField(source, separator, fieldNumber). We will use this to get our category menu and submenu options for each category (DEVICES,PLACES).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | Dim menucat, submenu As String Dim i As Integer me.Hierarchical = true //enabling the hierarchical Listbox property me.ColumnWidths = "125,0" //setting the Listbox column width menucat = "DEVICES,PLACES" //creating the menu category list for Listbox submenu = "Macintosh HD;Desktop,qbap,Applications,Documents,Downloads,Movies" //creating submenu list for menucat for i = 1 to CountFields (menucat, ",") //count the category list by "," mark if NthField (submenu, ";", i) < > "" then me.AddFolder "" //add a submenu me.Cell (i-1,1) = NthField (submenu, ";", i) //add submenu to menucat and separate the data by finding ";" mark end if me.Cell (i-1,0) = NthField (menucat, ",", i) //add menucat items to Listbox next me.ColumnCount = 1 |
As you probably can see our menu is now available in Listbox1 but it can’t expand. To get this effect done we must go to Listbox1 Sub ExpendRow () and make some fixes. Simply copy and paste this code there:
1 2 3 4 5 6 7 8 9 10 | Dim menucat As String Dim i As Integer menucat = me.Cell (row,1) //show or hide the submenu items for i = 1 to CountFields (menucat, ",") me.AddRow "" me.Cell(me.LastIndex,0) = NthField (menucat, ",", i) next |
If you see this message you probably want access special material for Premium Members (Source Files, Bonus Tutorials, free Classes and More). You can do this by subscribing REALbasic City site for 10 USD (access for 3 months). There is also a option to buy access only to this material. Login or register to get option to buy and support REALbasic City.
Log in
If you like to know how to add some icons to Listbox, change the background or selection color please go to my iTunes like sidebar tutorial: http://realbasic.tutspolis.com/tutorials/creating-itunes-like-sidebar-for-realbasic/ and learn this and even more
Thanks Jakub. That’s what I was waiting for
However, may I kindly ask to add some more detailed comments or steps inbetween in order to make it more understandable than just trying to figure out (for me) how this could work and what the scheme behind that is…
Yes, I am really a beginner
Alex
Ok, I will add more comments today but later.
As a note, the greater than/less than signs are missing in the following line:
if NthField (submenu, “;”, i) “” then
Thank you Bill, I corrected this bug
I will add more comments to code soon…
OK, I add more comments to the code. If you still don’t know what is going there please feel free to ask.
Hi Jakub, very good job
I had analized your job and I like it, but I want know, how can I do the same but for tree or more levels? Thanks!
Hello pedrojo,
you must use AddFolder to get this kind of effect