Show window modal to window in other thread - c#

I have in my application the standard UI-Thread and additional a second UI-Thread. Now, I create and show a Window in the second UI-Thread. After this, I should create and show a Dialog in the standard UI-Thread but it should be modal to the window, which was created and shown in the second UI-Thread. The reason is, that I have to create this Dialog in the standard UI-Thread, that this Dialog uses a Control which is not ThreadSafe.

You should just be able to set Owner property of the window:
newWindow.Owner = parentWindowFromOtherThread;

Related

How to check in .net if program's main form doesn't have any modal forms opened?

Let's say I have some threads running there.
They must perform some actions only if no modal forms shown over main form.
I don't want to add hack checks like setting some flag before opening some modal form. Is there any generic way?
We use this hack:
if (this.Visible && !this.CanFocus)
{
//another modal window is opened
}
But you should know, that modal window "blocks" main form execution. And you can check this only in some cases, like repainting.

How do you use a child window in Silverlight such that it is not done asynchronous?

How do you use a child window in Silverlight such that it is not done asynchronous? The way you use a child window in Silverlight (so far as I know) is to use the show method:
ChildWindow update_dlg = new Update();
update_dlg.Show();
But you cannot get the input from the dialog because the call is asynchronous and keeps on running while the child window us up.
You can't, you need to listen for the child window closing then grab the dialog result.
From MSDN page:
To get the DialogResult value from a child window, handle the Closed event in the code-behind page of the calling window. In the Closed event handler, cast the sender parameter to a ChildWindow or a derived class to access the DialogResult property.
I like to pass an Action to the child window, if the user cancels the window nothing is called, if the user accepts the window the action is executed with the parameters from the window. This way the code to run when the user accepts is still written in the calling window.
There are three ways of doing this as I see it.
1) launch a seperate thread in the main app that waits until some third party class member is switched from true to false to indicate that the child window has been closed. Then in the child window, when a shutdown or OK or Cancel is done, this third party class member is switched too false etc. (this is the way i did this)
2) have a call back function in the main program that is called by the child window.
3) kind of cheat and don't have a child window at all. Instead, in the XAML code of the main window, have a rectangle that looks like a pop up or child window that is hidden (visual set to collapse) that comes up (but really becomes visual) and all other fields in the main program are set to inactive until the right butons are puched.**

Visual C# Window Behaver

I have a form which instances a new form that I've already created using
statuswindow statwin = new statuswindow();
statwin.ShowDialog();
return statwin;
This code is in a function. The problem is when I run the function in other code the next line of code doesn't run until the newly instanced window is closed by the user, I'm guessing this is the side-effect of using ShowDialog(), is there a way to have it use the same behaver (being topmost, not being to select the other main window) while still letting the main form's code run?
Update: Although Show(); will work, I need to be able to make the window like a dialog by having it always on top of the other window and it always being the active selected window.
The ShowDialog method of showing a window shows it as a dialog, which is a blocking mechanism within C#/.net. If you want to simply display the window and not cause the running code to block until it is closed, you can use the window.Show() function.
In your code:
startwin.Show();
This will cause the startwin form to become visable to the user, and will fire the startwin.VisibleChanged event as well as the startwin.Load event to fire.
To make the new window always on top, you could set the Topmost window flag in the form properties.
To make the window run in a separate thread, first spawn the thread, then create the window from that thread.
There are also "application modal" and "system modal" Win32 window flags, but I don't know if those are exposed to WinForms or not -- go have a look on the form properties!

C#/WPF, how to make a window (created with Window.ShowDialog()) title bar blink when clicking its parent window (like MessageBox does)?

I'm trying to create a custom MessageBox by using a WPF Window that is called with ShowDialog().
So far, I've managed to implement everything, except for one thing.
As you know, when you use MessageBox.Show("text"); you cannot set the focus or click the parent window (the one that called the MessageBox). If you do try to click the parent window, the MessageBox will blink briefly in order to alert you that you must close if first.
Windows created with Window.ShowDialog();, however, do not show that behavior. In fact, while you cannot set the focus to the parent window, the child (called with ShowDialog()) will never blink briefly.
My question is, is there any way to implement that in WPF? I've been searching for an answer but I must admit, I am stumped.
Thanks everyone!
You need to set the Owner of the modal window correctly, e.g. using the following code from within the owning window:
Window win = new SomeModalWindow();
win.Owner = this;
win.ShowDialog();
You would have to set Owner property of the child Window to the parent Window. See the MSDN Documentation here.

Windows forms: ShowDialog - dialog doesn't show up

I am running an external process from an outlook plugin - and it doesn't show the dialog. I am using ShowDialog without parameters.
Any ideas how to show the dialog ?
The process doesn't have any form - i just want to show a dialog with some selections. If I call "MessageBox.Show" before, the dialog is shown - otherwise not.
I guess it is something like the answer of Peterchen in this thread: Form.ShowDialog() or Form.ShowDialog(this)?
BTW I don't have any owner, because this process doesn't have any "main window".
Any ideas ?
I think you would need a parent window for modal windows to work. After all, the only difference from Show() is that the child window blocks the parent. You can always create a hidden dummy window and use it as a main window but why not just use .Show() since it works ?

Categories

Resources