﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using Counter;
using CcntCs;

namespace Counter
{
	/// <summary>
	/// Comp の概要の説明です。
	/// </summary>
	public class Comp : System.Windows.Forms.Form
	{
		private System.Windows.Forms.RadioButton radioButton_Dec;
		private System.Windows.Forms.RadioButton radioButton_Hex;
		private System.Windows.Forms.GroupBox groupBox_Comparison_register_set;
		private System.Windows.Forms.Label label_Channel0;
		private System.Windows.Forms.Label label_Channel1;
		private System.Windows.Forms.Label label_Channel2;
		private System.Windows.Forms.Label label_Channel3;
		private System.Windows.Forms.Label label_Channel4;
		private System.Windows.Forms.Label label_Channel5;
		private System.Windows.Forms.Label label_Channel6;
		private System.Windows.Forms.Label label_Channel7;
		private System.Windows.Forms.GroupBox groupBox_Pulse_set_consistent_with_count;
		private System.Windows.Forms.Label label_PulseWidth;
		private System.Windows.Forms.ComboBox comboBox_PulseWidth;
		private System.Windows.Forms.Button button_OK;
		private System.Windows.Forms.Button button_Cancel;
		private System.Windows.Forms.TextBox textBox_Channel0;
		private System.Windows.Forms.TextBox textBox_Channel1;
		private System.Windows.Forms.TextBox textBox_Channel2;
		private System.Windows.Forms.TextBox textBox_Channel3;
		private System.Windows.Forms.TextBox textBox_Channel4;
		private System.Windows.Forms.TextBox textBox_Channel5;
		private System.Windows.Forms.TextBox textBox_Channel6;
		private System.Windows.Forms.TextBox textBox_Channel7;
		/// <summary>
		/// 必要なデザイナ変数です。
		/// </summary>
		private System.ComponentModel.Container components = null;

		Ccnt cnt=new Ccnt();

		private System.Windows.Forms.TextBox[] textBox_Channel = new System.Windows.Forms.TextBox[8];

		// 比較値
		static private uint[] CompValue ={ 100, 200, 300, 400, 500, 600, 700, 800 };

		public Comp()
		{
			//
			// Windows フォーム デザイナ サポートに必要です。
			//
			InitializeComponent();

			//
			// TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。
			//
			textBox_Channel[0] = textBox_Channel0;
			textBox_Channel[1] = textBox_Channel1;
			textBox_Channel[2] = textBox_Channel2;
			textBox_Channel[3] = textBox_Channel3;
			textBox_Channel[4] = textBox_Channel4;
			textBox_Channel[5] = textBox_Channel5;
			textBox_Channel[6] = textBox_Channel6;
			textBox_Channel[7] = textBox_Channel7;
			//-----------------------------
			// 表示の無効化
			//-----------------------------
			for (short ChNo = Counter.m_ChannelNum; ChNo < 8; ChNo++) 
			{
				textBox_Channel[ChNo].Enabled = false;	
			}
			//-----------------------------------
			// 画面情報を更新
			//-----------------------------------
			for (int ChNo = 0; ChNo < 8; ChNo++)
			{
				textBox_Channel[ChNo].Text = System.Convert.ToString(CompValue[ChNo]);
			}
			
			for (int i = 0; i < 0x10; i++)
			{
				comboBox_PulseWidth.Items.Add(System.Convert.ToString(i, 16));
			}
			
			int		Ret;
			short	PlsWidth;
			short   Ch_No=0;
			Ret = cnt.GetPulseWidth(Counter.m_Id, Ch_No, out PlsWidth);
			comboBox_PulseWidth.SelectedIndex = PlsWidth;
		}

