开发者

Close PrintPreviewDialog when ESC is pressed

I'm working on a WinForms application that uses System.Windows.Forms.PrintPreviewDialog to display a Print Preview dialog. When the user presses ESC in that dialog, I'd like to close the dialog. Unfortunately, I can't figure out how to do this. I've tried to install a KeyDown/PreviewKeyDown event handler, but it neve开发者_运维百科r gets called. I also tried setting focus to the dialog (and to its PrintPreviewControl), thinking that was the issue, but that didn't help either. Does anyone have any idea how to make this work?


I ended up customizing PrintPreviewDialog and overriding its ProcessCmdKey method to close the form when the user presses ESC. This seems like the cleanest solution.

Here's the code that I wrote:

using System.Windows.Forms;

namespace MyProject.UI.Dialogs
{
  class CustomPrintPreviewDialog : PrintPreviewDialog
  {
    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
      // Close the dialog when the user presses ESC
      if (keyData == Keys.Escape)
      {
        this.Close();
        return true;
      }

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


I haven't tried this, but don't System.Windows.Formss call CancelButton when you press Esc? Try creating a dummy Cancel button which calls .Close on the form.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