Solved

Treeview scroll options

Posted on 2002-05-20
7
344 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

930 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