Solved

Treeview scroll options

Posted on 2002-05-20
7
332 Views
Last Modified: 2006-11-17
I would like to create two command buttons: one that scrolls a treeview to the top for viewing and the other that scrolls the treeview to the bottom.  How do I do this?  Please provide me with the code.

Thanks
0
Comment
Question by:perkster
  • 3
  • 3
7 Comments
 
LVL 4

Expert Comment

by:gencross
ID: 7022185
Here is a very simple way to do this.  I have only tested it with 50 nodes so I don't know how fast it will be if there are many nodes.  There may also be a better way to do this.  I will keep checking.

Private Sub Command1_Click()
   
    'Scroll bottom
    TreeView1.Nodes(1).Selected = True
    TreeView1.Nodes(TreeView1.Nodes.Count).Selected = True
   
End Sub

Private Sub Command2_Click()
   
    'Scroll top
    TreeView1.Nodes(TreeView1.Nodes.Count).Selected = True
    TreeView1.Nodes(1).Selected = True
   
End Sub
0
 
LVL 4

Accepted Solution

by:
gencross earned 50 total points
ID: 7022195
Here is a better way to do this.  Hope this helps.

Private Sub Command1_Click()
   
   'Scroll bottom
   TreeView1.Nodes(TreeView1.Nodes.Count).EnsureVisible  
 
End Sub

Private Sub Command2_Click()
   
   'Scroll top
   TreeView1.Nodes(1).EnsureVisible      

End Sub
0
 
LVL 2

Expert Comment

by:Crin
ID: 7022335
Hi,

Yes, you surely will use Select or EnsureVisible methods, but the algo gencross supposed is not good, since nodes indices are not guaranteed to be from top to bottom, but in order of their load.

So, you should use FirstSibling, LastSibling, Expanded and some other properties of the treeview control.

Also, it is quite difficult to scroll line by line from CURRENT position, since there is no "IsVisible" property for the node object.

By the way, why not to use standard treeview scrollbars?

Sincerely,
Crin
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 4

Expert Comment

by:gencross
ID: 7022480
I'm not sure, but I believe the highest index of a control (treeview, combobox, listbox, collection, etc...) is going to be assigned to the last item (node).  You cannot, for example, assign index 10 to the first item in the treeview.  You can assign a key of "10" to the item, but the index should always start at 0 or 1 (1 in the case of the treeview) and the last item should have the highest index.  Not necessary the highest key, but that doesn't matter because key and index are two different things.  Please let me know if I am wrong.
0
 
LVL 2

Expert Comment

by:Crin
ID: 7022494
Hi,

Simple test will show indices applying.
Open new project, add MS Windows Common Controls, place treeview on the form, paste the code and run the form.

The code is obvious, so you will simple see what is node  index for particular node.

Private Sub Form_Load()

Dim nd As Node


Me.TreeView1.Nodes.Add , , "a", "a"
Me.TreeView1.Nodes.Add , , "b", "b"

Me.TreeView1.Nodes.Add "a", tvwChild, "a1", "a1"
Me.TreeView1.Nodes.Add "a", tvwChild, "a2", "a2"
Me.TreeView1.Nodes.Add "b", tvwChild, "b1", "b1"
Me.TreeView1.Nodes.Add "a", tvwChild, "a3", "a3"

For Each nd In Me.TreeView1.Nodes
  nd.Text = nd.Index
Next

End Sub


Sincerely,
Crin

0
 
LVL 2

Expert Comment

by:Crin
ID: 7022516
By the way - numeric key values are prohibited for the Node object as well as for ListItem object, so you can not assign "10" value to particular node key value, in spite of the fact key property is string.

Also, Index property is read-only for Node (and ListItem) and it achieve value while Add method execution for Nodes collection. So, you also cannot operate with it after node is loaded except to *get* its value.

Sincerely,
Crin
0
 

Author Comment

by:perkster
ID: 7023111
Perfect.  Thanks all.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now