//////////////////////////
//C# Sample_ListView.cs
//////////////////////////

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

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

		public AppForm()
		{
			//this.AutoSize = true;	//trueにすると縮まらなくなる
			this.Width = 440;
			this.Height = 240;
			this.Text = "ListView 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 = "Add data";
			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 = "Clear 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);

			//リストビュー
			listView = new ListView();
			//ListViewのプロパティを設定
			//位置
			listView.Location = new Point(10, 10);
			listView.Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right);
			//サイズ
			listView.Width = ClientSize.Width - button1.Width - 30;
			listView.Height = ClientSize.Height - 20;
			//一行選択
			listView.FullRowSelect = true;
			//グリッドラインの表示
			listView.GridLines = true;
			//並び替え表示
			listView.Sorting = SortOrder.Ascending;
			//ポイントで選択できるようにする
			listView.HoverSelection = false;
			//シングルクリック選択
			listView.Activation = ItemActivation.OneClick;
			//チェックボックス有効化
			listView.CheckBoxes = false;
			//リストビューの表示方法
			listView.View = View.Details;
//			listView.View = View.SmallIcon;
//			listView.View = View.LargeIcon;
//			listView.View = View.List;
//			listView.View = View.Tile;
			//ItemActivateイベントハンドラの追加
			listView.ItemActivate += new EventHandler(ListView_ItemActivate);
			listView.ItemSelectionChanged += new ListViewItemSelectionChangedEventHandler(ListView_ItemChanged);
			//ヘッダー定義
			ColumnHeader columnNo = new ColumnHeader{Text = "名前", Width = 100};
			ColumnHeader columnType = new ColumnHeader{Text = "種類", Width = 60};
			ColumnHeader columnData = new ColumnHeader{Text = "データ", Width = 150};
			ColumnHeader[] colHeaderRegValue = {columnNo, columnType, columnData};	//ヘッダー設定
			listView.Columns.AddRange(colHeaderRegValue);	//ヘッダー追加
			//他の列設定方法
//			listView.Columns.Add("名前", 100, HorizontalAlignment.Left);
//			listView.Columns.Add("種類", 60, HorizontalAlignment.Left);
//			listView.Columns.Add("データ", 150, HorizontalAlignment.Left);
			//FormにListViewを追加
			this.Controls.Add(listView);
		}

		private void Button1_Click(object sender, EventArgs e)
		{
			ListViewItem item0 = listView.Items.Add("リンゴ");
			item0.SubItems.Add("果物");
			item0.SubItems.Add("赤");
			ListViewItem item1 = listView.Items.Add("メロン");
			item1.SubItems.Add("果物");
			item1.SubItems.Add("緑");
			ListViewItem item2 = listView.Items.Add("ピーマン");
			item2.SubItems.Add("野菜");
			item2.SubItems.Add("緑");
/*
			//ListViewにデータを追加
			string[] item1 = { "リンゴ", "果物", "赤"};
			listView.Items.Add(new ListViewItem(item1));
			string[] item2 = { "メロン", "果物", "緑"};
			listView.Items.Add(new ListViewItem(item2));
			string[] item3 = { "ピーマン", "野菜", "緑"};
			listView.Items.Add(new ListViewItem(item3));
*/
		}

		private void Button2_Click(object sender, EventArgs e)
		{
			//ListViewのデータをクリアする
			listView.Items.Clear();
		}

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

		private void ListView_ItemActivate(object sender, EventArgs e)
		{
			//アイテムがアクティブになった時
			ListView lv = (ListView)sender;
			//フォーカスのあるアイテムのTextを表示する
			MessageBox.Show(lv.FocusedItem.Text);
		}

		private void ListView_ItemChanged(object sender, ListViewItemSelectionChangedEventArgs e)
		{
			//フォーカスのあるアイテムのTextを表示する
			ListView lv = (ListView)sender;
			MessageBox.Show(lv.FocusedItem.Text);
			string str = String.Format("{0} = {1}", "IsSelected", e.IsSelected);
			MessageBox.Show(str, "ListViewItemSelectionChangedEventArgs");
			str = String.Format("{0} = {1}", "Item", e.Item);
			MessageBox.Show(str, "ListViewItemSelectionChangedEventArgs");
			str = String.Format("{0} = {1}", "ItemIndex", e.ItemIndex);
			MessageBox.Show(str, "ListViewItemSelectionChangedEventArgs");
		}
	}
}
