Programming/C# - Window

C#/ KeyDown 이벤트 핸들러

esoog Polaris 2023. 10. 12. 11:42
반응형

키보드 키를 사용하여 프로그래밍 제어를 위해

KeyDown에 이벤트 핸들러를 설정해서 사용하고자 할 때,

Control 의 ProcessCmdKey 를 오버라이딩 하면 이벤트를 다룰 수 있다.

 

예시:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (keyData == (Keys.Shift | Keys.C)) // Shift + C 
    {
        btnPayment.PerformClick();   
    }
    else if (keyData == (Keys.Control | Keys.Enter)) // Ctrl+Enter   
    {
        btnPrintDirect.PerformClick();
    }
    else if (keyData == (Keys.Shift | Keys.K)) //   Shift + K     
    {
        btnSendtokitchen.PerformClick();
    }
    else if (keyData == (Keys.Shift | Keys.S)) //   Shift + S  
    {
        btnSaveOnly.PerformClick();
    }
    else if (keyData == (Keys.Shift | Keys.H)) //   Shift + H  
    {
        btnhold.PerformClick();
    }
    else if (keyData == (Keys.Shift | Keys.Delete)) // Shift + Del -> Suspen/clear all items
    {
        btnSuspend.PerformClick();
    }
    else if (keyData == (Keys.Shift | Keys.B)) // Shift + B Barcode Cursor
    {
        txtBarcodeReaderBox.Focus();
    }
    else if (keyData == (Keys.Shift | Keys.P)) // Press  Shift +  P for Cursor to Paid textbox 
    {
        txtPaidAmount.Focus();
    }
    else if (keyData == (Keys.Shift | Keys.Q)) // Press  Shift +  Q for Split payment
    {
        btnsplitpaidamount.Focus();
    }
    else if (keyData == (Keys.Shift | Keys.D))  // Selected item delete
    {
        try
        {
            foreach (DataGridViewRow row in dgrvSalesItemList.SelectedRows)
            {
                dgrvSalesItemList.Rows.RemoveAt(row.Index);
            }
            DiscountCalculation();
            vatcal();
            txtDiscountRate.Text = "0";
        }
        catch
        {
        }
    }
    else if (keyData == (Keys.F6)) // Increase item Qty
    {
        try
        { 

            int n = dgrvSalesItemList.CurrentCell.RowIndex;

            double Rprice = Convert.ToDouble(dgrvSalesItemList.Rows[n].Cells[1].Value);
            double Taxrate = Convert.ToDouble(dgrvSalesItemList.Rows[n].Cells[8].Value);

            int QtyInc = Convert.ToInt32(dgrvSalesItemList.Rows[n].Cells[2].Value);
            dgrvSalesItemList.Rows[n].Cells[2].Value = (QtyInc + 1);  //Qty Increase
            dgrvSalesItemList.Rows[n].Cells[3].Value = Rprice * (QtyInc + 1);   // Total price                              


            double qty = Convert.ToDouble(dgrvSalesItemList.Rows[n].Cells[2].Value);
            double disrate = Convert.ToDouble(dgrvSalesItemList.Rows[n].Cells[7].Value);

            if (disrate > 0)  // if discount has
            {
                double DisamtInc = (((Rprice * qty) * disrate) / 100.00);      // Total Discount amount of this item
                dgrvSalesItemList.Rows[n].Cells[5].Value = DisamtInc;
            }

            if (Taxrate > 0)   // // If   has tax 
            {
                // Total Tax amount  of this item  (Rprice - disamount) * taxRate / 100
                double TaxamtInc = ((((Rprice * qty) - (((Rprice * qty) * disrate) / 100.00)) * Taxrate) / 100.00);
                dgrvSalesItemList.Rows[n].Cells[6].Value = TaxamtInc;
            }
            DiscountCalculation();
            vatcal();
        }
        catch
        {
        }
    }
    else if (keyData == (Keys.F7)) // Decrease item Qty
    {
        try
        {
            int n = dgrvSalesItemList.CurrentCell.RowIndex;
            if (Convert.ToDouble(dgrvSalesItemList.Rows[n].Cells[2].Value) > 1)
            { 

                double Rprice = Convert.ToDouble(dgrvSalesItemList.Rows[n].Cells[1].Value);
                double Taxrate = Convert.ToDouble(dgrvSalesItemList.Rows[n].Cells[8].Value);

                int QtyInc = Convert.ToInt32(dgrvSalesItemList.Rows[n].Cells[2].Value);
                dgrvSalesItemList.Rows[n].Cells[2].Value = (QtyInc - 1);  //Qty Increase
                dgrvSalesItemList.Rows[n].Cells[3].Value = Rprice * (QtyInc - 1);   // Total price
          
                double qty = Convert.ToDouble(dgrvSalesItemList.Rows[n].Cells[2].Value);
                double disrate = Convert.ToDouble(dgrvSalesItemList.Rows[n].Cells[7].Value);

                if (disrate != 0)  // if discount has
                {
                    double DisamtInc = (((Rprice * qty) * disrate) / 100.00);      // Total Discount amount of this item
                    dgrvSalesItemList.Rows[n].Cells[5].Value = DisamtInc;
                }

                if (Taxrate > 0)   // // If   has tax 
                {
                    // Total Tax amount  of this item  (Rprice - disamount) * taxRate / 100
                    double TaxamtInc = ((((Rprice * qty) - (((Rprice * qty) * disrate) / 100.00)) * Taxrate) / 100.00);
                    dgrvSalesItemList.Rows[n].Cells[6].Value = TaxamtInc;
                }
                DiscountCalculation();
                vatcal();
            }

        }
        catch
        {
        }

    }

    return base.ProcessCmdKey(ref msg, keyData);
}
728x90