Reputation: 622
Yet another question about this issue. Trust that I have investigated thoroughly, but still not found a solution, which fits my application.
Introduction: My application consists of a main form and a couple of forms, which are shown as modal when needed. Then I have a "Screen dump" form, which is a small form with a button. When this is pressed a screen dump is made of the screen.
All of the above forms are displayed by pressing corresponding buttons on the main form.
What I want: When displayed, the screen dump form should always be on top of all other windows of my application (also windows shown with ShowModal()). It is acceptable if this automatically means on top of all other programs as well.
What I have tried: I am pretty sure I have to open the form in a separate thread, in order to keep it operatable even when other forms are shown modal. This I have tried as follows:
private void btnScreenDump_MouseDown(object sender, MouseEventArgs e)
{
Thread thread = new Thread(ThreadProc);
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}
private void ThreadProc()
{
using (FormScreenDump frmScreenDump1 = new FormScreenDump())
{
frmScreenDump1.TopMost = true;
Application.Run(frmScreenDump1);
}
}
What is the problem/question? Issue 1: The form frmScreenDump1 is not topmost at all. Whenever any other forms are clicked in my application the form frmScreenDump1 goes behind the application. Why is the form not topmost when frmScreenDump1.TopMost = true;, and what must I change to get this behavior?
Issue 2: I want the form frmScreenDump1 to close automatically whenever the main form is closed. How should I do this when this form is run on a separate thread?
Any help is highly appreciated!
Upvotes: 2
Views: 3317
Reputation: 18310
You shouldn't open forms in other threads. All UI related code (and objects) must be on the UI thread, or unexpected things may happen. This is probably why your form is not top-most even though you've set it to be.
If you want to show other forms as modal while leaving this form active, then don't show the other forms as modal. Instead have every new form change the parent/owner form's Enabled
property whenever it opens/closes.
Alternatively, put your form in a separate application and start it as a new process from your main app.
Upvotes: 3
Reputation: 469
Form.TopMost
, according to users on other TopMost-related questions, is working for all forms inside an application, but you're having two applications [Which is wrong, i misread].
So you're building a screenshot function that is supposed to work if a form is opened as a dialog. That's why the thread and the extra application for the new form. I had the same requirement once and i solved it by having my screenshot function not react to it's own click event, but instead i used a mousehook to track when the user clicks at the place where the button is located. Warning: mouse-hooks are leading to heavy lagging when starting the app in VS debug mode.
Upvotes: -1