在C#中为按钮设置快捷键

时间:2016-02-28 15:40:26

标签: c# winforms

我有一个用C#制作的销售点计划 按下提交按钮时,它会提交销售并打印发票 我想为它制作一个快捷方式,所以当我按下键盘上的快捷键时 按钮是否正常工作

这是我的按钮代码:

private void btnCompleteSalesAndPrint_Click(object sender, EventArgs e)
{
    DialogResult result = MessageBox.Show("Do you want to Complete Sale and Print?\n\n -If you need any item [duplicate] (1 item  2 piece) \n -Please Increase item Quantity \n ----- by clicking + sign  ", "Yes or No", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);

    if (result == DialogResult.Yes)
    {
        if (txtPaidAmount.Text == "")
        {
            MessageBox.Show("Sorry ! you have not enough product \n  Please Purchase product or Increase Product Quantity");
            // detail_info go = new detail_info();
            // go.ShowDialog();
        }
        else
        {
            try
            {
                sales_item();

                //Save payment info into sales_payment table
                payment_item();

                // 5 % Rewards Point add to customer Account for total Payable amount 
                AddCredit();

                PrintPage mkc = new PrintPage();
                mkc.saleno = txtInvoice.Text;
                mkc.vat = txtVATRate.Text;
                mkc.dis = txtDiscountRate.Text;
                mkc.paidamt = txtPaidAmount.Text;
                mkc.subtotal = lblsubtotal.Text;
                mkc.ShowDialog();

                showincrement();
                ClearForm2();
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

如何在表单上使用KeyDown事件?比如说快捷键是" E"。

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.E)
    {
        btnCompleteSalesAndPrint_Click(sender, new EventArgs());
    }
}

然而,这对多个键无效,因此您可能希望使用一个允许您检查是否按下某个键的系统。说" CTRL" +" E"是捷径。使用" PresentationCore"库以便访问" System.Windows.Input"命名空间。

using System.Windows.Input;

//[...]

private bool ControlPressed//Returns true if the left control button or the right control button is pressed. Returns false if neither are pressed.
{
    get
    {
       return Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl);
    }
}

private bool E_Pressed//Boolean that returns true if "E" is pressed and false if it isn't.
{
    get
    {
        return Keyboard.IsKeyDown(Key.E);
    }
}

定期检查是否按下了这些键。

public Form1()
{
    InitializeComponent();
    Timer timer = new Timer();//Create new instance of Timer.
    timer.Interval = 1;
    timer.Tick += new EventHandler(timer_Tick);//Set an eventhandler to occur after each 1000th of a second.
    timer.Enabled = true;
}

private void timer_Tick(object sender, EventArgs e)
{
    if (ControlPressed && E_Pressed)
    {
        btnCompleteSalesAndPrint_Click(sender, new EventArgs());
    }
}

Codf bflow:

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

namespace hmmmhmh
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Timer timer = new Timer();
            timer.Interval = 1;
            timer.Tick += new EventHandler(timer_Tick);
            timer.Enabled = true;
        }
        private void timer_Tick(object sender, EventArgs e)
        {
            if (Control_Pressed && F_Pressed)
            {
                button1_Click(sender, e);
            }
        }
        private bool Control_Pressed
        {
            get
            {
                return Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl);
            }
        }
        private bool F_Pressed
        {
            get
            {
                return Keyboard.IsKeyDown(Key.E);
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("hlhlh");
        }
    }
}