VB.NET, C#, PowerShellを使用しているエンジニアのためのサイト。
基本的な使用方法から開発で役立つTipsまで幅広く取り扱っています。
全ノードを展開する場合は、ExpandAllメソッドを、選択したノードを展開する場合はSelectedNode.Expandメソッド、選択したノードのすべて子を展開する場合はSelectedNode.ExpandAllメソッドを使用します。
private void Form1_Load(object sender, EventArgs e) { TreeNode rootNode = new TreeNode(); TreeNode childNode1 = new TreeNode(); TreeNode childNode2 = new TreeNode(); TreeNode grandChildNode1 = new TreeNode(); TreeNode grandChildNode2 = new TreeNode(); TreeNode grandChildNode3 = new TreeNode(); //親ノードの作成 rootNode.Text = "ルート"; //子ノードの作成 childNode1.Text = "子ノード1"; childNode2.Text = "子ノード2"; //孫ノードの作成 grandChildNode1.Text = "孫ノード1"; grandChildNode2.Text = "孫ノード2"; grandChildNode3.Text = "孫ノード3"; //孫ノードを親ノードに追加 childNode2.Nodes.Add(grandChildNode3); childNode1.Nodes.Add(grandChildNode1); childNode1.Nodes.Add(grandChildNode2); //子ノードを親ノードに追加 rootNode.Nodes.Add(childNode1); rootNode.Nodes.Add(childNode2); //親ノードをTreeViewに追加 treeView1.Nodes.Add(rootNode); } //全展開の例 private void button1_Click(object sender, EventArgs e) { treeView1.ExpandAll(); } //選択ノードの展開例 private void button2_Click(object sender, EventArgs e) { //選択されているノードがない場合は処理を抜ける if (treeView1.SelectedNode == null) return; treeView1.SelectedNode.Expand(); } //選択ノードのすべての展開例 private void button3_Click(object sender, EventArgs e) { //選択されているノードがない場合は処理を抜ける if (treeView1.SelectedNode == null) return; treeView1.SelectedNode.ExpandAll(); }