【问题标题】:How to calculate and change treeview width如何计算和更改树视图宽度
【发布时间】:2012-07-11 09:46:44
【问题描述】:

如何让TreeView在展开节点时改变它的宽度,使节点的标签完全显示出来。

首先我设置DrawMode = OwnerDrawAll;

然后处理事件DrawNodeandin handler

e.DrawDefault = true;
currentWith_ = Math.Max(currentWith_, e.Node.Bounds.Right);

然后在AfterExpand 中设置控件。但不是每次都有效。有时 with 没有更改或更改不正确。

如何解决这个问题。 提前致谢。

【问题讨论】:

  • 你试过什么?

标签: c# winforms user-interface treeview


【解决方案1】:

试试这个,成功了:

private void treeViewAfterExpand(object sender, TreeViewEventArgs e)
{
    int maxRight = treeView.ClientSize.Width;

    if(e.Node.Nodes != null)
        foreach (TreeNode node in e.Node.Nodes)
        {
            maxRight = Math.Max(maxRight, node.Bounds.Right);
        }

    treeView.ClientSize = new Size(maxRight, treeView.ClientSize.Height);
}

【讨论】:

  • treeView.BoundstreeView.ClientSize 添加的边框等。
  • 别工作!!!!只有集合中的第一个节点需要设置 Bounds 属性,其他值设置为 0。
  • 树中的每个节点都有bounds-property。您只需将树视图的大小设置为最大绑定宽度。
  • 你试过了吗?它们具有 Bounds 属性,但其属性(宽度、高度、...)设置为 0!
【解决方案2】:

Ria 给出的解决方案有效,但在构造函数中展开时无效。扩展加载事件而不是构造函数使其工作。 (无法评论,因为低于 50 分。)

【讨论】:

  • 解决方案在OnLoad 构造函数中也不起作用。有错误或问题吗?有解决办法吗?
猜你喜欢
  • 1970-01-01
  • 2013-06-09
  • 1970-01-01
  • 2017-07-23
  • 1970-01-01
  • 2011-03-28
  • 2015-09-07
  • 1970-01-01
  • 2020-07-15
相关资源
最近更新 更多