C# Winforms 粗体树视图节点不显示整个文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2272493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# Winforms bold treeview node doesn't show whole text
提问by Martijn
I'm using the following code to make my treenodes bold:
我正在使用以下代码使我的树节点加粗:
Font font = new Font(tvQuestionSequence.Font, FontStyle.Bold);
foreach (QuestionnaireBuilder_Category cat in categories)
{
TreeNode node = new TreeNode();
node.Text = cat.Description;
node.Name = cat.Id.ToString();
node.NodeFont = font;
tvQuestionSequence.Nodes.Add(node);
}
But the text of the bold nodes is not displayed correctly. The last letter(s) are not shown. How come? And how to solve this problem?
但是粗体节点的文字显示不正确。不显示最后一个字母。怎么来的?以及如何解决这个问题?
采纳答案by Martijn
I've found that this is a Windows issue. A workaround for this problem is this:
我发现这是一个 Windows 问题。此问题的解决方法是:
In the form constructor set the font of the treeview to bold. When adding nodes which must not be bold, change the font to regular:
在表单构造函数中,将树视图的字体设置为粗体。添加不能为粗体的节点时,将字体更改为常规:
// Constructor of your form
public Form()
{
InitializeComponent();
Font font = new Font(tvQuestionSequence.Font, FontStyle.Bold);
tvQuestionSequence.Font = font;
}
// Add regular nodes (not bold)
Font font = new Font(tvQuestionSequence.Font, FontStyle.Regular);
TreeNode treeNode = new TreeNode();
treeNode.Text = "Foo";
treeNode.NodeFont = font;
TreeNode parent = tvQuestionSequence.Nodes.Find("parent", true);
parent.Nodes.Add(treeNode);
回答by Adel Hazzah
This is a known Windows bug. The simple solution is just to append an extra space character at the end of your strings. The space character will not be visible, but it will increase the number of pixels needed to draw the string, so the entire string will be visible.
这是一个已知的 Windows 错误。简单的解决方案是在字符串的末尾附加一个额外的空格字符。空格字符将不可见,但它会增加绘制字符串所需的像素数,因此整个字符串都将可见。
回答by Miguel Mesquita Alfaiate
I found this Post when searching through the web because I am facing the exact same problem.
我在网上搜索时发现了这篇文章,因为我面临着完全相同的问题。
However, appending a white space to the end of the node was not an option, and I found an alternative way that seems to fix the issue.
但是,在节点末尾附加一个空格不是一种选择,我找到了一种似乎可以解决该问题的替代方法。
After setting my node font Bold, all I need to do is reset the node text with the same value.
设置节点字体粗体后,我需要做的就是使用相同的值重置节点文本。
Here is the Code Sample:
这是代码示例:
Font boldFont = new Font(treeview.Font, FontStyle.Bold);
node.NodeFont = boldFont;
node.Text = node.Text;
It seems that the node is redrawn after changing the text, which is exactly what I wanted in the first place.
好像是修改了文本后重新绘制了节点,这正是我最初想要的。
回答by Kilus
Very easy and works fine
非常简单,工作正常
treeView1.SelectedNode.NodeFont = new System.Drawing.Font(treeView1.SelectedNode.TreeView.Font, treeView1.SelectedNode.TreeView.Font.Style | FontStyle.Bold);
this.treeView1.SelectedNode.Text += string.Empty;
回答by Bappie
This is all not helping for me. What DID the trick is making the font a little bigger and bold at DESIGN time. (In the Properties window)
这一切对我都没有帮助。诀窍是在设计时使字体更大更粗。(在属性窗口中)
So make sure you define the treeview with big enough font, then later you can add nodes with smaller font. They will fit.
因此,请确保使用足够大的字体定义树视图,然后您可以使用较小的字体添加节点。他们会适合。
回答by user3161309
I do agree with the solution provided. I just want to add to it to shed a little more light on what the problem is. The treeview has its own font which determines the width of items at the root level. That compensates for the fact that there is only an item height property available and no item width property.
我同意提供的解决方案。我只是想补充一点,以进一步说明问题所在。树视图有自己的字体,用于确定根级别项目的宽度。这弥补了只有一个项目高度属性可用而没有项目宽度属性的事实。
The solution to your problem is to determine what the font of your root node should be, then set the tree to that same font. You can do that at design time also.
您的问题的解决方案是确定根节点的字体应该是什么,然后将树设置为相同的字体。你也可以在设计时做到这一点。
Hope that helps someone.
希望能帮助某人。
回答by Ismael
A workaround for this problem is this:
此问题的解决方法是:
Set the defaul font of treeview to bold in the properties.
在属性中将树视图的默认字体设置为粗体。
And chnage to not bold when you need.
并在需要时更改为不粗体。
回答by Ismael
I realize this is an old thread and it may have been answered. I just ran across this problem as I'm learning to use TreeViews. What worked for me was changing the font size for the entire TreeView to the same size, or bigger than the font of the level you want to bold. The default font size is 8.something. I changed mine to 10, which was the size I wanted my nodes, and the truncating was gone.
我意识到这是一个旧线程,它可能已经得到了回答。我刚刚在学习使用 TreeViews 时遇到了这个问题。对我有用的是将整个 TreeView 的字体大小更改为相同的大小,或者大于您想要加粗的级别的字体。默认字体大小为 8.something。我将我的更改为 10,这是我想要的节点大小,并且截断消失了。
回答by sean.net
What worked for me: Hooking into the load event in the Control's constructor and tweaking the node as explained in BlunT's answer.
对我有用的方法:在 Control 的构造函数中连接 load 事件并按照 BlunT 的回答中的说明调整节点。
public MyControl()
{
InitializeComponent();
_head = new TreeNode();
this.Load += (s, e) =>
{
trvParts.Nodes.Clear();
_head.NodeFont = new Font(trvParts.Font, FontStyle.Bold);
trvParts.Nodes.Add(_head);
_head.Text = "Node Name";
};
}
回答by planetblu
It's in vb.Net however the solution to re-enter the value of the TEXT field gets around this nicely. As in:
它在 vb.Net 中,但是重新输入 TEXT 字段值的解决方案很好地解决了这个问题。如:
With myNode
Dim myText As String = .Text 'capture the text
.NodeFont = New Font(<name of your treeview>.Font, FontStyle.Bold)
.Text = myText 'reset the text to the original value
End With