Hi
i want to avoid horizontal scroll in the tree, from many posts in the form i understand that there is no some sort of "autosize" property
i found this at on few of the posts
at Expand Event :
ultraTree1.Width = ultraTree1.Width + (e.TreeNode.TextWidth/2);
it doesn't make much sense to me , adding the TextWidth/2 of the clicked node, what if his child node has a long name, it will not work. this is some sort of average that may work generically, i dont want to speak for everyone but for me it just does not work well. it either sets the width too much too less.
i was thinking of changing the width of tree control after each expand incase there is" horizontal ScollBar Scrolling availability", meaning something like
while (ultraTree1.HorizontalScrollBar == true) // or ultraTree1.Scrollbars == Scrollbars.Vertical;
{
ultraTree1.Width ++;
ultraTree1.Refresh();
}
however i have not found the appropriate property to look at. also i couldn't find something like ScrollPosition for horizontal. for vertical i read that it can be set using the TopNode property, but i need the horizontal.
can this be done?
Hello,
Taking a quick look at the UltraTree's API, I don't see anything exposed that would help you out in this regard without looping. However, we do have this information available internally and with a little reflection, you can gain access to it.
Try calling the following method in your AfterExpand and AfterCollapse event handlers:
private void AutoSizeTree()
UIElement controlUIElement = this.ultraTree1.UIElement;
controlUIElement.VerifyChildElements();
// if there is a vertical scrollbar, gets its width from the system
int reservedSpace = (controlUIElement.GetDescendant(typeof(ScrollBarUIElement), Orientation.Vertical) != null) ? SystemInformation.VerticalScrollBarWidth : 0;
reservedSpace += 2;
BindingFlags flags = BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic;
Type treeType = typeof(UltraTree);
PropertyInfo field = treeType.GetProperty("RightMostNodePosition", flags);
int rightMostNodePosition = (int)field.GetValue(this.ultraTree1, null);
this.ultraTree1.Width = Math.Max(this.minTreeWidth, rightMostNodePosition + reservedSpace);
With this method, there is also a local member called 'minTreeWidth' which is set in the form's constructor to the Width of the UltraTree. This is used to make sure the control never gets smaller than than the initial width.
Hopefully this helps. Let me know if you require any further assistance.
Thanks,
Chris
out of this world!
this is exactly what i needed, works great !
in my case i had the ultratree as a DropDownEditorButton therefore i've perform the Math.Max on the dropDownEditorButton.Control.Width instead of the UltraTree.Width
and at the Expand event i've done
AutoSizeTree();
dropDownEditorButton.CloseUp();dropDownEditorButton.DropDown();
because the dropDownEditorButton.Control.Refresh() or UltraTree1.Refresh() didn't do it.this does cause a CloseUp-DropDown flicker but so be it.....
thanks alot Chris !
your right, silly me....
Thanks Chris, this works like a charm !
The ResizeDropDown() call is not working because you are passing the wrong control for the Control parameter. In your case, the Control would be the editor control that is displaying the DropDownEditorButton (i.e. UltraTextEditor, UltraDateTimeEditor). The Refresh() calls should not be necessary.
To autosize based on height, you'll need to adjust the height based on the last visible node. The UltraTree has an internal LastVisibleNode property that you can utilize (again, via reflection).
Here are my AutoSizeTree() and ResizeDropDown() methods. Note that the ResizeDropDown() is called from AutoSizeTree() so my AfterExpand and AfterCollapse event handlers simply call AutoSizeTree().
// calculate the new width
int newWidth = Math.Max(this.originalSize.Width, rightMostNodePosition + reservedSpace);
// calculate the new heigth
field = treeType.GetProperty("LastVisibleNode", flags);
UltraTreeNode lastNode = field.GetValue(this.ultraTree1, null) as UltraTreeNode;
reservedSpace = lastNode.Bounds.Height;
int newHeight = Math.Max(this.originalSize.Height, lastNode.Bounds.Bottom + reservedSpace);
this.ultraTree1.Size = new Size(newWidth, newHeight);
this.ResizeDropDown(this.ultraTextEditor1, this.ultraTree1);
private void ResizeDropDown(Control owner, Control dropControl)
DropDownManager.ResetDropDownPositionAndSize(
owner,
new Rectangle(DropDownManager.GetOriginalRequestedScreenRect(owner).Location, dropControl.PreferredSize),
DropDownManager.GetExclusionScreenRect(owner),
true);
Let me know if you have any other issues
Chris, thank you for the reply :)
i've tried using your code "ResizeDropDown" as you suggested but it didn't do the trick...
what i did is :
1. i've commented the DropDown() and CloseUp();
2. added the method "ResizeDropDown" to the code
3. added the call to "ResizeDropDown" in the "AutoSizeTree" (that you kindly sent me) as follows :
private void AutoSizeTree() { ResizeDropDown(dropDownEditorButton.Control, ut); dropDownEditorButton.Control.Refresh(); uce.Refresh(); ut.Refresh();
UIElement controlUIElement = this.ut.UIElement;
reservedSpace += 5;
int rightMostNodePosition = (int)field.GetValue(this.ut, null); dropDownEditorButton.Control.Width = Math.Max(minTreeWidth, rightMostNodePosition + reservedSpace);
ResizeDropDown(dropDownEditorButton.Control, ut); dropDownEditorButton.Control.Refresh(); uce.Refresh(); ut.Refresh(); }
4. when it didn't work i added the dropDownEditorButton.Control.Refresh(); uce.Refresh(); ut.Refresh();
what am i doing wrong?
Chris, i am trying to do the same to the height, but i'm not sure even how to approach it ... can you shed some guidance ?
All the dropdown functionality for our controls is handled by the DropDownManager. In general, the size of the DropDownWindow is determined just before it is shown. However, you can tell the DropDownManager to adjust the size of its window while it is still open. This will eliminate the need to close and reopen the dropdown (causing the flickering).
Try adding the following method to your code. In your case, you would pass the dropDownEditorButton.Control for the owner and the tree as the dropControl.
-Chris