Forum  General Visual ...  Visual WebGui v...  6.4d (and 6.4c) TreeNode.EnsureVisible does not work
Previous Previous
 
Next Next
New Post 4/21/2012 11:47 AM
Unresolved
  glomax
107 posts
5th Level Poster






6.4d (and 6.4c) TreeNode.EnsureVisible does not work 

Sent to Support@VisualWebGui.com ...

Attached is a sample application that contains a simple docked TreeView that scans “Program Files” and loads all files in a TreeView. 

When the VS2010 devenv.exe is found that node is expanded and shown using Node.EnsureVisible.
 
In the WinForm version the treeview scrolls so that the node is visible.
 
In the Visual WebGUI version, the treeview does not scroll.
 
 

Here's the sample code for reference too...

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            string[] Files = System.IO.Directory.GetFiles("C:\\Program Files""*.*", System.IO.SearchOption.AllDirectories);
            foreach (string File in Files)
            {
                List<string> FileParts = new List<string>(File.Split('\\'));
                AddTreeNode(treeView1.Nodes, FileParts);
            }
            treeView1.SelectedNode.EnsureVisible();
        }
 
 
        private void AddTreeNode(TreeNodeCollection Nodes, List<string> FileParts)
        {
            TreeNode ExistingNode = GetExistingNode(Nodes, FileParts[0]);
            if (ExistingNode == null)
            {
                ExistingNode = new TreeNode(FileParts[0]);
                Nodes.Add(ExistingNode);
            }
            if (FileParts.Count > 1)
            {
                FileParts.RemoveAt(0);
                AddTreeNode(ExistingNode.Nodes, FileParts);
            }
            else
            {
                Debug.WriteLine(ExistingNode.FullPath);
                if (ExistingNode.FullPath == @"C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe")
                {
                    treeView1.SelectedNode = ExistingNode;
                    //while (ExistingNode != null)
                    //{
                    //    ExistingNode.Expand();
                    //    ExistingNode = ExistingNode.Parent;
                    //}
                }
            }
        }
 
 
        private TreeNode GetExistingNode(TreeNodeCollection Nodes, string FileName)
        {
            foreach (TreeNode Node in Nodes)
            {
                if (Node.Text == FileName)
                {
                    return Node;
                }
            }
            return null;
        }
 
 
    }

Greg Lomax - Gizmox MVP
 
New Post 4/21/2012 2:55 PM
  glomax
107 posts
5th Level Poster






Re: 6.4d (and 6.4c) TreeNode.EnsureVisible does not work 

In case this could help anyone, here's my workaround...

    public class TreeViewEx : TreeView
    {
 
        public void EnsureNodeVisible(TreeNode Node)
        {
            Node.EnsureVisible();
            Graphics g = GraphicsHelper.CreateMeasurementGraphics();
            int Position = 0;
            while (Node.PrevVisibleNode != null)
            {
                Font NodeFont = (Node.NodeFont == null ? this.Font : Node.NodeFont);
                int NodeHeight = Convert.ToInt32(g.MeasureString(Node.Text, NodeFont).Height) + 2;
                Position += NodeHeight;
                Node = Node.PrevVisibleNode;
            }
            base.ScrollTop = Math.Max(0, Position - (base.Height / 2));
        }
 
 
    }

 


Greg Lomax - Gizmox MVP
 
New Post 4/21/2012 4:57 PM
  palli
14295 posts
1st Level Poster




Re: 6.4d (and 6.4c) TreeNode.EnsureVisible does not work 
Modified By palli  on 4/21/2012 7:57:58 PM)

Hi Greg,

Nice thought on your workaround and thanks for the sample application.

This problem is already reported in an open tracker entry here. It has a workaround suggestion that you may want to apply to your application. The unerlying problem with EnsureVisible that by the time it is called, the node you want scrolled to is not expanded in the TreeView and therefore it is not registered yet. The workaround is to create a helper class and make sure that every TreeNode added is registered right from the start.

Add something like the following:

public class RegisteringTreeNode : TreeNode
{
    public RegisteringTreeNode(string strLabel) : base(strLabel)
    {
        if (!this.IsRegistered)
            this.RegisterSelf();
    }
}

And then instead of adding an instance of a TreeNode, use your new helper class and the EnsureVisible should work fine.

Palli

 


Páll Björnsson - Visual WebGui support team - Email: support@visualwebgui.com
 
New Post 4/21/2012 5:27 PM
  glomax
107 posts
5th Level Poster






Re: 6.4d (and 6.4c) TreeNode.EnsureVisible does not work 

Thanks.  I'll keep an eye on the tracker item.


Greg Lomax - Gizmox MVP
 
New Post 4/22/2012 5:34 AM
  palli
14295 posts
1st Level Poster




Re: 6.4d (and 6.4c) TreeNode.EnsureVisible does not work 

Hi Greg,

You are most welcome,

Palli

 


Páll Björnsson - Visual WebGui support team - Email: support@visualwebgui.com
 
Previous Previous
 
Next Next
  Forum  General Visual ...  Visual WebGui v...  6.4d (and 6.4c) TreeNode.EnsureVisible does not work
.NET HTML5 Web, Cloud and Mobile application delivery | Sitemap | Terms of Use | Privacy Statement | Copyright © 2005-2012 Visual WebGui®       Visual WebGui weblog on ASP.NET Gizmox Blog Visual WebGui Group on LinkedIn Visual WebGui updates on Twitter Visual WebGui Page on Facebook Visual WebGui YouTube Channel Visual WebGui Platform News RSS