Close PrintPreviewDialog when ESC is pressed - winforms

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 never 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.

Related

Avoid radcombobox suggest list from being collapsed

I have a RadDropDownList in suggestappend mode and a usercontrol as keyboard, this have buttons with ControlStyles.Selectable = false. the MouseUp event fires a SendKeys.Send(key).
The thing is when I focus in the RadDropDownList and write with my keyboard (UserControl) the suggest list appears for a milisecond and desappear.
I tried to control the popup event but it seems to have nothing to do with the suggest list.
how can i keep it opened showing suggestions until user leaves the RadDropDownList?
Here is how to access the auto complete suggest popup and cancel the closure of the popup:
radDropDownList1.DropDownListElement.AutoCompleteSuggest.DropDownList.PopupClosing += DropDownList_PopupClosing;
. . .
void DropDownList_PopupClosing(object sender, Telerik.WinControls.UI.RadPopupClosingEventArgs args)
{
args.Cancel = true;
}

Windows Close button in WPF

I am currently working on a WPF application with Caliburn framework. At the top right of the application windows, there is a windows CLOSE(X) button. I would like to catch the event for the windows CLOSE button. However, when the application window is closing, the fade out will begin regardless of any buttons which will close the application windows. Also, when the application closes, the application will ask the user whether they want to save the changes or not if there is any changes. However, I can only manage to get the EXIT button in my application to pop up the SAVE CHANGES message and then start the fade out, but this does not occur for the windows CLOSE(X) button. When I pressed the windows CLOSE(X) button, the fadeout will begin first*(Therotically, this shouldn't happen, it should show the SAVE CHANGES message first and then fadeout afterwards)*. During the fade out, the SAVE CHANGES message appears. At the end, the application crashes because the application cannot close as the message still shows in the application. Does any one know any way to work around this? Below is the code I used for the issue.
The code-behind of the wpf view - I used this to catch the event for WINDOWS CLOSE button:
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
if (!closed)
{
e.Cancel = true;
FormFadeOut.Begin();
closed = true;
}
base.OnClosing(e);
}
This code is used to close the application when the fadeout ends:
private void FormFadeOutAnimation_Completed(object sender, EventArgs e)
{
this.Close();
}
In my xaml,I used this code in order to call the function to pop up the SAVE CHANGES message when it is closing:
cal:Message.Attach="[Event Closing] = [Action CloseApp2()]"
In my view model, the following function is called by the above xaml code:
public void CloseApp2()
{
// isClosing = true;
events.Publish(new IsClosingEvent());
// events.Publish(new ClearItemsEvent());
// events.Publish(new SwitchTimerOffEvent());
// Thread.Sleep(2000);
}
When the "IsClosingEvent" event is sent, the SAVE CHANGES message will appear if there are any changes made by the user.
Does anyone have any good idea of how to solve this issue?
Thanks for any helps in advance.
Charles
Use Window.Closing event instead of
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)

How To Close Unknown Window

all,, I have a project that is built in VB.Net 2010 and WPF 4. I have an option to return to a sign-in screen, yet this option can only be accessed from a separate (quit confirmation) window. This function can be called when one of about a hundred windows are open behind the quit confirmation window.
What I want to do is close the quit confirmation window, and the other window that is open, but open the sign-in window. I know to use "Me.Close()" to close the quit confirmation window, and I know how to open the sign-in window, but I do not know how to close the other window that is open.
Help?
Couldn't you give the constructor of the quit-confirmation-window a reference to the window it should close when that option is chosen?
Edit: Two examples of how to do it:
Often you'd like to open a confirmation-dialogue when the window is being closed, so you'd create it in the Closing event-handler like this:
private void Window_Closing(object sender, CancelEventArgs e)
{
ConfirmationDialog diag = new ConfirmationDialog();
diag.ShowDialog();
switch (diag.Result)
{
...
}
}
Here your window waits for the confirmation dialogue to return (diag.Show would not wait), if that is the case you do not need any information about the window in the dialogue itself. (Result is a custom property that you can define in your dialogue if the DialogResult bool is not differentiated enough)
If your dialogue is opened whenever and you cannot wait for it to return you can create it with a reference:
private void OpenConfirmDialog()
{
ConfirmationDialog diag = new ConfirmationDialog(this);
diag.Show()
}
and in your dialogue code:
public ConfirmationDialog(Window owner)
{
Owner = owner;
}
public void OpenWelcomeScreenThing()
{
this.Close();
Owner.Close();
new WelcomeScreen().Show();
}
or something like that. Alternatively you could work with events as well.

