0

How can i do a short key for a printing action?

I already have this.

I don't know what comes inside of the if{}

    if (keyData == (Keys.Control | Keys.P))
    {
        drucken_Click();

    }


    private void drucken_Click(object sender, EventArgs e)
    {

        PrintDialog printDialog = new PrintDialog();
        PrintDocument printDocument = new PrintDocument();
        printDialog.Document = printDocument;

        printDocument.PrintPage += new PrintPageEventHandler  (PrintReceiptPage);

        DialogResult result = printDialog.ShowDialog();

        if (result == DialogResult.OK)
        {
            printDocument.Print();
        }


    }
Litisqe Kumar
  • 2,512
  • 4
  • 26
  • 40
phill
  • 63
  • 5
  • See : http://stackoverflow.com/questions/1100285/how-to-detect-the-currently-pressed-key answer at bottom – PaulF Aug 14 '15 at 11:25

3 Answers3

0

You don't use arguments in the procedure, so you can use it this way:

if (keyData == (Keys.Control | Keys.P))
{
    drucken_Click(null, EventArgs.Empty);
}

But better way is to create new parameterless function and use it. Like this:

private void Print()
{
    PrintDialog printDialog = new PrintDialog();
    PrintDocument printDocument = new PrintDocument();
    printDialog.Document = printDocument;

    printDocument.PrintPage += new PrintPageEventHandler  (PrintReceiptPage);

    DialogResult result = printDialog.ShowDialog();

    if (result == DialogResult.OK)
    {
        printDocument.Print();
    }
}
Artem Kulikov
  • 2,250
  • 19
  • 32
0

You can either pass in null values as you don't use them but this might break in the future. Preferably, move the print code into it's own method:

if (keyData == (Keys.Control | Keys.P))
{
    PrintDocument();

}


private void drucken_Click(object sender, EventArgs e)
{
    PrintDocument();
}

private void PrintDocument()
{
    PrintDialog printDialog = new PrintDialog();
    PrintDocument printDocument = new PrintDocument();
    printDialog.Document = printDocument;

    printDocument.PrintPage += new PrintPageEventHandler  (PrintReceiptPage);

    DialogResult result = printDialog.ShowDialog();

    if (result == DialogResult.OK)
    {
        printDocument.Print();
    }
}
DavidG
  • 113,891
  • 12
  • 217
  • 223
-1

Use the KeyDown event

private void Form1_Load(object sender, EventArgs e)
    {

      this.KeyDown += Form1_KeyDown;

    }  

    void Form1_KeyDown(object sender, KeyEventArgs e)
    {

        if(e.KeyCode =xxx)//check the key pressed
            //DO....
     drucken_Click(null, null);
    }
Slashy
  • 1,841
  • 3
  • 23
  • 42