cscの作法 その354

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

# 練習問題
TableLayoutPanelで電卓を作れ。

# 写真

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/3e47633d-e0d0-aabd-f6ed-dc607e6ce8a8.png)


# サンプルコード

```
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

public class Form1: Form {
	Button btn0;
	Button btn1;
	Button btn2;
	Button btn3;
	Button btn4;
	Button btn5;
	Button btn6;
	Button btn7;
	Button btn8;
	Button btn9;
	Button btn10;
	Button btn11;
	Button btn12;
	Button btn13;
	Button btn14;
	Button btn15;
	Button btn16;
	string v;
	double m = 0;
	double n = 0;
	int s = 0;
	Label label;
	public Form1() {
		ClientSize = new Size(260, 260);
		Text = "電卓";
		InitializeComponent();
		v = "";
	}
	private void InitializeComponent() {
		label = new Label();
		label.Text = "0";
		label.Font = new Font("Geneva", 20, FontStyle.Regular);
		label.BackColor = Color.Azure;
		label.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
		label.Dock = DockStyle.Fill;
		TableLayoutPanel parent = new TableLayoutPanel();
		parent.Width = 250;
		parent.Height = 250;
		parent.ColumnCount = 4;
		parent.RowCount = 6;
		for (int i = 0; i < parent.RowCount; i++)
		{
			parent.RowStyles.Add(new RowStyle() {
				Height = 20,
				SizeType = SizeType.Percent
			});
		}
		for (int j = 0; j < parent.ColumnCount; j++)
		{
			parent.ColumnStyles.Add(new ColumnStyle() {
				Width = 25,
				SizeType = SizeType.Percent
			});
		}
		this.Controls.Add(parent);
		Control childControl;
		btn0 = new Button();
		btn0.Click += new System.EventHandler(btn0_Click);
		btn0.Text = "Clear";
		btn0.Dock = DockStyle.Fill;
		parent.Controls.Add(btn0);
		btn1 = new Button();
		btn1.Click += new System.EventHandler(btn1_Click);
		btn1.Text = "/";
		btn1.Dock = DockStyle.Fill;
		parent.Controls.Add(btn1);
		btn2 = new Button();
		btn2.Click += new System.EventHandler(btn2_Click);
		btn2.Text = "*";
		btn2.Dock = DockStyle.Fill;
		parent.Controls.Add(btn2);
		btn3 = new Button();
		btn3.Click += new System.EventHandler(btn3_Click);
		btn3.Text = "-";
		btn3.Dock = DockStyle.Fill;
		parent.Controls.Add(btn3);
		btn4 = new Button();
		btn4.Click += new System.EventHandler(btn4_Click);
		btn4.Text = "7";
		btn4.Dock = DockStyle.Fill;
		parent.Controls.Add(btn4);
		btn5 = new Button();
		btn5.Click += new System.EventHandler(btn5_Click);
		btn5.Text = "8";
		btn5.Dock = DockStyle.Fill;
		parent.Controls.Add(btn5);
		btn6 = new Button();
		btn6.Click += new System.EventHandler(btn6_Click);
		btn6.Text = "9";
		btn6.Dock = DockStyle.Fill;
		parent.Controls.Add(btn6);
		btn7 = new Button();
		btn7.Click += new System.EventHandler(btn7_Click);
		btn7.Text = "+";
		btn7.Dock = DockStyle.Fill;
		parent.Controls.Add(btn7);
		childControl = parent.GetControlFromPosition(3, 1);
		parent.SetRowSpan(childControl, 2);
		btn8 = new Button();
		btn8.Click += new System.EventHandler(btn8_Click);
		btn8.Text = "4";
		btn8.Dock = DockStyle.Fill;
		parent.Controls.Add(btn8);
		btn9 = new Button();
		btn9.Click += new System.EventHandler(btn9_Click);
		btn9.Text = "5";
		btn9.Dock = DockStyle.Fill;
		parent.Controls.Add(btn9);
		btn10 = new Button();
		btn10.Click += new System.EventHandler(btn10_Click);
		btn10.Text = "6";
		btn10.Dock = DockStyle.Fill;
		parent.Controls.Add(btn10);
		btn11 = new Button();
		btn11.Click += new System.EventHandler(btn11_Click);
		btn11.Text = "1";
		btn11.Dock = DockStyle.Fill;
		parent.Controls.Add(btn11);
		btn12 = new Button();
		btn12.Click += new System.EventHandler(btn12_Click);
		btn12.Text = "2";
		btn12.Dock = DockStyle.Fill;
		parent.Controls.Add(btn12);
		btn13 = new Button();
		btn13.Click += new System.EventHandler(btn13_Click);
		btn13.Text = "3";
		btn13.Dock = DockStyle.Fill;
		parent.Controls.Add(btn13);
		btn14 = new Button();
		btn14.Click += new System.EventHandler(btn14_Click);
		btn14.Text = "Enter";
		btn14.Dock = DockStyle.Fill;
		parent.Controls.Add(btn14);
		childControl = parent.GetControlFromPosition(3, 3);
		parent.SetRowSpan(childControl, 2);
		btn15 = new Button();
		btn15.Click += new System.EventHandler(btn15_Click);
		btn15.Text = "0";
		btn15.Dock = DockStyle.Fill;
		parent.Controls.Add(btn15);
		childControl = parent.GetControlFromPosition(0, 4);
		parent.SetColumnSpan(childControl, 2);
		btn16 = new Button();
		btn16.Click += new System.EventHandler(btn16_Click);
		btn16.Text = ".";
		btn16.Dock = DockStyle.Fill;
		parent.Controls.Add(btn16);
		parent.Controls.Add(label);
		childControl = parent.GetControlFromPosition(0, 5);
		parent.SetColumnSpan(childControl, 3);
	}
	private void btn0_Click(object sender, EventArgs e) {
		v = "";
		m = 0;
		label.Text = v;
		Console.WriteLine("clear");
	}
	private void btn1_Click(object sender, EventArgs e) {
		m = double.Parse(v);
		v = "";
		s = 1;
		//label.Text = v;
		Console.WriteLine("/");
	}
	private void btn2_Click(object sender, EventArgs e) {
		m = double.Parse(v);
		v = "";
		s = 2;
		//label.Text = v;
		Console.WriteLine("*");
	}
	private void btn3_Click(object sender, EventArgs e) {
		m = double.Parse(v);
		v = "";
		s = 3;
		//label.Text = v;
		Console.WriteLine("-");
	}
	private void btn4_Click(object sender, EventArgs e) {
		v += "7";
		label.Text = v;
	}
	private void btn5_Click(object sender, EventArgs e) {
		v += "8";
		label.Text = v;
	}
	private void btn6_Click(object sender, EventArgs e) {
		v += "9";
		label.Text = v;
	}
	private void btn7_Click(object sender, EventArgs e) {
		m = double.Parse(v);
		v = "";
		s = 4;
		//label.Text = v;
		Console.WriteLine("+");
	}
	private void btn8_Click(object sender, EventArgs e) {
		v += "4";
		label.Text = v;
	}
	private void btn9_Click(object sender, EventArgs e) {
		v += "5";
		label.Text = v;
	}
	private void btn10_Click(object sender, EventArgs e) {
		v += "6";
		label.Text = v;
	}
	private void btn11_Click(object sender, EventArgs e) {
		v += "1";
		label.Text = v;
	}
	private void btn12_Click(object sender, EventArgs e) {
		v += "2";
		label.Text = v;
	}
	private void btn13_Click(object sender, EventArgs e) {
		v += "3";
		label.Text = v;
	}
	private void btn14_Click(object sender, EventArgs e) {
		n = double.Parse(v);
		switch(s)
		{
		case 1:
			m = m / n;
		break;
		case 2:
			m = m * n;
		break;
		case 3:
			m = m - n;
		break;
		case 4:
			m = m + n;
		break;
		default:
		break;
		}
		v = m.ToString();
		label.Text = v;
		Console.WriteLine("enter");
	}
	private void btn15_Click(object sender, EventArgs e) {
		v += "0";
		label.Text = v;
	}
	private void btn16_Click(object sender, EventArgs e) {
		v += ".";
		label.Text = v;
	}
	[STAThread]
	static void Main() {
		Application.EnableVisualStyles();
		Application.Run(new Form1());
	}
}





```


以上。