Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 粗体treeview节点被截断-由于构造函数中的代码,官方修复程序将不起作用_C#_Winforms_Treeview - Fatal编程技术网

C# 粗体treeview节点被截断-由于构造函数中的代码,官方修复程序将不起作用

C# 粗体treeview节点被截断-由于构造函数中的代码,官方修复程序将不起作用,c#,winforms,treeview,C#,Winforms,Treeview,我遇到了一个众所周知的问题,将TreeNode的字体设置为粗体后,TreeNode的文本会被截断。然而,我相信我发现了一种情况,即所有普遍接受的“修复”都无法奏效 通用解决方案: 变体1:(见布伦特的答案) 变体2: 上述修复不起作用的场景: 如果将节点设置为粗体的代码位于构造函数中(表单中或在本例中为用户控件),则修复程序将不起作用: public partial class IncidentPlanAssociations : UserControl { public Inciden

我遇到了一个众所周知的问题,将TreeNode的字体设置为粗体后,TreeNode的文本会被截断。然而,我相信我发现了一种情况,即所有普遍接受的“修复”都无法奏效

通用解决方案:

变体1:(见布伦特的答案)

变体2:

上述修复不起作用的场景:

如果将节点设置为粗体的代码位于构造函数中(表单中或在本例中为用户控件),则修复程序将不起作用:

public partial class IncidentPlanAssociations : UserControl
{
    public IncidentPlanAssociations()
    {
        InitializeComponent();

        TreeNode node = new TreeNode("This is a problem.");
        node.NodeFont = new Font(treeView1.Font, FontStyle.Bold);
        treeView1.Nodes.Add(node);

        // This does not fix problem
        node.Text += string.Empty;

        // This does not fix problem
        node.Text = node.Text;

        // This does not fix problem
        treeView1.BackColor = treeView1.BackColor;

    }
}

然而,如果我将这三个“修复”中的任何一个放在一个按钮后面的代码中,并在一切运行后单击它,它就会正常工作。我确信这与最初绘制树状视图或其他东西有关,但我正试图找到一种好的方法来解决这个问题。有什么建议吗?

谢谢@Robert Harvey的帮助

如果有人遇到类似的问题,这里的解决方法是将代码从构造函数移动到用户控件的加载事件。上述三种变体中的任何一种都可以工作

我个人选择了:

private void IncidentPlanAssociations_Load(object sender, EventArgs e)
{
    TreeNode node = new TreeNode("This is no longer a problem.");
    node.NodeFont = new Font(treeView1.Font, FontStyle.Bold);
    treeView1.Nodes.Add(node);

    // This fixes the problem, now that the code 
    //      is in the Load event and not the constructor
    node.Text = node.Text;

}

代码只需要在Load事件中,而不在构造函数中,就可以解决已知错误。如果将要使用的最大字体(粗体和/或最大点大小)指定给基本TreeView控件,然后显式地将标准大小的行的字体更改为较小的字体,就不会有截断问题

此外,如果选择与标准字体距离不太远的最大字体,则所有内容的间距都会很小。

在任何视觉更改中使用treeView.BeginUpdate()和treeView.EndUpdate()

例如:

this.treeView.BeginUpdate();
this.treeView.Nodes.Clear();
this.treeView.Nodes.AddRange(allNodes.ToArray());
this.treeView.EndUpdate();

我知道这是一个非常古老的帖子,但我刚刚找到了一个适合我的解决方案。显然,通过将back color设置为空,它解决了粗体文本被截断的问题

以下是修复方法:

treeView.BackColor = Color.Empty;

我已经编辑了你的标题。请参阅“”,其中一致意见是“不,他们不应该”。请尝试调用
treeview1.Refresh()
,作为构造函数中的最后一行代码。@RobertHarvey,谢谢你的建议。对Refresh()的调用没有什么不同。对。尝试将代码放入用户控件的
Load
事件中,而不是构造函数中。好的!!好电话。通过将代码移动到Load事件,bug解决方案现在可以完美地工作。如果你想用答案来回应,我很乐意标记为答案。似乎也需要在你将节点添加到treeView控件后进行。(??)
private void IncidentPlanAssociations_Load(object sender, EventArgs e)
{
    TreeNode node = new TreeNode("This is no longer a problem.");
    node.NodeFont = new Font(treeView1.Font, FontStyle.Bold);
    treeView1.Nodes.Add(node);

    // This fixes the problem, now that the code 
    //      is in the Load event and not the constructor
    node.Text = node.Text;

}
this.treeView.BeginUpdate();
this.treeView.Nodes.Clear();
this.treeView.Nodes.AddRange(allNodes.ToArray());
this.treeView.EndUpdate();
treeView.BackColor = Color.Empty;