﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Globalization;
using CdioCs;

namespace Infinite
{
	/// <summary>
	/// Summary description for Condition.
	/// </summary>
	public class Condition : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.TextBox txtNumEvent;
		private System.Windows.Forms.Label label11;
		private System.Windows.Forms.Button btnCancel;
		private System.Windows.Forms.Button btnOK;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Label label9;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.TextBox txtPtnMask;
		private System.Windows.Forms.TextBox txtCmpPtn;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label10;
		private System.Windows.Forms.TextBox txtStopNum;
		private System.Windows.Forms.TextBox txtInternalClock;
		private System.Windows.Forms.ComboBox cboStopTrg;
		private System.Windows.Forms.ComboBox cboClockTrg;
		private System.Windows.Forms.Label label8;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.ComboBox cboStartTrg;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label1;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		struct StrToNum
		{													// structure of condition and parameters
			public string	Str;
			public short	Num;
			public StrToNum(string Str, short Num)
			{
				this.Str = Str;
				this.Num = Num;
			}
		};
		// Start Condition
		static StrToNum[] StartTrgTable = 
		{
			new StrToNum("Software Start",	(short)CdioConst.DIODM_START_SOFT),
			new StrToNum("External UP",		(short)CdioConst.DIODM_START_EXT_RISE),
			new StrToNum("External Down",	(short)CdioConst.DIODM_START_EXT_FALL),
			new StrToNum("Pattern",			(short)CdioConst.DIODM_START_PATTERN),
			new StrToNum("End",				0)
		};
		// Clock Condition
		static StrToNum[] ClockTrgTable = 
		{
			new StrToNum("Internal Clock",	(short)CdioConst.DIODM_CLK_CLOCK),
			new StrToNum("External Clock",	(short)CdioConst.DIODM_CLK_EXT_TRG),
			new StrToNum("Hand Shake",		(short)CdioConst.DIODM_CLK_HANDSHAKE),
			new StrToNum("End",				0)
		};
		// Stop Condition
		static StrToNum[] StopTrgTable = 
		{
			new StrToNum("Software Stop",	(short)CdioConst.DIODM_STOP_SOFT),
			new StrToNum("External UP",		(short)CdioConst.DIODM_STOP_EXT_RISE),
			new StrToNum("External Down",	(short)CdioConst.DIODM_STOP_EXT_FALL),
			new StrToNum("Number",			(short)CdioConst.DIODM_STOP_NUM),
			new StrToNum("End",				0)
		};

		Cdio				dio				= new Cdio();
		static string		szStart			= "Software Start";
		static string		szClock			= "Internal Clock";
		static string		szStop			= "Software Stop";
		static uint			Pattern			= 0x55;
		static uint			Mask			= 0xFF;
		static uint			InternalClock	= 100000;
		static uint			StopNumber		= 1000;
		public static uint	NumberOfData	= 500;

