0

i have a point of sale program made with C# when pressing submit button it submit the sale and print the invoice i want to make a shortcut for it so when i press a shortcut key on the keyboard it does the buttons work

here is my button Code:

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);
            }
        }
    }
}
Marc
  • 3,905
  • 4
  • 21
  • 37
ibrahim karim
  • 9
  • 1
  • 1
  • 2
  • 1
    Maybe this will help: http://stackoverflow.com/questions/400113/best-way-to-implement-keyboard-shortcuts-in-a-windows-forms-application – Szabolcs Dézsi Feb 28 '16 at 15:41
  • i saw that and i tryed it doesnt work – ibrahim karim Feb 28 '16 at 15:43
  • How does it not work? [Hans's code](http://stackoverflow.com/a/400325/366904) works *perfectly*, I have used the same method myself multiple times. The code you've shown here is irrelevant, it is just the button's click event handler. The point is getting that event handler *triggered*, which you do by adding additional code. If you can't get it going, [edit] the question with *specific* details, like the code you've tried and what exactly isn't working. – Cody Gray - on strike Feb 28 '16 at 16:14

1 Answers1

0

How about using the KeyDown event on the form? Say for example that the shortcut key is "E".

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

However this wouldn't really work with multiple keys, so instead you may want to use a system that allows you to check if a certain key is pressed instead. Say that "CTRL" + "E" is the shortcut. Use the "PresentationCore" library in order to access the "System.Windows.Input" namespace.

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);
    }
}

And to check at regular intervals to see if these keys are pressed.

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");
        }
    }
}
Sophie Coyne
  • 1,018
  • 7
  • 16