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
Related
Hi I have a code to be written while window(WPF window) activate like clicking on the window or using alt/tab. The window is the child of a main form (windows app). I have used ToolWindow as the windowstyle.
It has a xamdatagrid which has to updated on activation
Problem is it fires multiple times. It should be fired once. I don not want my code to run multiple times
How to make it work. please help
From the Window.Activated Event page on MSDN:
Occurs when a window becomes the foreground window.
The Window.Activated Event is supposed to be called multiple times, so perhaps it is not the best event for you to handle. Alternatively, you could add a bool isFirstTime variable and use it to restrict your code to only being called once. Take this example:
private bool isFirstTime = true;
...
private void WindowActivated(object sender, EventArgs e)
{
if (isFirstTime)
{
isFirstTime = false;
// do something here just once
}
}
However, as (from the linked page)...
A window is activated (becomes the foreground window) when:
• The window is first opened.
• A user switches to a window by selecting it with the mouse, pressing ALT+TAB, or from Task Manager.
• A user clicks the window's taskbar button.
... you may find that this will not work for you.
I got it done.
I was using the below code
private void OnAttributeHistoryWindowActivated(object sender, EventArgs e)
{
var win = ((RoutedEventArgs)(e)).Source as AttributeHistoryWindow;
//My Code
}
The first line of code was firing back the Activated event. And it never goes to the next line of my code.
Now I used below code and it works.
private void OnAttributeHistoryWindowActivated(object sender, EventArgs e)
{
var win = sender as AttributeHistoryWindow;
//My Code
}
Now it fires once.
I've got a modal dialog box and (when user presses its OK button) I want to hide it, show another modal dialog box (MessageBox for example) and then show it back again. My problem is that when the dialog is hidden, its DialogResult gets false and of course its getting closed right after the button's handler method ends. I've even tried to set Opacity to 0 instead of Hide() but that doesn't work at all (it's still visible).
Is there a way to hide a modal dialog box for a moment without closing it?
Workaround that is working for me:
To prevent the window from being closed once you set the DialogResult, create your own DialogResult instead:
public new bool DialogResult;
Now you can still set the variable and choose Hide() instead of Close().
So all the places where DialogResult is set I add a
Hide();
So i looks like this:
DialogResult=true;
Hide();
or
DialogResult=false;
Hide();
That way I can do a new ShowDialog() again.
So if I need to reopen the window until the content is correct (if validation happens after closing), it would look something like this:
public void ShowDialog()
{
var dialog = new MyDialog();
bool ok = false;
while (!ok)
{
dialog.ShowDialog();
if (dialog.DialogResult)
{
ok = DoSomeValidation();
}
else
{
ok = true;
}
}
}
This does not deal with the result but see how to return data from a Page
PageModal is a Page
You use NavigationWindow for the modal part
public partial class MainWindow : Window
{
private PageModal pageModal = new PageModal();
public MainWindow()
{
InitializeComponent();
}
private void btnLaunchModal(object sender, RoutedEventArgs e)
{
NavigationWindow navWindow = new NavigationWindow();
navWindow.Content = pageModal;
navWindow.ShowDialog();
}
}
Ok, the opacity IS working. I just had it blocked by finished animation (with HoldEnd behavior) and I didn't knew about it. So, if anyone has the same problem and needs to hide a modal window, the Opacity = 0; is the solution.
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)
i've seen so many samples that in order to open closed window i should hide the window in closing event, but this is not fair for me if i close window in middle of my work and again open same window it showing me content where i left because i'm hiding window previously. So how can i start freshly my window after closed or hide the window.
currently i'm calling winload method which is to show fresh window after calling the show method of a hide window.
private PurgeData data=new PurgeData();
private void MenuPurgeData_Click(object sender, RoutedEventArgs e)
{
try
{
if (PurgeData == null)
{
PurgeData = new PurgeData();
PurgeData.Show();
}
else
{
PurgeData.WinLoad();
PurgeData.Show();
}
}
Thanks,
#nagaraju.
If you want the behaviour of hide/show, you should not call Window.Close() on the window at all, but hide it by calling Window.Hide(). If the user has closed it though and a close is unavoidable, you can try the following. Override OnClosing inside the Window and set e.Cancelled to true, then call .Hide(). This should allow the window to be hidden/shown even if the user closes the window.
// Disclaimer, untested!
protected override void OnClosing(CancelEventArgs e)
{
e.Cancel = true; // cancels the window close
this.Hide(); // Programmatically hides the window
}
EDIT:
Ok I've now read your question properly ;-)
So how can i start freshly my window after closed or hide the window.
When you re-show the window using the above, it will of course be the same instance as the one that was previously hidden, hence will have the same data and state. If you want completely new contents you need to create a new Window() and call Window.Show() on that. If you hide/show as above then you'll get the window back in exactly the same state before it was hidden.
Are you using the MVVM pattern in your WPF application? If so, you could try the following. By having all the data in your ViewModel and bound to by the View (ie: no business logic or data in the code behind of the Window), then you could invoke a method on the ViewModel to reset all data when the window is shown. Your bindings will refresh and the window state will be reset. Note this will only work if you have correctly followed MVVM and bound all elements on the main form to ViewModel properties (including sub controls).
Best regards,
It really depends on the structure of your app. Since you're not maintaining state, you only need to save the actual type of window that was closed. Depending on what type of window you're using, you can assign it an ID (e.g. in its Tag property) so that it can be recognized later. You can then push this ID during the Closing event in a Stack. Then, if the user reopens it, pop the stack to get the ID and check what window that corresponds to. You can then reopen that window.
Of course, Stack is just one data structure and it may or may not be suitable for you. Using a Stack means that user can reopen all the past windows they closed, but maybe you might just want the one window instead.
Edit - basic code:
//create an enum to make it easy to recognise the type of window that was open
enum WindowType { Profile, Settings };
//create a stack to hold the list of past windows
Stack<WindowType> pastWindows = new Stack<WindowType>();
//give the window type a particular id
profileWindow.Tag = WindowType.Profile;
//open the window
profileWindow.Show();
//in the closing event, if you want the user to be able to reopen this window, push it to the stack
protected override void OnClosing(CancelEventArgs e)
{
pastWindows.Push(WindowType.Profile); //or whatever type it was
base.OnClosing(e);
}
//to reopen the last window
void ReopenLastWindow()
{
WindowType lastType = pastWindows.Pop();
switch(lastType)
{
case WindowType.Profile:
profileWindow.Show();
break;
case WindowType.Settings:
settingsWindow.Show();
break;
default:
break;
}
}
this.Opacity = 0
to "close the window"
this.Opacity = 1
to "re-open it"
A remark concerning the Hide() method: from another class, the window will in fact be considered as closed and the code will continue after a ShowDialog() method usage. Using the "Opacity" property overrides the problem.
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.