My application loses focus when a window is closed

I have a simple two forms, one that contains a grid and a button. When I click the button, my application starts doing a long operation. While it is working, I show another form that contains a progress bar
I open it like this:
_busyWindow.ShowDialog();
And defined
public partial class BusyWindow : DevExpress.XtraEditors.XtraForm
{
public BusyWindow()
{
InitializeComponent();
}
private void BusyWindow_FormClosing(object sender, FormClosingEventArgs e)
{
this.Hide();
e.Cancel = true; // this cancels the close event.
}
}
When the operation is finished, I hide the form like this
if (ended)
_busyWindow.Hide();
It works fine. The problem is that when I close the second form (same closing code), it also closes fine but my main GUI loses the focus. For example, if I have the Firefox opened behind the application, then the Firefox gets the focus.
This only happens when I close the second form when the busyWindow has been opened, and no when it hasn't (ie, if I open the form, I close it without clicking on the button, then the main GUI doesn't lose the focus).
Do you know what is happening or where could I try to search?
There could be two possible solutions to enable you to keep focus on your main window:
//Edited: Main Window in the below example would be the window with Grid and Button.
Since you are showing the busy window via ShowDialog() try setting the owner of the window by this: _busyWindow.ShowDialog(this);. I had earlier faced a similar problem and this worked for me. Since you specify the owner of the busyWindow, when it closes it would put the focus back on its owner,i.e. your main window
In case the above technique doesnt work (it should, as it worked for me), you could try to pass the reference of the main window to the busyWindow and then on its close set the focus of the main window. Sample:
_busyWindow.MyMainWindow = this; //MyMainWindow references mainWindow of your app
_busyWindow.ShowDialog();
And the following at the FormClosing of busyWindow:
private void BusyWindow_FormClosing(object sender, FormClosingEventArgs e)
{
this.Hide();
e.Cancel = true; // this cancels the close event.
MainWindow.Focus();
}
See if it works. The first solution should work.
Hope it helps.
Thanks & Happy Windowing!
Just set child's window Owner = null before closing it

Disable backspace in wpf

I am working in a WPF application, using C#.net
I want to know, is there any way to disable Backspace button on a particular xaml page.
I want to prevent user from using the Backspace button on this particular xaml page. Even if the user presses the Backspace button, no effect should take place.
Thanks
If you want to prevent backspace going back up the navigation history in a WPF Frame, including special "Back" hardware buttons, use:
NavigationCommands.BrowseBack.InputGestures.Clear();
NavigationCommands.BrowseForward.InputGestures.Clear();
You'll need to catch the onKeyDown event and set handled to true for backspace.
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Back)
{
e.Handled = true;
}
}
So I preferred the approach by sipwiz because I did not want to disable all keyboard shortcut (I still want to use ALT-Left etc just not the Backspace).
For me using a WPF NavigationWindow, overriding the OnKeyDown method did not work at all, the window still navigated back when I pressed the Backspace key. Overriding the OnPreviewKeyDown did seem to work to start with but then I ran into problems when I needed the Backspace key to work with textboxes.
So I took what I learned from the approach by Ed Andersen and I added the following code to my NavigationWindow constructor:
KeyGesture backKeyGesture = null;
foreach(var gesture in NavigationCommands.BrowseBack.InputGestures)
{
KeyGesture keyGesture = gesture as KeyGesture;
if((keyGesture != null) &&
(keyGesture.Key == Key.Back) &&
(keyGesture.Modifiers == ModifierKeys.None))
{
backKeyGesture = keyGesture;
}
}
if (backKeyGesture != null)
{
NavigationCommands.BrowseBack.InputGestures.Remove(backKeyGesture);
}

Resources