使用Treeview显示信息

时间:2016-03-24 09:07:21

标签: c# winforms treeview

我有treeview,其中包含国家/地区列表,我还有一个textbox,其中包含有关每个国家/地区的说明,如何根据点击的节点更改说明中的文字。< / p>

1 个答案:

答案 0 :(得分:1)

您可以订阅TreeView的{​​{3}}事件:

public partial class Form1
{
    private TreeView treeView1;
    private TextBox textBox1;
    // ... shortened example

    public Form1()
    {
        InitializeComponent();
        treeView1.AfterSelect += treeView1_AfterSelect;
        //...
    }

    private void TreeView1_AfterSelect(object sender, TreeViewEventArgs e)
    {
        string description = string.Empty;
        TreeNode node = treeView1.SelectedNode;
        if (node != null)
            description = // determine the text from your country data

        textBox1.Text = description;
    }
}

我通常将Tag的{​​{1}}属性设置为相应的模型实例。所以如果你有一个TreeNode类:

Country

我这样添加public class Country { public string Name { get; set; } public string Description { get; set; } }

TreeNodes

您的Country country = new Country { Name = "SomeCountry", Description = "description" }; TreeNode nextNode = new TreeNode(country.Name); nextNode.Tag = country; parentNode.Nodes.Add(nextNode); 处理程序可能如下所示:

AfterSelect