		public Condition()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.txtNumEvent = new System.Windows.Forms.TextBox();
			this.label11 = new System.Windows.Forms.Label();
			this.btnCancel = new System.Windows.Forms.Button();
			this.btnOK = new System.Windows.Forms.Button();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.label9 = new System.Windows.Forms.Label();
			this.label6 = new System.Windows.Forms.Label();
			this.txtPtnMask = new System.Windows.Forms.TextBox();
			this.txtCmpPtn = new System.Windows.Forms.TextBox();
			this.label5 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.label10 = new System.Windows.Forms.Label();
			this.txtStopNum = new System.Windows.Forms.TextBox();
			this.txtInternalClock = new System.Windows.Forms.TextBox();
			this.cboStopTrg = new System.Windows.Forms.ComboBox();
			this.cboClockTrg = new System.Windows.Forms.ComboBox();
			this.label8 = new System.Windows.Forms.Label();
			this.label7 = new System.Windows.Forms.Label();
			this.cboStartTrg = new System.Windows.Forms.ComboBox();
			this.label3 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.label1 = new System.Windows.Forms.Label();
			this.groupBox2.SuspendLayout();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.txtNumEvent,
																					this.label11});
			this.groupBox2.Location = new System.Drawing.Point(9, 168);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(463, 66);
			this.groupBox2.TabIndex = 7;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "Notification";
			// 
			// txtNumEvent
			// 
			this.txtNumEvent.Location = new System.Drawing.Point(96, 28);
			this.txtNumEvent.Name = "txtNumEvent";
			this.txtNumEvent.Size = new System.Drawing.Size(88, 20);
			this.txtNumEvent.TabIndex = 9;
			this.txtNumEvent.Text = "";
			this.txtNumEvent.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			// 
			// label11
			// 
			this.label11.Location = new System.Drawing.Point(8, 31);
			this.label11.Name = "label11";
			this.label11.Size = new System.Drawing.Size(96, 18);
			this.label11.TabIndex = 0;
			this.label11.Text = "Number of Data:";
			// 
			// btnCancel
			// 
			this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.btnCancel.Location = new System.Drawing.Point(376, 248);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new System.Drawing.Size(96, 28);
			this.btnCancel.TabIndex = 5;
			this.btnCancel.Text = "Cancel";
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			// 
			// btnOK
			// 
			this.btnOK.Location = new System.Drawing.Point(264, 248);
			this.btnOK.Name = "btnOK";
			this.btnOK.Size = new System.Drawing.Size(96, 28);
			this.btnOK.TabIndex = 4;
			this.btnOK.Text = "OK";
			this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.label9,
																					this.label6,
																					this.txtPtnMask,
																					this.txtCmpPtn,
																					this.label5,
																					this.label4,
																					this.label10,
																					this.txtStopNum,
																					this.txtInternalClock,
																					this.cboStopTrg,
																					this.cboClockTrg,
																					this.label8,
																					this.label7,
																					this.cboStartTrg,
																					this.label3,
																					this.label2,
																					this.label1});
			this.groupBox1.Location = new System.Drawing.Point(9, 3);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(463, 157);
			this.groupBox1.TabIndex = 6;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Sampling Conditions";
			// 
			// label9
			// 
			this.label9.Location = new System.Drawing.Point(432, 61);
			this.label9.Name = "label9";
			this.label9.Size = new System.Drawing.Size(24, 19);
			this.label9.TabIndex = 22;
			this.label9.Text = "Hex";
			// 
			// label6
			// 
			this.label6.Location = new System.Drawing.Point(432, 30);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(24, 19);
			this.label6.TabIndex = 21;
			this.label6.Text = "Hex";
			// 
			// txtPtnMask
			// 
			this.txtPtnMask.Location = new System.Drawing.Point(344, 60);
			this.txtPtnMask.Name = "txtPtnMask";
			this.txtPtnMask.Size = new System.Drawing.Size(88, 20);
			this.txtPtnMask.TabIndex = 6;
			this.txtPtnMask.Text = "";
			this.txtPtnMask.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			// 
			// txtCmpPtn
			// 
			this.txtCmpPtn.Location = new System.Drawing.Point(344, 29);
			this.txtCmpPtn.Name = "txtCmpPtn";
			this.txtCmpPtn.Size = new System.Drawing.Size(88, 20);
			this.txtCmpPtn.TabIndex = 5;
			this.txtCmpPtn.Text = "";
			this.txtCmpPtn.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(248, 61);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(80, 19);
			this.label5.TabIndex = 18;
			this.label5.Text = "Pattern Mask:";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(248, 30);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(104, 19);
			this.label4.TabIndex = 17;
			this.label4.Text = "Compare Pattern:";
			// 
			// label10
			// 
			this.label10.Location = new System.Drawing.Point(432, 92);
			this.label10.Name = "label10";
			this.label10.Size = new System.Drawing.Size(24, 18);
			this.label10.TabIndex = 16;
			this.label10.Text = "ns";
			// 
			// txtStopNum
			// 
			this.txtStopNum.Location = new System.Drawing.Point(344, 122);
			this.txtStopNum.Name = "txtStopNum";
			this.txtStopNum.Size = new System.Drawing.Size(88, 20);
			this.txtStopNum.TabIndex = 8;
			this.txtStopNum.Text = "";
			this.txtStopNum.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			// 
			// txtInternalClock
			// 
			this.txtInternalClock.Location = new System.Drawing.Point(344, 91);
			this.txtInternalClock.Name = "txtInternalClock";
			this.txtInternalClock.Size = new System.Drawing.Size(88, 20);
			this.txtInternalClock.TabIndex = 7;
			this.txtInternalClock.Text = "";
			this.txtInternalClock.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			// 
			// cboStopTrg
			// 
			this.cboStopTrg.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboStopTrg.Location = new System.Drawing.Point(96, 122);
			this.cboStopTrg.Name = "cboStopTrg";
			this.cboStopTrg.Size = new System.Drawing.Size(144, 21);
			this.cboStopTrg.Sorted = true;
			this.cboStopTrg.TabIndex = 4;
			this.cboStopTrg.SelectedIndexChanged += new System.EventHandler(this.cboStopTrg_SelectedIndexChanged);
			// 
			// cboClockTrg
			// 
			this.cboClockTrg.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboClockTrg.Location = new System.Drawing.Point(96, 91);
			this.cboClockTrg.Name = "cboClockTrg";
			this.cboClockTrg.Size = new System.Drawing.Size(144, 21);
			this.cboClockTrg.Sorted = true;
			this.cboClockTrg.TabIndex = 3;
			this.cboClockTrg.SelectedIndexChanged += new System.EventHandler(this.cboClockTrg_SelectedIndexChanged);
			// 
			// label8
			// 
			this.label8.Location = new System.Drawing.Point(248, 123);
			this.label8.Name = "label8";
			this.label8.Size = new System.Drawing.Size(80, 19);
			this.label8.TabIndex = 9;
			this.label8.Text = "Stop Number:";
			// 
			// label7
			// 
			this.label7.Location = new System.Drawing.Point(248, 92);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(80, 18);
			this.label7.TabIndex = 8;
			this.label7.Text = "Internal Clock:";
			// 
			// cboStartTrg
			// 
			this.cboStartTrg.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboStartTrg.Location = new System.Drawing.Point(96, 29);
			this.cboStartTrg.Name = "cboStartTrg";
			this.cboStartTrg.Size = new System.Drawing.Size(144, 21);
			this.cboStartTrg.Sorted = true;
			this.cboStartTrg.TabIndex = 2;
			this.cboStartTrg.SelectedIndexChanged += new System.EventHandler(this.cboStartTrg_SelectedIndexChanged);
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(8, 123);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(88, 19);
			this.label3.TabIndex = 2;
			this.label3.Text = "Stop Condition:";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 92);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(96, 18);
			this.label2.TabIndex = 1;
			this.label2.Text = "Clock Condition:";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 30);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(88, 19);
			this.label1.TabIndex = 0;
			this.label1.Text = "Start Condition:";
			// 
			// Condition
			// 
			this.AcceptButton = this.btnOK;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.CancelButton = this.btnCancel;
			this.ClientSize = new System.Drawing.Size(480, 286);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.groupBox2,
																		  this.btnCancel,
																		  this.btnOK,
																		  this.groupBox1});
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "Condition";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Set Infinite Sampling Conditions";
			this.Load += new System.EventHandler(this.Condition_Load);
			this.groupBox2.ResumeLayout(false);
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		//===================================================
		//	Dialog init
		//===================================================
		private void Condition_Load(object sender, System.EventArgs e)
		{
			//--------------------------------
			// Init 
			//--------------------------------
			uint	i;
			string	strTmp;

			for (i = 0; string.Compare(StartTrgTable[i].Str, "End") != 0; i++) 
			{
				cboStartTrg.Items.Add(StartTrgTable[i].Str);
			}
			for (i = 0; string.Compare(ClockTrgTable[i].Str, "End") != 0; i++) 
			{
				cboClockTrg.Items.Add(ClockTrgTable[i].Str);
			}
			for (i = 0; string.Compare(StopTrgTable[i].Str, "End") != 0; i++) 
			{
				cboStopTrg.Items.Add(StopTrgTable[i].Str);
			}

			cboStartTrg.Text = szStart;
			cboClockTrg.Text = szClock;
			cboStopTrg.Text = szStop;

			strTmp = string.Format("{0:X}", Pattern);
			txtCmpPtn.Text = strTmp;
			strTmp = string.Format("{0:X}", Mask);
			txtPtnMask.Text = strTmp;
			strTmp = string.Format("{0:d}", InternalClock);
			txtInternalClock.Text = strTmp;
			strTmp = string.Format("{0:d}", StopNumber);
			txtStopNum.Text = strTmp;
			strTmp = string.Format("{0:d}", NumberOfData);
			txtNumEvent.Text = strTmp;

			cboStartTrg_SelectedIndexChanged(cboStartTrg, new System.EventArgs());
			cboClockTrg_SelectedIndexChanged(cboClockTrg, new System.EventArgs());
			cboStopTrg_SelectedIndexChanged(cboStopTrg, new System.EventArgs());
		}

		//===================================================
		//	Modify start condition 
		//===================================================
		private void cboStartTrg_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			string	szBuff = new string(' ', 100);
			//--------------------------------
			// Enable/disable Pattern and Mask
			//--------------------------------
			szBuff = cboStartTrg.Text;
			if (string.Compare(szBuff, "Pattern") == 0) 
			{
				txtCmpPtn.Enabled = true;
				txtPtnMask.Enabled = true;
			} 
			else 
			{
				txtCmpPtn.Enabled = false;
				txtPtnMask.Enabled = false;
			}	
		}

		//===================================================
		//	Modify clock condition 
		//===================================================
		private void cboClockTrg_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			string	szBuff = new string(' ', 100);
			//--------------------------------
			// Enable/disable Internal Clock
			//--------------------------------
			szBuff = cboClockTrg.Text;
			if (string.Compare(szBuff, "Internal Clock") == 0) 
			{
				txtInternalClock.Enabled = true;
			} 
			else 
			{
				txtInternalClock.Enabled = false;
			}
		}

		//===================================================
		//	Modify stop condition 
		//===================================================
		private void cboStopTrg_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			string	szBuff = new string(' ', 100);
			//--------------------------------
			// Enable/disable Stop Number
			//--------------------------------
			szBuff = cboStopTrg.Text;
			if (string.Compare(szBuff, "Number") == 0) 
			{
				txtStopNum.Enabled = true;
			} 
			else 
			{
				txtStopNum.Enabled = false;
			}
		}

		//===================================================
		// OK button handle
		//===================================================
		private void btnOK_Click(object sender, System.EventArgs e)
		{
			string	error_string = new string(' ', 256);
			string	szTmp = new string(' ', 100);
			short	StartTrg;
			short	ClockTrg;
			short	StopTrg;
			short	i;
			short	Direction = (short)CdioConst.DIODM_DIR_IN;
			int		ret;
			string	strMsg;

			//--------------------------------
			// Get the displayed data
			//--------------------------------
			szStart = cboStartTrg.Text;
			szClock = cboClockTrg.Text;
			szStop = cboStopTrg.Text;
			szTmp = txtCmpPtn.Text;
			try
			{
				Pattern = uint.Parse(szTmp, NumberStyles.HexNumber);
			}
			catch (System.FormatException) 
			{
				Pattern = 0x55;
			}
			szTmp = txtPtnMask.Text;
			try
			{
				Mask = uint.Parse(szTmp, NumberStyles.HexNumber);
			}
			catch (System.FormatException) 
			{
				Mask = 0xFF;
			}
			szTmp = txtInternalClock.Text;
			try
			{
				InternalClock = uint.Parse(szTmp);
			}
			catch (System.FormatException)
			{
				InternalClock = 100000;
			}
			szTmp = txtStopNum.Text;
			try
			{
				StopNumber = uint.Parse(szTmp);
			}
			catch (System.FormatException)
			{
				StopNumber = 1000;
			}
			szTmp = txtNumEvent.Text;
			try
			{
				NumberOfData = uint.Parse(szTmp);
			}
			catch (System.FormatException)
			{
				NumberOfData = 500;
			}
			//--------------------------------
			// Convert data to number 
			//--------------------------------
			StartTrg = 0;
			for (i = 0; string.Compare(StartTrgTable[i].Str, "End") != 0; i++) 
			{
				if (string.Compare(szStart, StartTrgTable[i].Str) == 0) 
				{
					StartTrg = StartTrgTable[i].Num;
					break;
				}
			}
			ClockTrg = 0;
			for (i = 0; string.Compare(ClockTrgTable[i].Str, "End") != 0; i++) 
			{
				if (string.Compare(szClock, ClockTrgTable[i].Str) == 0) 
				{
					ClockTrg = ClockTrgTable[i].Num;
					break;
				}
			}
			StopTrg = 0;
			for (i = 0; string.Compare(StopTrgTable[i].Str, "End") != 0; i++) 
			{
				if (string.Compare(szStop, StopTrgTable[i].Str) == 0) 
				{
					StopTrg = StopTrgTable[i].Num;
					break;
				}
			}
			//--------------------------------
			// Set sampling condition
			//--------------------------------
			//	Specify input/output direction (32-point input)
			ret = dio.DmSetDirection(Infinite.Id, (short)CdioConst.PI_32);
			if (ret != (int)CdioConst.DIO_ERR_SUCCESS) 
			{
				dio.GetErrorString(ret, out error_string);
				strMsg = string.Format("DioDmSetDirection ret = {0:d}: {1}", ret, error_string);
				MessageBox.Show(strMsg, this.ProductName, MessageBoxButtons.OK);
				return;
			}
			// Start alone
			ret = dio.DmSetStandAlone(Infinite.Id);
			if (ret != (int)CdioConst.DIO_ERR_SUCCESS) 
			{
				dio.GetErrorString(ret, out error_string);
				strMsg = string.Format("DioDmSetStandAlone ret = {0:d}: {1}", ret, error_string);
				MessageBox.Show(strMsg, this.ProductName, MessageBoxButtons.OK);
				return;
			}
			// Set start condition
			ret = dio.DmSetStartTrigger(Infinite.Id, Direction, StartTrg);
			if (ret != (int)CdioConst.DIO_ERR_SUCCESS) 
			{
				dio.GetErrorString(ret, out error_string);
				strMsg = string.Format("DioDmSetStartTrigger ret = {0:d}: {1}", ret, error_string);
				MessageBox.Show(strMsg, this.ProductName, MessageBoxButtons.OK);
				return;
			}
			// Set clock condition
			ret = dio.DmSetClockTrigger(Infinite.Id, Direction, ClockTrg);
			if (ret != (int)CdioConst.DIO_ERR_SUCCESS) 
			{
				dio.GetErrorString(ret, out error_string);
				strMsg = string.Format("DioDmSetClockTrigger ret = {0:d}: {1}", ret, error_string);
				MessageBox.Show(strMsg, this.ProductName, MessageBoxButtons.OK);
				return;
			}
			// Set stop condition
			ret = dio.DmSetStopTrigger(Infinite.Id, Direction, StopTrg);
			if (ret != (int)CdioConst.DIO_ERR_SUCCESS) 
			{
				dio.GetErrorString(ret, out error_string);
				strMsg = string.Format("DioDmSetStopTrigger ret = {0:d}: {1}", ret, error_string);
				MessageBox.Show(strMsg, this.ProductName, MessageBoxButtons.OK);
				return;
			}
			// pattern/mask
			ret = dio.DmSetStartPattern(Infinite.Id, Pattern, Mask);
			if (ret != (int)CdioConst.DIO_ERR_SUCCESS) 
			{
				dio.GetErrorString(ret, out error_string);
				strMsg = string.Format("DioDmSetStartPattern ret = {0:d}: {1}", ret, error_string);
				MessageBox.Show(strMsg, this.ProductName, MessageBoxButtons.OK);
				return;
			}
			// Set internal clock
			ret = dio.DmSetInternalClock(Infinite.Id, Direction, InternalClock, (short)CdioConst.DIODM_TIM_UNIT_NS);
			if (ret != (int)CdioConst.DIO_ERR_SUCCESS) 
			{
				dio.GetErrorString(ret, out error_string);
				strMsg = string.Format("DioDmSetInternalClock ret = {0:d}: {1}", ret, error_string);
				MessageBox.Show(strMsg, this.ProductName, MessageBoxButtons.OK);
				return;
			}
			// Set stop number
			ret = dio.DmSetStopNumber(Infinite.Id, Direction, StopNumber);
			if (ret != (int)CdioConst.DIO_ERR_SUCCESS) 
			{
				dio.GetErrorString(ret, out error_string);
				strMsg = string.Format("DioDmSetStopNumber ret = {0:d}: {1}", ret, error_string);
				MessageBox.Show(strMsg, this.ProductName, MessageBoxButtons.OK);
				return;
			}
			this.Close();
		}

		//===================================================
		//	Cancel button handle
		//===================================================
		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}
	}
}