cscの作法 その487

# 概要
cscの作法、調べてみた。
練習問題やってみた。

# 練習問題
datatableで、CRUDを実装せよ。

# 方針
- tabcontrole使う。
- listview使う。

# 写真

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/4582f29c-7fe1-da9a-676f-e961b03679a1.png)

# サンプルコード

```


using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;

class SampleForm: Form {
	DataTable dt;
	ListView lsv;
	TextBox bo0;
	TextBox bo1;
	TextBox bo2;
	TextBox bo3;
	TextBox bo4;
	SampleForm() {
		ClientSize = new Size(340, 340);
		Text = "CRUD";
		dt = new DataTable();
		dt.Columns.Add("id", typeof(string));
		dt.Columns.Add("ToDo", typeof(string));
		TabControl tabc = new TabControl() {
			Location = new Point(20, 20),
			Size = new Size(300, 300),
		};
		Controls.AddRange(new Control[] {
			tabc
		});
		TabPage tabp0 = new TabPage() {
			Text = "Create",
			Dock = DockStyle.Fill,
		};
		tabc.TabPages.Add(tabp0);
		Label la0 = new Label();
		la0.Location = new Point(50, 80);
		la0.Text = "id";
		bo0 = new TextBox();
		bo0.Location = new Point(150, 80);
		bo0.Text = "1";
		Label la1 = new Label();
		la1.Location = new Point(50, 110);
		la1.Text = "To Do";
		bo1 = new TextBox();
		bo1.Location = new Point(150, 110);
		bo1.Text = "金持ちになる";
		Button btn0 = new Button();
		btn0.Location = new Point(150, 150);
		btn0.Text = "登録";
		btn0.Click += btn0_Click;
		tabp0.Controls.Add(la0);
		tabp0.Controls.Add(bo0);
		tabp0.Controls.Add(la1);
		tabp0.Controls.Add(bo1);
		tabp0.Controls.Add(btn0);
		TabPage tabp1 = new TabPage() {
			Text = "Read",
			Dock = DockStyle.Fill,
		};
		tabc.TabPages.Add(tabp1);
		Button btn1 = new Button();
		btn1.Location = new Point(100, 10);
		btn1.Text = "呼出";
		btn1.Click += btn1_Click;
		lsv = new ListView();
		lsv.Location = new Point(0, 40);
		lsv.Size = new System.Drawing.Size(300, 200);
		lsv.View = View.Details;
		lsv.FullRowSelect = true;
		lsv.GridLines = true;
		lsv.Columns.Add("id", 50, HorizontalAlignment.Left);
		lsv.Columns.Add("ToDo", 200, HorizontalAlignment.Left);
		tabp1.Controls.Add(btn1);
		tabp1.Controls.Add(lsv);
		TabPage tabp2 = new TabPage() {
			Text = "Update",
			Dock = DockStyle.Fill,
		};
		tabc.TabPages.Add(tabp2);
		Label la2 = new Label();
		la2.Location = new Point(50, 80);
		la2.Text = "id";
		bo2 = new TextBox();
		bo2.Location = new Point(150, 80);
		bo2.Text = "1";
		Label la3 = new Label();
		la3.Location = new Point(50, 110);
		la3.Text = "To Do";
		bo3 = new TextBox();
		bo3.Location = new Point(150, 110);
		bo3.Text = "車を買う";
		Button btn2 = new Button();
		btn2.Location = new Point(150, 150);
		btn2.Text = "更新";
		btn2.Click += btn2_Click;
		tabp2.Controls.Add(la2);
		tabp2.Controls.Add(bo2);
		tabp2.Controls.Add(la3);
		tabp2.Controls.Add(bo3);
		tabp2.Controls.Add(btn2);
		TabPage tabp3 = new TabPage() {
			Text = "Delete",
			Dock = DockStyle.Fill,
		};
		tabc.TabPages.Add(tabp3);
		Label la4 = new Label();
		la4.Location = new Point(50, 110);
		la4.Text = "id";
		bo4 = new TextBox();
		bo4.Location = new Point(150, 110);
		bo4.Text = "1";
		Button btn3 = new Button();
		btn3.Location = new Point(150, 150);
		btn3.Text = "削除";
		btn3.Click += btn3_Click;
		tabp3.Controls.Add(la4);
		tabp3.Controls.Add(bo4);
		tabp3.Controls.Add(btn3);
	}
	void btn0_Click(object sender, System.EventArgs e) {
		dt.Rows.Add(bo0.Text, bo1.Text);
	}
	void btn1_Click(object sender, System.EventArgs e) {
		lsv.Items.Clear();
		for (int i = 0; i < dt.Rows.Count; i++)
		{
			DataRow row = dt.Rows[i];
			ListViewItem listitem = new ListViewItem(row["id"].ToString());
			listitem.SubItems.Add(row["ToDo"].ToString());
			lsv.Items.Add(listitem);
		}
	}
	void btn2_Click(object sender, System.EventArgs e) {
		for (int i = 0; i < dt.Rows.Count; i++)
		{
			DataRow row = dt.Rows[i];
			if (row["id"].ToString() == bo2.Text)
			{
				row["ToDo"] = bo3.Text;
			}
		}
	}
	void btn3_Click(object sender, System.EventArgs e) {
		for (int i = 0; i < dt.Rows.Count; i++)
		{
			DataRow row = dt.Rows[i];
			if (row["id"].ToString() == bo4.Text)
			{
				dt.Rows.Remove(row);
			}
		}
	}
	[STAThread]
	static void Main(string[] args) {
		Application.Run(new SampleForm());
	}
}





```

以上。