如何在c#win的treeview中的树节点中写多行。形成 [英] How to write multiple line in a tree node in treeview in c# win. form

查看:219
本文介绍了如何在c#win的treeview中的树节点中写多行。形成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在treeview的节点中写多行: -



示例:图片 [ ^ ]

How to write multiple line in treeview's node:-

Example:Image[^]

推荐答案

正如您所知,没有直接的方法来获取文本多行显示为WinForms的TreeView TreeNode的内容。



从理论上讲,您可以通过自定义所有者绘制节点及其文本来实现此目的,并且Microsoft提供了自FrameWork 2.0以来,这个例子(但是,该示例没有演示多行文本):[ ^ ]。



我建议你不要浪费时间尝试创建自定义所有者绘制的节点,除非:你有经验的开发人员,你有很多时间浪费一点点回报。



那么,还有什么选择?



1.将TreeView的'Scrollable属性设置为'true,让最终用户滚动查看TreeNode中的扩展文本内容。



2.将'Scrollable to'设置为false,将'ShowNodeToolTips设置为'true,并将带有换行符的字符串分配给所有或部分TreeNodes的ToolTip属性。或者,您可以编写一些代码,在应用程序启动时递归遍历所有TreeNodes,并仅为那些您以某种方式计算出具有扩展长度Text的TreeNode创建自定义工具提示。问题是,鉴于TreeView的宽度,无法准确检测节点中当前文本的多少



最后,我建议你检查多年来在CP上发布的各种TreeView和TreeListView控件,从Philip Piper的经典'ObjectListView开始,它已经发展多年,并且仍然得到积极的支持(如2014年5月):[ ^ ]。



我相信,但我不确定,'ObjectListView支持节点中的多行文本内容。
As you have found out, there is no direct way to have text with multiple lines displayed as the content of a WinForms' TreeView TreeNode.

Theoretically you could do this by custom owner-drawing the Node and its Text, and Microsoft has supplied an example of this (but, the example does not demonstrate multiple lines of text) since FrameWork 2.0: [^].

I would suggest you not waste the time to try and create custom owner-drawn nodes unless: you are an experienced developer, and you have a lot of time to waste for little return.

So, what are the alternatives ?

1. set the 'Scrollable property of the TreeView to 'true, and let the end-user scroll to see extended text content in a TreeNode.

2. set 'Scrollable to 'false and 'ShowNodeToolTips to 'true, and assign strings with line-break characters to the ToolTip property of all, or some, TreeNodes. Or, you could write some code that at application launch recursively went through all the TreeNodes, and created custom ToolTips for only those TreeNodes that you somehow figured out had extended length Text. The problem being there's no way to detect exactly how much of the current Text in the Node is not visible given the TreeView's width.

Finally, I'd suggest you examine the various TreeView, and TreeListView, controls that have been published here on CP over the years, starting with Philip Piper's classic 'ObjectListView, which has been evolving for many years, and which is still actively supported (as of May, 2014): [^].

I believe, but am not sure, that 'ObjectListView supports multi-line text content in Nodes.


您好我只给出了方法知道如何做到这一点确定它的唯一例子我还没有测试这个代码是否有用......





private void Form1_Load(ob ject发送者,EventArgs e)

{

//

//这是视图中的第一个节点。

//

TreeNode treeNode = new TreeNode(BMW);

treeView1.Nodes.Add(treeNode);

//

//第一个节点后面的另一个节点。

//

treeNode = new TreeNode(Maruti);

treeView1.Nodes.Add(treeNode);

//

//创建两个子节点并将它们放在一个数组中。

// ...添加第三个节点,并将它们指定为子节点。

//

TreeNode node2 = new TreeNode(Boloro);

TreeNode node3 = new TreeNode(Xylo);

TreeNode [] array = new TreeNode [] {node2,node3};

//

//最终节点。

//

treeNode = new TreeNode(Mahindra,数组);

treeView1.Nodes.Add(treeNode);

}
Hi i am giving u method only to know how to do this ok its only example i am not testing yet this code is work or not......


private void Form1_Load(object sender, EventArgs e)
{
//
// This is the first node in the view.
//
TreeNode treeNode = new TreeNode("BMW");
treeView1.Nodes.Add(treeNode);
//
// Another node following the first node.
//
treeNode = new TreeNode("Maruti");
treeView1.Nodes.Add(treeNode);
//
// Create two child nodes and put them in an array.
// ... Add the third node, and specify these as its children.
//
TreeNode node2 = new TreeNode("Boloro");
TreeNode node3 = new TreeNode("Xylo");
TreeNode[] array = new TreeNode[] { node2, node3 };
//
// Final node.
//
treeNode = new TreeNode("Mahindra", array);
treeView1.Nodes.Add(treeNode);
}


这篇关于如何在c#win的treeview中的树节点中写多行。形成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