//////////////////////////
//C# Sample_TreeView.cs
//////////////////////////

using System;
using System.Windows.Forms;
using System.Drawing;
 
namespace WindowsFormsApp1
{
	public partial class AppForm : Form
	{
		TreeView treeView;
		Button button1, button2, button3;

		[STAThread]
		public static void Main()
		{
			AppForm ap = new AppForm();
			Application.Run(ap);
		}

		public AppForm()
		{
//			this.AutoSize = true;
			this.Text = "TreeView Test";
			this.Load += AppForm_Load;
		}
 
		private void AppForm_Load(object sender, EventArgs e)
		{
			//ExpandAllボタン
			button1 = new Button();
			button1.Location = new Point(ClientSize.Width - button1.Width - 10, 10);
			button1.Text = "Expand All";
			button1.Anchor = (AnchorStyles.Top | AnchorStyles.Right);
			button1.Click += Button1_Click;
			this.Controls.Add(button1);
 
			//CollapseAllボタン
			button2 = new Button();
			button2.Location = new Point(ClientSize.Width - button2.Width - 10, 60);
			button2.Text = "Collapse All";
			button2.Anchor = (AnchorStyles.Top | AnchorStyles.Right);
			button2.Click += Button2_Click;
			this.Controls.Add(button2);

			//Exitボタン
			button3 = new Button();
			button3.Location = new Point(ClientSize.Width - button3.Width - 10, 110);
			button3.Text = "Exit";
			button3.Anchor = (AnchorStyles.Top | AnchorStyles.Right);
			button3.Click += Button3_Click;
			this.Controls.Add(button3);

			//ツリービュー
			treeView = new TreeView();
			//位置
			treeView.Location = new Point(10, 10);
			treeView.Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right);
			//サイズ
			treeView.Width = ClientSize.Width - button1.Width - 30;
			treeView.Height = ClientSize.Height - 20;
			//チェックボックス有効
			treeView.CheckBoxes = true;
			//チェック時のイベント
			treeView.AfterCheck += TreeView_AfterCheck;
			//FormにTreeViewを追加
			this.Controls.Add(treeView);
 
			//rootノード
			TreeNode rootNode = new TreeNode("root node");
			//childノード
			TreeNode childNode1 = new TreeNode("node1");
			TreeNode childNode2 = new TreeNode("node2");
			TreeNode childNode3 = new TreeNode("node3");
 
			//childNode1のチェックを有効
//			childNode1.Checked = true;
 
			//grandChildノード
			TreeNode grandChildNode11 = new TreeNode("node1-1");
			TreeNode grandChildNode12 = new TreeNode("node1-2");
			TreeNode grandChildNode21 = new TreeNode("node2-1");
			TreeNode grandChildNode22 = new TreeNode("node2-2");
			TreeNode grandChildNode31 = new TreeNode("node3-1");
			TreeNode grandChildNode32 = new TreeNode("node3-2");
 
			//rootノードをTreeViewに追加
			treeView.Nodes.Add(rootNode);
 
			//childノードをrootノードに追加
			rootNode.Nodes.Add(childNode1);
			rootNode.Nodes.Add(childNode2);
			rootNode.Nodes.Add(childNode3);
 
			//grandChildノードをchildノードに追加
			childNode1.Nodes.Add(grandChildNode11);
			childNode1.Nodes.Add(grandChildNode12);
			childNode2.Nodes.Add(grandChildNode21);
			childNode2.Nodes.Add(grandChildNode22);
			childNode3.Nodes.Add(grandChildNode31);
			childNode3.Nodes.Add(grandChildNode32);
		}
 
		private void Button1_Click(object sender, EventArgs e)
		{
			//全ノードを展開
			treeView.ExpandAll();
		}

		private void Button2_Click(object sender, EventArgs e)
		{
			//全ノードを折りたたむ
			treeView.CollapseAll();
		}

		private void Button3_Click(object sender, EventArgs e)
		{
			//終了する
			Close();
		}

		private void TreeView_AfterCheck(object sender, TreeViewEventArgs e)
		{
			//ノードをチェック後に、配下ノードをまとめてチェック
			foreach (TreeNode node in e.Node.Nodes) node.Checked = e.Node.Checked;
		}
	}
}
