Click here to Skip to main content
13,289,107 members (50,659 online)
Click here to Skip to main content
Add your own
alternative version


31 bookmarked
Posted 5 Apr 2005

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.


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;
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.. 
    // ..and afterwards we scroll to the left again!
        SendMessage(treeView1.Handle, WM_HSCROLL,SB_LEFT , 0);


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");
    // the former call - just override this call to stop scrolling the the right
    // node.EnsureVisible(); 
    // new call



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

You may also be interested in...


Comments and Discussions

QuestionDoes this work with VB.NET? Pin
AmitWS13-Apr-05 8:45
sussAmitWS13-Apr-05 8:45 
AnswerRe: Does this work with VB.NET? Pin
AmitWS13-Apr-05 9:07
sussAmitWS13-Apr-05 9:07 
GeneralGood stuff Pin
Mikael Wiberg7-Apr-05 4:04
memberMikael Wiberg7-Apr-05 4:04 
GeneralRe: Good stuff Pin
MW237-Apr-05 23:51
memberMW237-Apr-05 23:51 
GeneralRe: Good stuff Pin
Mikael Wiberg8-Apr-05 0:35
memberMikael Wiberg8-Apr-05 0:35 
GeneralInteresting. What about... Pin
Judah Himango5-Apr-05 6:49
memberJudah Himango5-Apr-05 6:49 
GeneralRe: Interesting. What about... Pin
MW235-Apr-05 10:46
memberMW235-Apr-05 10:46 
Thanks for your comment. It was not my intention to disable EVERY horizontal scrolling (but only during Drag & Drop). Therefore you must decide within the WndProc method if you are dragging or not.
GeneralRe: Interesting. What about... Pin
Judah Himango5-Apr-05 11:06
memberJudah Himango5-Apr-05 11:06 

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.

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