		/// <summary>
		/// 使用されているリソースに後処理を実行します。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
		/// コード エディタで変更しないでください。
		/// </summary>
		private void InitializeComponent()
		{
			this.groupBox_Comparison_register_set = new System.Windows.Forms.GroupBox();
			this.textBox_Channel7 = new System.Windows.Forms.TextBox();
			this.textBox_Channel6 = new System.Windows.Forms.TextBox();
			this.textBox_Channel5 = new System.Windows.Forms.TextBox();
			this.textBox_Channel4 = new System.Windows.Forms.TextBox();
			this.textBox_Channel3 = new System.Windows.Forms.TextBox();
			this.textBox_Channel2 = new System.Windows.Forms.TextBox();
			this.textBox_Channel1 = new System.Windows.Forms.TextBox();
			this.textBox_Channel0 = new System.Windows.Forms.TextBox();
			this.label_Channel7 = new System.Windows.Forms.Label();
			this.label_Channel6 = new System.Windows.Forms.Label();
			this.label_Channel5 = new System.Windows.Forms.Label();
			this.label_Channel4 = new System.Windows.Forms.Label();
			this.label_Channel3 = new System.Windows.Forms.Label();
			this.label_Channel2 = new System.Windows.Forms.Label();
			this.label_Channel1 = new System.Windows.Forms.Label();
			this.label_Channel0 = new System.Windows.Forms.Label();
			this.radioButton_Hex = new System.Windows.Forms.RadioButton();
			this.radioButton_Dec = new System.Windows.Forms.RadioButton();
			this.groupBox_Pulse_set_consistent_with_count = new System.Windows.Forms.GroupBox();
			this.comboBox_PulseWidth = new System.Windows.Forms.ComboBox();
			this.label_PulseWidth = new System.Windows.Forms.Label();
			this.button_OK = new System.Windows.Forms.Button();
			this.button_Cancel = new System.Windows.Forms.Button();
			this.groupBox_Comparison_register_set.SuspendLayout();
			this.groupBox_Pulse_set_consistent_with_count.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox_Comparison_register_set
			// 
			this.groupBox_Comparison_register_set.Controls.AddRange(new System.Windows.Forms.Control[] {
																										   this.textBox_Channel7,
																										   this.textBox_Channel6,
																										   this.textBox_Channel5,
																										   this.textBox_Channel4,
																										   this.textBox_Channel3,
																										   this.textBox_Channel2,
																										   this.textBox_Channel1,
																										   this.textBox_Channel0,
																										   this.label_Channel7,
																										   this.label_Channel6,
																										   this.label_Channel5,
																										   this.label_Channel4,
																										   this.label_Channel3,
																										   this.label_Channel2,
																										   this.label_Channel1,
																										   this.label_Channel0,
																										   this.radioButton_Hex,
																										   this.radioButton_Dec});
			this.groupBox_Comparison_register_set.Location = new System.Drawing.Point(16, 16);
			this.groupBox_Comparison_register_set.Name = "groupBox_Comparison_register_set";
			this.groupBox_Comparison_register_set.Size = new System.Drawing.Size(248, 336);
			this.groupBox_Comparison_register_set.TabIndex = 0;
			this.groupBox_Comparison_register_set.TabStop = false;
			this.groupBox_Comparison_register_set.Text = "比較レジスタ設定";
			// 
			// textBox_Channel7
			// 
			this.textBox_Channel7.Location = new System.Drawing.Point(120, 304);
			this.textBox_Channel7.Name = "textBox_Channel7";
			this.textBox_Channel7.TabIndex = 17;
			this.textBox_Channel7.Text = "";
			this.textBox_Channel7.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			// 
			// textBox_Channel6
			// 
			this.textBox_Channel6.Location = new System.Drawing.Point(120, 272);
			this.textBox_Channel6.Name = "textBox_Channel6";
			this.textBox_Channel6.TabIndex = 16;
			this.textBox_Channel6.Text = "";
			this.textBox_Channel6.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			// 
			// textBox_Channel5
			// 
			this.textBox_Channel5.Location = new System.Drawing.Point(120, 240);
			this.textBox_Channel5.Name = "textBox_Channel5";
			this.textBox_Channel5.TabIndex = 15;
			this.textBox_Channel5.Text = "";
			this.textBox_Channel5.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			// 
			// textBox_Channel4
			// 
			this.textBox_Channel4.Location = new System.Drawing.Point(120, 208);
			this.textBox_Channel4.Name = "textBox_Channel4";
			this.textBox_Channel4.TabIndex = 14;
			this.textBox_Channel4.Text = "";
			this.textBox_Channel4.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			// 
			// textBox_Channel3
			// 
			this.textBox_Channel3.Location = new System.Drawing.Point(120, 176);
			this.textBox_Channel3.Name = "textBox_Channel3";
			this.textBox_Channel3.TabIndex = 13;
			this.textBox_Channel3.Text = "";
			this.textBox_Channel3.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			// 
			// textBox_Channel2
			// 
			this.textBox_Channel2.Location = new System.Drawing.Point(120, 144);
			this.textBox_Channel2.Name = "textBox_Channel2";
			this.textBox_Channel2.TabIndex = 12;
			this.textBox_Channel2.Text = "";
			this.textBox_Channel2.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			// 
			// textBox_Channel1
			// 
			this.textBox_Channel1.Location = new System.Drawing.Point(120, 112);
			this.textBox_Channel1.Name = "textBox_Channel1";
			this.textBox_Channel1.TabIndex = 11;
			this.textBox_Channel1.Text = "";
			this.textBox_Channel1.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			// 
			// textBox_Channel0
			// 
			this.textBox_Channel0.Location = new System.Drawing.Point(120, 80);
			this.textBox_Channel0.Name = "textBox_Channel0";
			this.textBox_Channel0.TabIndex = 10;
			this.textBox_Channel0.Text = "";
			this.textBox_Channel0.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			// 
			// label_Channel7
			// 
			this.label_Channel7.Location = new System.Drawing.Point(16, 304);
			this.label_Channel7.Name = "label_Channel7";
			this.label_Channel7.TabIndex = 9;
			this.label_Channel7.Text = "ﾁｬﾈﾙ7：";
			// 
			// label_Channel6
			// 
			this.label_Channel6.Location = new System.Drawing.Point(16, 272);
			this.label_Channel6.Name = "label_Channel6";
			this.label_Channel6.TabIndex = 8;
			this.label_Channel6.Text = "ﾁｬﾈﾙ6：";
			// 
			// label_Channel5
			// 
			this.label_Channel5.Location = new System.Drawing.Point(16, 240);
			this.label_Channel5.Name = "label_Channel5";
			this.label_Channel5.TabIndex = 7;
			this.label_Channel5.Text = "ﾁｬﾈﾙ5：";
			// 
			// label_Channel4
			// 
			this.label_Channel4.Location = new System.Drawing.Point(16, 208);
			this.label_Channel4.Name = "label_Channel4";
			this.label_Channel4.TabIndex = 6;
			this.label_Channel4.Text = "ﾁｬﾈﾙ4：";
			// 
			// label_Channel3
			// 
			this.label_Channel3.Location = new System.Drawing.Point(16, 176);
			this.label_Channel3.Name = "label_Channel3";
			this.label_Channel3.TabIndex = 5;
			this.label_Channel3.Text = "ﾁｬﾈﾙ3：";
			// 
			// label_Channel2
			// 
			this.label_Channel2.Location = new System.Drawing.Point(16, 144);
			this.label_Channel2.Name = "label_Channel2";
			this.label_Channel2.TabIndex = 4;
			this.label_Channel2.Text = "ﾁｬﾈﾙ2：";
			// 
			// label_Channel1
			// 
			this.label_Channel1.Location = new System.Drawing.Point(16, 112);
			this.label_Channel1.Name = "label_Channel1";
			this.label_Channel1.TabIndex = 3;
			this.label_Channel1.Text = "ﾁｬﾈﾙ1：";
			// 
			// label_Channel0
			// 
			this.label_Channel0.Location = new System.Drawing.Point(16, 80);
			this.label_Channel0.Name = "label_Channel0";
			this.label_Channel0.TabIndex = 2;
			this.label_Channel0.Text = "ﾁｬﾈﾙ0：";
			// 
			// radioButton_Hex
			// 
			this.radioButton_Hex.Location = new System.Drawing.Point(16, 48);
			this.radioButton_Hex.Name = "radioButton_Hex";
			this.radioButton_Hex.TabIndex = 1;
			this.radioButton_Hex.Text = "16進数";
			this.radioButton_Hex.CheckedChanged += new System.EventHandler(this.radioButton_Hex_CheckedChanged);
			// 
			// radioButton_Dec
			// 
			this.radioButton_Dec.Checked = true;
			this.radioButton_Dec.Location = new System.Drawing.Point(16, 24);
			this.radioButton_Dec.Name = "radioButton_Dec";
			this.radioButton_Dec.TabIndex = 0;
			this.radioButton_Dec.TabStop = true;
			this.radioButton_Dec.Text = "10進数";
			this.radioButton_Dec.CheckedChanged += new System.EventHandler(this.radioButton_Dec_CheckedChanged);
			// 
			// groupBox_Pulse_set_consistent_with_count
			// 
			this.groupBox_Pulse_set_consistent_with_count.Controls.AddRange(new System.Windows.Forms.Control[] {
																												   this.comboBox_PulseWidth,
																												   this.label_PulseWidth});
			this.groupBox_Pulse_set_consistent_with_count.Location = new System.Drawing.Point(16, 360);
			this.groupBox_Pulse_set_consistent_with_count.Name = "groupBox_Pulse_set_consistent_with_count";
			this.groupBox_Pulse_set_consistent_with_count.Size = new System.Drawing.Size(248, 56);
			this.groupBox_Pulse_set_consistent_with_count.TabIndex = 1;
			this.groupBox_Pulse_set_consistent_with_count.TabStop = false;
			this.groupBox_Pulse_set_consistent_with_count.Text = "カウント一致パルス設定";
			// 
			// comboBox_PulseWidth
			// 
			this.comboBox_PulseWidth.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.comboBox_PulseWidth.Location = new System.Drawing.Point(120, 24);
			this.comboBox_PulseWidth.Name = "comboBox_PulseWidth";
			this.comboBox_PulseWidth.Size = new System.Drawing.Size(121, 20);
			this.comboBox_PulseWidth.TabIndex = 1;
			// 
			// label_PulseWidth
			// 
			this.label_PulseWidth.Location = new System.Drawing.Point(16, 24);
			this.label_PulseWidth.Name = "label_PulseWidth";
			this.label_PulseWidth.TabIndex = 0;
			this.label_PulseWidth.Text = "パルス長：";
			// 
			// button_OK
			// 
			this.button_OK.Location = new System.Drawing.Point(96, 424);
			this.button_OK.Name = "button_OK";
			this.button_OK.TabIndex = 2;
			this.button_OK.Text = "OK";
			this.button_OK.Click += new System.EventHandler(this.button_OK_Click);
			// 
			// button_Cancel
			// 
			this.button_Cancel.Location = new System.Drawing.Point(184, 424);
			this.button_Cancel.Name = "button_Cancel";
			this.button_Cancel.TabIndex = 3;
			this.button_Cancel.Text = "ｷｬﾝｾﾙ";
			this.button_Cancel.Click += new System.EventHandler(this.button_Cancel_Click);
			// 
			// Comp
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
			this.ClientSize = new System.Drawing.Size(280, 470);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.button_Cancel,
																		  this.button_OK,
																		  this.groupBox_Pulse_set_consistent_with_count,
																		  this.groupBox_Comparison_register_set});
			this.Location = new System.Drawing.Point(16, 16);
			this.Name = "Comp";
			this.Text = "比較設定";
			this.groupBox_Comparison_register_set.ResumeLayout(false);
			this.groupBox_Pulse_set_consistent_with_count.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void radioButton_Dec_CheckedChanged(object sender, System.EventArgs e)
		{
			//-----------------------------------
			// 画面情報を更新
			//-----------------------------------
			for (short ChNo = 0; ChNo < 8; ChNo++)
			{
				textBox_Channel[ChNo].Text = System.Convert.ToString(CompValue[ChNo]);
			}
		}

