본문 바로가기

C#

C# - PDA 프로그램 개발 업데이트

반응형

 using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Threading;

namespace JCNH_MES_PDA
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        #region Load Method
        private void Main_Load(object sender, EventArgs e)
        {
            
            this.SetGrid();
            
           
        }
        #endregion

        #region User Method
        /// <summary>
        /// 지시 -> 시작 ! 메서드  
        /// </summary>
        /// <param name="ordCD"></param>
        private void StartOrd(string ordCD)
        {
            SqlCommand Cmd = new SqlCommand();

            Cmd.Connection = new SqlConnection(Common.DBConn);

            Cmd.CommandType = CommandType.StoredProcedure;

            Cmd.CommandText = "USP_PDA_SET_MGR_SUNGUAPROCESS_START";

            Cmd.Parameters.Add(new SqlParameter("@ORDCD", ordCD));

            SqlTransaction trans = null;

            try
            {
                Cursor.Current = Cursors.WaitCursor;

                Cmd.Connection.Open();

                trans = Cmd.Connection.BeginTransaction();

                Cmd.Transaction = trans;

                Cmd.ExecuteNonQuery();

                trans.Commit();
            }
            catch (Exception ex)
            {
                trans.Rollback();

                MessageBox.Show(ex.Message);
            }
            finally
            {
                Cursor.Current = Cursors.Default;

                Cmd.Connection.Close();
            }
        }
        /// <summary>
        /// 지시 번호 DB 저장 메서드 
        /// </summary>
        /// <param name="dt"></param>
        private void SaveOrdNo(DataTable dt)
        {
            SqlCommand Cmd = new SqlCommand();

            Cmd.Connection = new SqlConnection(Common.DBConn);

            Cmd.CommandType = CommandType.StoredProcedure;

            Cmd.CommandText = "USP_PDA_SET_REG_MGR_SUNGUAPROCESSORD_ORDNO";

            SqlTransaction trans = null;

            int cnt = 0;

            try
            {
                Cmd.Connection.Open();

                trans = Cmd.Connection.BeginTransaction();

                Cmd.Transaction = trans;

                foreach (DataRow dr in dt.Rows)
                {
                    Cmd.Parameters.Clear();

                    Cmd.Parameters.Add(new SqlParameter("@SUNGUAPROCESSORDCD", Convert.ToString(dr["지시"]).Trim()));
                    Cmd.Parameters.Add(new SqlParameter("@ORDNO", ++cnt));

                    Cmd.ExecuteNonQuery();
                }

                trans.Commit();
            }
            catch (Exception ex)
            {
                trans.Rollback();

                MessageBox.Show(ex.Message);
            }
            finally
            {
                Cmd.Connection.Close();
            }
        }
        /// <summary>
        /// 그리드 세팅 메서드
        /// </summary>
        private void SetGrid()
        {
            object isComplete = false;

            if (this.Rbtn_Complete.Checked)
                isComplete = true;

            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = new SqlConnection(Common.DBConn);

            Cmd.CommandType = CommandType.StoredProcedure;

            Cmd.CommandText = "USP_PDA_GET_LIST_MGR_SUNGUAPROCESS";

            Cmd.Parameters.Add(new SqlParameter("@ISCOMPLETE", isComplete));

            DataTable dt = new DataTable();

            try
            {
                Cursor.Current = Cursors.WaitCursor;

                Cmd.Connection.Open();

                SqlDataAdapter adp = new SqlDataAdapter(Cmd);
                adp.Fill(dt);

                this.Grid_List.DataSource = dt;
                this.Txt_BarFocus();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Cmd.Connection.Close();

                Cursor.Current = Cursors.Default;
            }
        }
        #endregion

        #region Control Method
        /// <summary>
        /// 업버튼 클릭 이벤트 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_GoUp_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;

                if (!this.Rbtn_Ord.Checked)
                    return;
                
                int prevRowIndex = this.Grid_List.CurrentRowIndex - 1;

                if (prevRowIndex < 0)
                    return;

                string statusNM = Convert.ToString(this.Grid_List[this.Grid_List.CurrentRowIndex - 1, 0]).Trim();

                if (statusNM.Equals("작업"))
                    return;
                
                DataTable dt = this.Grid_List.DataSource as DataTable;

                DataRow dr = dt.NewRow();
                dr[0] = dt.Rows[this.Grid_List.CurrentRowIndex - 1][0];
                dr[1] = dt.Rows[this.Grid_List.CurrentRowIndex - 1][1];
                dr[2] = dt.Rows[this.Grid_List.CurrentRowIndex - 1][2];
                dr[3] = dt.Rows[this.Grid_List.CurrentRowIndex - 1][3];

                dt.Rows.InsertAt(dr, this.Grid_List.CurrentRowIndex + 1);

                dt.Rows.RemoveAt(this.Grid_List.CurrentRowIndex - 1);

                dt.AcceptChanges();

                this.SaveOrdNo(dt);
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
                this.Txt_BarFocus();
            }
           
        }
        /// <summary>
        /// 다운 버튼 클릭 이벤트
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_GoDown_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;

                if (!this.Rbtn_Ord.Checked)
                    return;

                string statusNM = Convert.ToString(this.Grid_List[this.Grid_List.CurrentRowIndex, 0]).Trim();

                if (statusNM.Equals("작업"))
                    return;

                DataTable dt = this.Grid_List.DataSource as DataTable;

                if (dt.Rows.Count == (this.Grid_List.CurrentRowIndex + 1))
                    return;

                DataRow dr = dt.NewRow();
                dr[0] = dt.Rows[this.Grid_List.CurrentRowIndex][0];
                dr[1] = dt.Rows[this.Grid_List.CurrentRowIndex][1];
                dr[2] = dt.Rows[this.Grid_List.CurrentRowIndex][2];
                dr[3] = dt.Rows[this.Grid_List.CurrentRowIndex][3];

                dt.Rows.InsertAt(dr, this.Grid_List.CurrentRowIndex + 2);

                dt.Rows.RemoveAt(this.Grid_List.CurrentRowIndex);

                dt.AcceptChanges();

                this.SaveOrdNo(dt);
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
                this.Txt_BarFocus();
            }
            
        }
        /// <summary>
        /// 리프레시 버튼 이벤트
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void Btn_Refresh_Click(object sender, EventArgs e)
        {
            this.SetGrid();
            this.Txt_BarFocus();
        }
        /// <summary>
        /// 지시 라디오 버튼 클릭 이벤트
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Rbtn_Ord_Click(object sender, EventArgs e)
        {
            this.SetGrid();
            this.Txt_BarFocus();
        }
        /// <summary>
        /// 완료 라디오버튼 클릭 이벤트
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Rbtn_Complete_Click(object sender, EventArgs e)
        {
            this.SetGrid();
            this.Txt_BarFocus();
        }
        /// <summary>
        /// 텍스트 박스 바코드 키다운 이벤트 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        
        private void TxtBox_Barcode_KeyDown(object sender, KeyEventArgs e)
        {
            
            if (e.KeyData != Keys.Enter)
                return;

            string ordCD = this.TxtBox_Barcode.Text.Trim();

            if (ordCD.Equals(""))
                return;

            this.SetGrid();

            DataTable dt = this.Grid_List.DataSource as DataTable;

            if (dt.Rows.Count < 1)
                return;

            string gridOrdCd = Convert.ToString(this.Grid_List[0, 1]).Trim();

            if (ordCD != gridOrdCd)
            {
                MessageBox.Show("현재 순번의 접수증 번호가 아닙니다.");

                this.TxtBox_Barcode.Text = "";

                return;
            }

            this.StartOrd(ordCD);

            this.SetGrid();

            this.Txt_BarClear();

            this.Txt_BarFocus();
        }
        /// <summary>
        /// 로그인 버튼 이벤트
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_Login_Click(object sender, EventArgs e)
        {
            SqlCommand Cmd = new SqlCommand();

            Cmd.Connection = new SqlConnection(Common.DBConn);

            Cmd.CommandType = CommandType.StoredProcedure;

            Cmd.CommandText = "USP_PDA_CHK_LOGIN";

            object loginID = this.TxtBox_ID.Text.Trim();
            object loginPwd = this.TxtBox_Pwd.Text.Trim();

            Cmd.Parameters.Add(new SqlParameter("@LOGINID", loginID));
            Cmd.Parameters.Add(new SqlParameter("@PASSWD", loginPwd));

            try
            {
                Cmd.Connection.Open();

                bool isUser = Convert.ToBoolean(Cmd.ExecuteScalar());

                if (isUser)
                {
                    this.Panel_Login.Visible = false;

                    this.Txt_BarFocus();
                }
                else
                {
                    MessageBox.Show("사용자 정보가 올바르지 않습니다.");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Cmd.Connection.Close();
            }
        }
        private void Txt_BarClear() 
        {
            Thread.Sleep(1000);
            this.TxtBox_Barcode.Text = "";
        }

        private void Txt_BarFocus()
        {
            TxtBox_Barcode.Focus();
        }
        #endregion

        private void Grid_List_Click(object sender, EventArgs e)
        {
            this.Txt_BarFocus();
        }

        private void Grid_List_DoubleClick(object sender, EventArgs e)
        {
            this.Txt_BarFocus();
        }

        private void Grid_List_GotFocus(object sender, EventArgs e)
        {
            this.Txt_BarFocus();
        }

        
    }
}

2023.03.12 - [C#] - C# - Visual Studio 2008 PDA 개발 하는 방법

반응형