Click here to Skip to main content
Licence 
First Posted 5 Apr 2005
Views 56,012
Bookmarked 28 times

TreeView without right scrolling

By | 17 Apr 2005 | Article
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

Member

Check out Lookeen Search for Outlook

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionDoes this work with VB.NET? PinsussAmitWS7:45 13 Apr '05  
AnswerRe: Does this work with VB.NET? PinsussAmitWS8:07 13 Apr '05  
GeneralGood stuff PinmemberMikael Wiberg3:04 7 Apr '05  
GeneralRe: Good stuff PinmemberMW2322:51 7 Apr '05  
GeneralRe: Good stuff PinmemberMikael Wiberg23:35 7 Apr '05  
GeneralInteresting. What about... PinmemberJudah Himango5:49 5 Apr '05  
GeneralRe: Interesting. What about... PinmemberMW239:46 5 Apr '05  
GeneralRe: Interesting. What about... PinmemberJudah Himango10:06 5 Apr '05  

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.

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