2016-03-24 8 views

答えて

1

あなたTreeViewAfterSelectイベントをサブスクライブすることができます:私は、通常、対応するモデルインスタンスにTreeNodeTagプロパティを設定し

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; 
    } 
関連する問題