Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Treeview scroll options

Posted on 2002-05-20
7
360 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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 Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

791 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