WinForms modal windows alt+tab problem - winforms

Suppose multiple Modal Windows shown above each other.
All of those have ShowInTaskbar = false, which means that in the TaskBar you only see the MainForm and all Modal Windows are hidden.
Now you press ALT+ TAB and the most upper modal Windows disappears. But you cannot get it back in front.
How should be this done correctly in your opinion?

If a modal window is getting stuck behind the main form, it sounds like you are not setting its owner. When you call showDialog(), you need to pass in the main form like this:
modalWin.showDialog(mainForm);
Any time you call showDialog(), and your program has another form that should be underneath, it is best to pass it as the owner. If you show modal window when there is already a modal window up, then pass the first modal window as the owner.

OK Just to complete it:
This is how to set an Owner to be a Winform for a Winform:
form.ShowDialog(ownerInstance);
This is how to set an Owner to be a Winform for a WPF Window:
MyWpfDialog dialog = new MyWpfDialog();
new System.Windows.Interop.WindowInteropHelper(dialog).Owner = ownerInstance.Handle;
dialog.ShowDialog();
This is how to set an Ownder to be a Wpf Window for a Wpf Window:
.Owner = Window.GetWindow(ownerInstance)

Related

WPF - Focus second window by clicking a button

I'm creating a WPF application and I have 3 Windows (MainWindow, Window2 and Window3). When I start the project Window2 loads right after my MainWindow is loaded. And when I click a Button, Window3 opens
Window3 w3 = new Window3(this, this.window2);
w3.ShowDialog();
but I'm not able to do things (click controls etc.) in Window2, I know that ShowDialog() method is disabling other open Windows but is there a possibility to access using those controls in Window2. How could I get access to Window2's functionalities when Window3 is open?
PS.(Sorry for asking a stupid question but I'm beginner in WPF)!
You can access System.Windows.Application static class's Windows collection to have list of all currently opened windows. Then you can iterate the collection and find your window by type or some other criteria. One way would be:
var window3 = Application.Current.Windows.OfType<Window3>().FirstOrDefault();
Edit:
In comments to the question you stated that you were opening window using w3.Show() method. That opens window in non modal way. While after editing the question w3.ShowDialog() opens the window in modal way.
You can learn more about modal windows at MSDN
A modal dialog box is displayed by a function when the function needs additional data from a user to continue. Because the function depends on the modal dialog box to gather data, the modal dialog box also prevents a user from activating other windows in the application while it remains open.
So to be able to switch focus between windows they should be non modal and opened using Show method instead of ShowDialog.

Displaying a modal dialog (ChildWindow) from a user control

I have a user control that has a Grid as a main container. This control needs to display a modal dialog. However, when I show the dialog (implements ChildWindow) from the control, nothing happens, no errors and no dialog.
MyDialog dialog = new MyDialog();
dialog.Show();
If ChildWindow is something that can only be displayed from the main page and not user control, what's my alternative?
Solved the problem by creating a user control and putting it in the same grid location as the original control with Visibility:Collapsed. Every time I'd need a modal dialog, I'd disable the user control that is visible and set Visibility:Visible on the modal dialog user control.
This blog post helped a great deal: Silverlight Tutorial Part 6: Using User Controls to Implement Master/Detail Scenarios

When Modal Dialog is shown in WPF and a user clicks on the parent window, make the modal dialog flash, blink or shake

New Thought, Maybe I am looking at this totally incorrectly. So Here is exactly what I am trying to do in case there is another option I am not aware of.
I have a WPF app, the main window shows a smaller dialog window using ShowDialog(), when a user clicks on the parent window that showed the dialog, I need to make the dialog window, flash, shake or blink.
AresAvatar posted a link that looks like it might be able to use, but is there another option I am not aware of?
My original Question.
Mouse click event when Modal window's parent is clicked in WPF app?
I have a wpf app that shows a modal window using ShowDialog().
I would like to fire an event when the user tries to click the parent window that is now disabled.
Is it possible on the parent to receive a click event when it has shown a modal window?
When I attempted this using an interaction trigger, the events never fired on parent window.
Otherwise, what suggestions / options are there.
Thanks
No WPF events are sent under these conditions. The only Windows message I can see that gets sent is WM_WINDOWPOSCHANGING. You could check for that message, and check if the window was disabled when it occurred. Here's a good article on checking WM_WINDOWPOSCHANGING.
Edit: that link seems to be dead. Here's an example on StackOverflow of checking window messages.
I know this is an old question but I'll post my solution in case any one needs it.
Set the dialog.owner prior to calling ShowDialog().
var dialog = new DialogWindow();
dialog.owner = MainWindow;
dialog.ShowDialog();
The result is that clicking on the main window, brings the dialog window to the front and makes the dialog window flash.

WPF window in front of another WPF window

I have a WPF-window, in this you can click a button to see a new window. Now I want to disable, that you can click the main-window. It should be like a MessageBox.
You want to use dialog.ShowDialog().
Modeless dialogs (via dialog.Show) are ones that you can interact with the background window. Modal dialogs (via dialog.ShowDialog() are ones that don't allow you to interact with the parent window.
You're looking for Window.ShowDialog()
Try this:
dialogYouNeedToShow.Owner = App.Current.MainWindow;
dialogYouNeedToShow.ShowDialog();
ShowDialog will always show your dialog as a modal one, so you won't have access to background form.
Do you mean
MessageBox.Show()
or
myWindowName.ShowDialog()
?

Popup window and context menu

I am using the ToolStripDropDown to host the user control as the pop-up window. The problem is when a context menu strip is displayed from within this pop-up window, the pop-up itself closes in the moment the context menu opens.
I have tried to subclass the ContextMenuStrip and added WS_EX_NOACTIVATE to CreateParams but nothing changed. First I thought that there is no way to do this since it is common behavior but then I tried to put a TextBox class onto the pop-up user control and invoke the Edit control context menu - and the parent pop-up window did not close.
What am I missing?
Had a similary Problem. On my UserControll was a toolstrip. When I pressed the toolsstripdropdownbutton the dropdown was shown but the popup disapeared.
The reason was that popup.Autoclose was true. After Setting to false the Popup is not closed any more.
ToolStripDropDown popup = new ToolStripDropDown();
popup.AutoClose = false; //Set to FALSE
popup.Margin = Padding.Empty;
popup.Padding = Padding.Empty;
ToolStripControlHost host = new ToolStripControlHost(userControl1);
host.Margin = Padding.Empty;
host.Padding = Padding.Empty;
popup.Items.Add(host);
popup.Show(button1, new Point(100,100));
Actual Solution should be the one in Martin's final comment:
Use ContextMenu Instead of ContextMenuStrip
That one worked for me, and the ToolStripDropDown no longer closes by itself when right clicking one of its content controls, like it should. We still need it to AutoClose, disabling AutoClose on ToolStripDropDown will do bad things, it is supposed to close on losing focus. Example: open any other app window, and the ToolStripDropDown will continue to appear on top

Resources