1

I created a form that has printpreview dialog and set the print button tool for show print dialog like this

1

this is a code

    public partial class frmTest : Form
        {
                 public frmTest()
                   {
                      InitializeComponent();
                   }

             private void button1_Click(object sender, EventArgs e)
              {
                 PrintPreviewDialog printPrvDlg = new PrintPreviewDialog();         
                 ToolStripButton b = new ToolStripButton();
                 b.Image = ((System.Windows.Forms.ToolStrip) 
                 (printPrvDlg.Controls[1])).ImageList.Images[0];
                 b.DisplayStyle = ToolStripItemDisplayStyle.Image;
                 b.Click += printPreview_PrintClick;
                ((ToolStrip)(printPrvDlg.Controls[1])).Items.RemoveAt(0);
                ((ToolStrip)(printPrvDlg.Controls[1])).Items.Insert(0, b);
                 printPrvDlg.ShowDialog();
              }
            private void printPreview_PrintClick(object sender, EventArgs e)
              {
                 PrintDialog printDlg = new PrintDialog();
                 printDlg.ShowDialog();  
              }

}

when the ptintpreview Dialog appear and i clicked the printer button it's ok to show printDialog but i want to use keyboard ctrl+P to show PrintDialog instead of Clicking Does Anyone feel free to help me thank you

vimuth
  • 5,064
  • 33
  • 79
  • 116
  • 1
    You don't need to replace the Button, you can attach a handler to the existing Button, e.g., `var printButton = (ToolStrip)printPrvDlg.Controls[1]; printButton.Click += printPreview_PrintClick` -- The PrintDialog is a Form, so you can activate its KeyPreview: `(printPrvDlg as Form).KeyPreview = true;`, then subscribe to the `printPrvDlg.KeyPress` event and filter the combination of keys you want to handle. Call `printPreview_PrintClick(null, null)` when the combination is `CTRL + P` (= 16) – Jimi Jul 28 '22 at 11:49
  • 1
    It's probably simpler if you handle the PrintPreviewControl's `PreviewKeyDown` event: e.g., `var preview = (PrintPreviewControl)printPrvDlg.Controls[0]; preview.PreviewKeyDown += [the Event handler];`. Simpler because it's easier to determine the combination of Key presses. – Jimi Jul 28 '22 at 12:33

0 Answers0