0
私はtreeview
に国のリストを持っています。また、各国についての説明があるtextbox
もあります。どのノードをクリックするかによってどのようにテキストを変更できるのですか?ツリービューを使用して情報を表示する
私はtreeview
に国のリストを持っています。また、各国についての説明があるtextbox
もあります。どのノードをクリックするかによってどのようにテキストを変更できるのですか?ツリービューを使用して情報を表示する
あなたTreeView
のAfterSelect
イベントをサブスクライブすることができます:私は、通常、対応するモデルインスタンスにTreeNode
のTag
プロパティを設定し
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;
}
}
。あなたはこのようCountry
クラスを持っているのであれば:
Country country = new Country { Name = "SomeCountry", Description = "description" };
TreeNode nextNode = new TreeNode(country.Name);
nextNode.Tag = country;
parentNode.Nodes.Add(nextNode);
そして、あなたのAfterSelect
ハンドラは次のようになります:
public class Country
{
public string Name { get; set; }
public string Description { get; set; }
}
私はこのようなTreeNodes
を追加したい
private void TreeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
textBox1.Text = (treeView1.SelectedNode?.Tag as Country)?.Description ?? string.Empty;
}