Solved

Treeview scroll options

Posted on 2002-05-20
7
355 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

772 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