Click here to Skip to main content
15,879,535 members
Articles / Programming Languages / C#

TreeView Without Right Scrolling

Rate me:
Please Sign up or sign in to vote.
4.76/5 (22 votes)
17 Apr 20051 min read 86K   31   8
This article shows how to stop right scrolling when using the EnsureVisible() method for TreeView controls.

Introduction

Since this article is only a small (but hopefully a useful) delta to other Treeview articles, there is no project to download - it consists of only a few lines of code, and therefore should be very easy to integrate this feature into your own solution.

There are situations where you need to expand the Treeview control's functionality. For example, you want to add Drag & Drop behaviour to your solution. To implement scrolling while dragging, many examples use the TreeNode.EnsureVisible method. Whenever this method is called, the control scrolls to the right end of the node by default. Depending on the length of the node’s text, this has - in my eyes - the negative effect that most of the tree content is no longer visible! This article is about stopping the control from scrolling right.

The Solution

The ‘key idea’ is to create a left scrolling method which is invoked right after the regular EnsureVisible method. Left scrolling is implemented by a SendMessage call. We need a couple of declarations for that:

C#
using System.Runtime.InteropServices;
C#
// Constants for the SendMessage() method.
private const int WM_HSCROLL = 276;
private const int SB_LEFT = 6;
[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hWnd, int wMsg, 
                                  int wParam, int lParam);

To keep it simple, we encapsulate the new feature in a new method (in all probability, we could override the TreeNode’s method as well).

C#
private void EnsureVisibleWithoutRightScrolling(TreeNode node)
{
    // we do the standard call.. 
        node.EnsureVisible();
    
    // ..and afterwards we scroll to the left again!
        SendMessage(treeView1.Handle, WM_HSCROLL,SB_LEFT , 0);
}

Usage

In your own solution, you only have to replace the EnsureVisible calls by the new call and you are done - no more right scrolling.

C#
private void button1_Click(object sender, System.EventArgs e)
{
    TreeNode node = new TreeNode("A very very very very large item");
    treeView1.Nodes.Add(node);
  
    // the former call - just override this call to stop scrolling the the right
    // node.EnsureVisible(); 
        
    // new call
    EnsureVisibleWithoutRightScrolling(node);
}

References

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.


Written By
CEO Axonic Informationssysteme GmbH, Germany
Germany Germany

Comments and Discussions

 
GeneralGood stuff Pin
Mikael Wiberg7-Apr-05 3:04
Mikael Wiberg7-Apr-05 3:04 
GeneralRe: Good stuff Pin
Martin Welker7-Apr-05 22:51
Martin Welker7-Apr-05 22:51 
GeneralRe: Good stuff Pin
Mikael Wiberg7-Apr-05 23:35
Mikael Wiberg7-Apr-05 23:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.