Click here to Skip to main content
Click here to Skip to main content

TreeView without right scrolling

, 17 Apr 2005
Rate this:
Please Sign up or sign in to vote.
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:

using System.Runtime.InteropServices;
// 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).

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.

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

About the Author

Martin Welker
CEO Axonic Informationssysteme GmbH, Germany
Germany Germany

Comments and Discussions

 
QuestionDoes this work with VB.NET? PinsussAmitWS13-Apr-05 7:45 
This is exactly what I need to do. However it doesn't seem to be working in my VB.NET app. Here's my code:

Const WM_HSCROLL As Integer = 276
Const SB_LEFT As Integer = 6
Private Declare Function SendMessage Lib "user32.dll" ( _
ByVal hwnd As IntPtr, _
ByVal wMsg As Integer, _
ByVal wParam As Integer, _
ByVal lParam As Integer) As Integer
 

and when calling it:

...
destNode.NextVisibleNode.EnsureVisible()
SendMessage(treWorkSpace.Handle, WM_HSCROLL, SB_LEFT, 0)
...

 
Any ideas? Thanks!
AnswerRe: Does this work with VB.NET? PinsussAmitWS13-Apr-05 8:07 
GeneralGood stuff PinmemberMikael Wiberg7-Apr-05 3:04 
GeneralRe: Good stuff PinmemberMW237-Apr-05 22:51 
GeneralRe: Good stuff PinmemberMikael Wiberg7-Apr-05 23:35 
GeneralInteresting. What about... PinmemberJudah Himango5-Apr-05 5:49 
GeneralRe: Interesting. What about... PinmemberMW235-Apr-05 9:46 
GeneralRe: Interesting. What about... PinmemberJudah Himango5-Apr-05 10:06 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 17 Apr 2005
Article Copyright 2005 by Martin Welker
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid