//////////////////////////
//C# Sample_TextBox.cs
//////////////////////////

using System;
using System.Windows.Forms;
using System.Drawing;
 
namespace WindowsFormsApp
{
	public partial class AppForm : Form
	{
		TextBox txtBox;
		Button clrBtn, rdBtn, wrtBtn, extBtn;

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

		public AppForm()
		{
//			this.AutoSize = true;
			this.Size = new Size(640, 480);
			this.MinimumSize = new Size(320, 190);
			this.Text = "TextBox Test";
			this.Load += AppForm_Load;
		}

		private void AppForm_Load(object sender, EventArgs e)
		{
			//Clearボタン
			clrBtn = new Button();
			clrBtn.Location = new Point(ClientSize.Width - clrBtn.Width - 10, 10);
			clrBtn.Text = "Clear";
			clrBtn.Anchor = (AnchorStyles.Top | AnchorStyles.Right);
			clrBtn.Click += Button1_Click;
			this.Controls.Add(clrBtn);

			//Readボタン
			rdBtn = new Button();
			rdBtn.Location = new Point(ClientSize.Width - rdBtn.Width - 10, clrBtn.Height + 20);
			rdBtn.Text = "Read";
			rdBtn.Anchor = (AnchorStyles.Top | AnchorStyles.Right);
			rdBtn.Click += Button2_Click;
			this.Controls.Add(rdBtn);

			//Writeボタン
			wrtBtn = new Button();
			wrtBtn.Location = new Point(ClientSize.Width - wrtBtn.Width - 10, clrBtn.Height + rdBtn.Height + 30);
			wrtBtn.Text = "Write";
			wrtBtn.Anchor = (AnchorStyles.Top | AnchorStyles.Right);
			wrtBtn.Click += Button3_Click;
			this.Controls.Add(wrtBtn);

			//Exitボタン
			extBtn = new Button();
			extBtn.Location = new Point(ClientSize.Width - extBtn.Width - 10, ClientSize.Height - extBtn.Height - 20);
			extBtn.Text = "Exit";
			extBtn.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right);
			extBtn.Click += Button4_Click;
			this.Controls.Add(extBtn);

			//テキストボックス
			txtBox = new TextBox();
			//位置
			txtBox.Location = new Point(10, 10);
			txtBox.Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right);
			//サイズ
			txtBox.Width = ClientSize.Width - clrBtn.Width - 30;
			txtBox.Height = ClientSize.Height - 20;
			//その他
			txtBox.Multiline = true;
			txtBox.ScrollBars = ScrollBars.Both;
			txtBox.WordWrap  = false;
			//FormにTextBoxを追加
			this.Controls.Add(txtBox);
		}

		//終了時処理
		protected override void OnFormClosing(FormClosingEventArgs e)
		{
			base.OnFormClosing(e);
			DialogResult dr = MessageBox.Show("終了しますか？", "確認", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
			if(dr == DialogResult.No)
			{
				e.Cancel = true;
			}
		}

		private void Button1_Click(object sender, EventArgs e)
		{
			//txtBoxをクリアする
			txtBox.Clear();
		}

		private void Button2_Click(object sender, EventArgs e)
		{
			//txtBoxにファイルを読む
			txtBox.Text = FileRead();
		}

		private void Button3_Click(object sender, EventArgs e)
		{
			//txtBox内容をファイルに書く
			FileWrite(txtBox.Text);
		}

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

		private string FileRead()
		{
			string text = "";
			//ファイルを読む
			return text;
		}

		private void FileWrite(string str)
		{
			//ファイルを書く
			
		}
	}
}
