New topic: Hierarchical Listbox problem - remember expanded...?
<http://forums.realsoftware.com/viewtopic.php?t=30576> Page 1 of 1 [ 10 posts ] Previous topic | Next topic Author Message Wolfsnap Post subject: Hierarchical Listbox problem - remember expanded...?Posted: Tue Oct 20, 2009 8:17 pm Joined: Thu May 10, 2007 10:25 pm Posts: 23 To start with - my apologies for not knowing how to correctly display code or URLs - any help would be appreciated. I know this has probably already been explained somewhere in the forum, but I haven't been able to find it in the past three days. My question is: How can I store and recall the "expanded" property of an hierarchical listbox? I have an hierarchical listbox that displays 5 levels - Root, Book, Chapter, Category, Recipe The "Root" level I am creating in code (In the "Open" event - it's either "Cookbooks" or "Favorites") - let's leave "Favorites" alone for now (if I can get "Cookbooks" figured out, "favorites" shouldn't be a problem......yeah, right) When I expand "Cookbooks", I am reading into a database of BookTitles - easy enough. I SQL all Books from my "Cookbooks" table. Within that table, I have a field for the bookpath and for the property of expanded (IsExpanded - true or false) When I expand "Cookbooks", all of the BookTitles are gathered, and another SQL query looks at a "Chapters" table to determine whether the book has any chapters associated with it - if it does, it creates a folder for that book title, if not, it creates a row for that book title. All is well up until now... Once I add a Chapter, I need to check if there are any Categories associated with that chapter, and then on down to recipes associated with that category, etc. The "Chapter" table has a "ChapterPath" field which is the path for that chapter and book, likewise, the "CategoryPath" has the path for that category, chapter and book. Each table has an "IsExpanded" field - true or false - used to determine if the cell of the listbox should be expanded or not. The same for the child tables (categories, recipes) (Deep breath) I have the following code in the ExpandRow event of the listbox class, but it only displays that the "BookTitle" row as expanded (via the triangle) - but it doesn't populate the "Chapters".....? [code][ dim RowsAdded as Integer = 0 //<--------Keeps count of the rows added based on currently selected row dim f as string //<------------------This is the classifying string i.e. Root, BookTitle, Chapter, etc. dim f2 as String //<-----------------This is the path of the selected cell stored in the path field of the table dim GetBooks, ChapterCount, GetChapters, CategoryCount, GetCategories, RecipeCount, GetRecipes as RecordSet f=str(cell(Row,1)) //<----------------Sets the initial classifying string f2=str(cell(Row, 2)) //<--------------Sets the initial path Select Case f Case "Root" //---------------------------------Start Root, expand to display each book-------------------------------------------------- // Get All Cookbooks from the Database GetBooks = App.RecipeDB.SQLSelect("SELECT BookName, BookPath, IsExpanded FROM Cookbooks ORDER BY BookName") if app.RecipeDB.error then app.displayDatabaseError false return end // Reads through each book found and determines if each book has any chapters - if so, add them if the book is expanded (or add the book as a folder if not expanded) while GetBooks.EOF = false //Checks if there are any sub-files - makes this a folder or a row ChapterCount = App.RecipeDB.SQLSelect("SELECT count(*) FROM Chapters WHERE BookName = '" + GetBooks.Field("BookName").StringValue + "'") if ChapterCount.idxField( 1 ).integerValue > 0 then// record exists if the count is greater than 0 AddFolder " " + GetBooks.Field("BookName").StringValue RowsAdded=RowsAdded+1 //put the path in a non-visible column cell(row+RowsAdded,1)="BookTitle" f=str(cell(Row,1)) cell(row+RowsAdded,2)=GetBooks.Field("BookPath").StringValue f2 = GetBooks.Field("BookPath").StringValue cell(row+RowsAdded,3)=GetBooks.Field("IsExpanded").StringValue Expanded(row+RowsAdded) = GetBooks.Field("IsExpanded").BooleanValue //Checks to see if this folder is expanded or not //------------------------------------Start Chapters within expanded book------------------------------------------------------------------ Select Case f Case "BookTitle" // Get All Chapters within each book and add the folders or rows for each GetChapters = App.RecipeDB.SQLSelect("SELECT ChapterName, ChapterPath, IsExpanded FROM Chapters WHERE BookPath = '" + EliminateSpaces5(f2) + "'") if app.RecipeDB.error then app.displayDatabaseError false return end if GetChapters.idxField( 1 ).integerValue > 0 then //<---------The book has chapters assiciated with it // Add all of the Chapters that were returned while GetChapters.EOF = false //Checks if there are any sub-files - makes this a folder or a row CategoryCount = App.RecipeDB.SQLSelect("SELECT count(*) FROM Categories WHERE ChapterPath = '" + EliminateSpaces5(f2) + "/" + GetBooks.Field("ChapterName").StringValue + "'") if CategoryCount.idxField( 1 ).integerValue > 0 then //<---------The chapter has categories associated with it AddFolder " " + GetChapters.Field("ChapterName").StringValue RowsAdded=RowsAdded+1 //put the path in a non-visible column cell(row+RowsAdded,1)="Chapter" f=str(cell(Row,1)) cell(row+RowsAdded,2)=GetChapters.Field("ChapterPath").StringValue f2=GetChapters.Field("ChapterPath").StringValue cell(row+RowsAdded,3)=GetChapters.Field("IsExpanded").StringValue Expanded(row+RowsAdded) = GetChapters.Field("IsExpanded").BooleanValue GetChapters.MoveNext else //<---------------The chapter doesn't have any categories associated with it AddRow " " + GetChapters.Field("ChapterName").StringValue RowsAdded=RowsAdded+1 //put the path in a non-visible column cell(row+RowsAdded,1)="Chapter" f=str(cell(Row,1)) cell(row+RowsAdded,2)=GetChapters.Field("ChapterPath").StringValue f2=GetChapters.Field("ChapterPath").StringValue cell(row+RowsAdded,3)=GetChapters.Field("IsExpanded").StringValue GetChapters.MoveNext end if f="BookTitle" wend //<---------------We've reached the end of the chapters for this book----------------------------- end if end Select f="Root" GetBooks.MoveNext //<--------Checks the next book else //<-----------------------The book doesn't have any chapters associated with it AddRow " " + GetBooks.Field("BookName").StringValue RowsAdded=RowsAdded+1 //put the path in a non-visible column cell(row+RowsAdded,1)="BookTitle" f=str(cell(Row,1)) cell(row+RowsAdded,2)=GetBooks.Field("BookPath").StringValue f2=GetBooks.Field("BookPath").StringValue cell(row+RowsAdded,3)=GetBooks.Field("IsExpanded").StringValue GetBooks.MoveNext end if wend GetBooks.close //----------------------------------------------End of Root--------------------------------------------------- end Select ] OK - stop laughing - I know it's sloppy as can be - and doesn't really tell the whole story. If anyone's interested, the full application code is here: [url][http://www.moonlightaviation.com/RecipeCode/] This is an early work in progress, so there are A LOT of commented out lines - sorry Top Wolfsnap Post subject: Re: Hierarchical Listbox problem - remember expanded...?Posted: Tue Oct 20, 2009 8:25 pm Joined: Thu May 10, 2007 10:25 pm Posts: 23 Oops - forgot to mention my version of RB and operating system. RB - Version RB 2009 Release 2 OS - Mac 17" MacBook Pro Top timhare Post subject: Re: Hierarchical Listbox problem - remember expanded...?Posted: Tue Oct 20, 2009 10:26 pm Joined: Fri Jan 06, 2006 3:21 pm Posts: 6588 Location: Portland, OR USA Setting expanded from within the ExpandRow event does work. I have done it before. It may be that your problem is in storing and retrieving a boolean value from the database. I would focus on that and make sure you're getting the value back corrrectly. Tim Top Wolfsnap Post subject: Re: Hierarchical Listbox problem - remember expanded...?Posted: Tue Oct 20, 2009 11:44 pm Joined: Thu May 10, 2007 10:25 pm Posts: 23 Evidently, the "isExpanded" field of the table is being read and payed attention to (the triangles are pointed downward when the field is "true", and pointed sideways when the field is "False". I'm using the "expandrow" and the "CollapseRow" events to update the datqabase with these values - and they're being read back in - but the sub-categories aren't being displayed...? Top timhare Post subject: Re: Hierarchical Listbox problem - remember expanded...?Posted: Wed Oct 21, 2009 12:26 am Joined: Fri Jan 06, 2006 3:21 pm Posts: 6588 Location: Portland, OR USA Your code is hard to read in the format you posted, but it appears that the entire set of code is enclosed in Code:select case f case "Root" ... end Therefore, it will only execute at the root level. You have additional select case statements within "Root". They should probably be at the same level of select case as "Root" Code:select case f case "Root" // Get all Cookbooks ... case "BookTitle" // Get all Chapters ... end As you set me.Expanded = True, you get a cascade of ExpandRow events, one for each row you are expanding. So in the first ExpandRow, you get a Row value that points to "Root". As you add a folders and expand it, you get another ExpandRow event, this one with a Row value that points to the newly added and expanded "BookTitle". And so on. In any instance of ExpandRow, it will either be a "Root" or "BookTitle" or "Chapter", etc., but only one of them. Tim Top Wolfsnap Post subject: Re: Hierarchical Listbox problem - remember expanded...?Posted: Wed Oct 21, 2009 1:22 am Joined: Thu May 10, 2007 10:25 pm Posts: 23 Tim - My apologies for not being very proficient with this particular forum's abilities - first time posting here. The code posted does only apply to the "Root" level (for now) - meaning, if I expand the "Root" (Cookbooks), at this point, I would be happy to see subsequent folders expand (with their sub-categories, if they exist) depending on the "IsExpanded" field of the database "Cookbooks". Once I get the initial expansion to act correctly, then I plan to expand tha case select statement to include "BookTitle", "Chapter", Category" Looking at the point you make, I have to wonder if the entire code (based on the initial folder having a "Root" level) continues on through the subsequent levels...? i.e., if I expand a sub-category based on a "Root" level that the code recognizes, should sub-categories that have an "IsExpanded" property loaded from the data table be read and carried out...? ...I'm just confusing myself at this point..........???? Top Wolfsnap Post subject: Re: Hierarchical Listbox problem - remember expanded...?Posted: Wed Oct 21, 2009 1:47 am Joined: Thu May 10, 2007 10:25 pm Posts: 23 Tim - i really appreciate your help - and again, my apologies for my lack of communications skills within this forum. You stated: "As you set me.Expanded = True, you get a cascade of ExpandRow events, one for each row you are expanding. So in the first ExpandRow, you get a Row value that points to "Root". As you add a folders and expand it, you get another ExpandRow event, this one with a Row value that points to the newly added and expanded "BookTitle". And so on. In any instance of ExpandRow, it will either be a "Root" or "BookTitle" or "Chapter", etc., but only one of them." If I understand what you're saying - subsequent code doesn't perform because I've changed the Row value from "Root" to "BookTitle" - and because I'm looking for the case of "Root", I'm not getting the expansion for row values of "BookTitle". Would the solution be to put another collection of case statements within the cascade? - OR - simplify the code to eliminate the Case statements altogether (The only reason I'm clinging on to the case statements is that I'm using them to determine the graphic drawn into each cell - maybe I need to take the callpaint event to look at the path within the data table...? I know this all sounds incoherent - I think I'm probably overthinking the situation Marc Top Wolfsnap Post subject: Re: Hierarchical Listbox problem - remember expanded...?Posted: Wed Oct 21, 2009 1:55 am Joined: Thu May 10, 2007 10:25 pm Posts: 23 Let's possibly simplify my problem - by explaining how the expanded property works: If i expand a row, subsequent folders and rows appear, right? If I expand a row and, through code, determine that a subsequent row is to be expanded -- does that simply expand that row and then end the current ExpandRow event, or does the "Expanded = True" re-fire the ExpandRow event, which would create an endless loop...? Signed - a confused New-B Top timhare Post subject: Re: Hierarchical Listbox problem - remember expanded...?Posted: Wed Oct 21, 2009 2:16 am Joined: Fri Jan 06, 2006 3:21 pm Posts: 6588 Location: Portland, OR USA When the user clicks on the expand widget, you get an ExpandRow event that passes you a row number and says, expand this. Your ExpandRow code then examines the row and determines what "expand" means for that row. Normally what would display is one additional level of the hierarchy - the one immediately below the row the user clicked on. If you set Expanded = True in code, the exact same thing happens. Now, if you are setting Expanded in code inside the ExpandRow event, it's as if you expanded the one row and the user immediately started clicking more rows. So, yes, you will get a bunch of additional ExpandRow events firing, one for each row you set Expanded on. In your ExpandRow event, you need to handle a single row. Don't worry about an additional levels of hierarchy, they'll get their own ExpandRow events. The easiest way to do that given your setup would be a big case statement like I suggested at the end of my previous post. For any given ExpandRow, only one section of that case statement will execute. If you set the Expanded state of each row as you load it, then you will get a bunch of ExpandRow events firing, and you will get several levels of hierarchy displayed all at once. That seems like a reasonable thing to want to achieve - setting the state back to the way it was when the user last saw it. One thing to be aware of - if you do succeed in getting multiple levels expanded, your RowsAdded offsets will not be valid. It's much better to use LastIndex instead of Row+RowsAdded. LastIndex is the index of the row you just added. It will be valid between AddFolder and Expanded=True. Don't try to use it after Expanded=True, as that will add additional rows. HTH, Tim Top Wolfsnap Post subject: Re: Hierarchical Listbox problem - remember expanded...?Posted: Wed Oct 21, 2009 2:48 am Joined: Thu May 10, 2007 10:25 pm Posts: 23 Tim, You've given me a whole new way to approach this - I'm going to try them all out (tomorrow - or - later today...getting clodse to my bedtime now, 4A.M or so) Thank you for your insight and suggestions! I'll let you know how it goes! Marc Top Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending Page 1 of 1 [ 10 posts ] -- Over 1500 classes with 29000 functions in one REALbasic plug-in collection. The Monkeybread Software Realbasic Plugin v9.3. http://www.monkeybreadsoftware.de/realbasic/plugins.shtml [email protected]