		private void radioButton_Hex_CheckedChanged(object sender, System.EventArgs e)
		{
			//-----------------------------------
			// 画面情報を更新
			//-----------------------------------
			for (short ChNo = 0; ChNo < 8; ChNo++)
			{
				textBox_Channel[ChNo].Text = System.Convert.ToString(CompValue[ChNo], 16);
			}
		}

		private void button_OK_Click(object sender, System.EventArgs e)
		{
			
			//-----------------------------------
			// 比較値設定
			//-----------------------------------
			if (radioButton_Dec.Checked == true) 
			{
				for (short ChNo = 0; ChNo < Counter.m_ChannelNum; ChNo++)
				{
					try 
					{
						CompValue[ChNo] = System.Convert.ToUInt32(textBox_Channel[ChNo].Text); 
					} 
					catch
					{
						CompValue[ChNo] = 0;
					}
				}
			}
			else 
			{
				for (short ChNo = 0; ChNo < Counter.m_ChannelNum; ChNo++)
				{
					try 
					{
						CompValue[ChNo] = System.Convert.ToUInt32(textBox_Channel[ChNo].Text, 16);
					}
					catch	
					{
						CompValue[ChNo] = 0;
					}
				}
			}

			for (short ChNo = 0; ChNo < Counter.m_ChannelNum; ChNo++)
			{
				int Ret;
				Ret = cnt.NotifyCountUp(Counter.m_Id, ChNo, 0, CompValue[ChNo], Counter.m_Handle);
				if (Ret != 0)
				{
					string csTmp;
					csTmp = string.Format("CntNotifyCountUp Return Code = {0:d5}",Ret);
					MessageBox.Show(csTmp, "エラー", MessageBoxButtons.OK);
					return;
				}
			}

			//-----------------------------------
			// パルス長設定
			//-----------------------------------
			for (short ChNo = 0; ChNo < Counter.m_ChannelNum; ChNo++)
			{
				int		Ret;
				short	PlsWidth;
				PlsWidth = System.Convert.ToInt16(comboBox_PulseWidth.Text,16);
				Ret = cnt.SetPulseWidth(Counter.m_Id, ChNo, PlsWidth);
				if (Ret != 0)
				{
					string csTmp;
					csTmp = string.Format("CntSetPulseWidth Return Code = {0:d5}",Ret);
					MessageBox.Show(csTmp, "エラー", MessageBoxButtons.OK);
					return;
				}
			}

			this.Close();
		}

		private void button_Cancel_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

	}
}
