简体   繁体   中英

Owner of Form which is not inside Application.Run

I'm trying to show a Form before an application's start and get its DialogResult , so I'm just creating it and using ShowDialog (because Application.Run 's return value is void).

What I'm worried about is that it might get 'hijacked' by mistake by some other Form that might be shown at the time. Not by this application, obviously. See What is the meaning of Form.Show(null)? that it's not advisable to use the parameterless overload of ShowDialog .

I have tested and seen that the Form's Owner property was null. But will it always be so? Or should I create a Form and use that as the Owner without showing it? That seems a strange solution but logically it should avoid any problem. Or will that introduce new ones?

Not by this application, obviously

This is already taken care of by Windows, it enforces a strong separation between processes and windows owned by threads. A typical choice for the owner of a dialog for example is the window returned by GetActiveWindow(). The active window is a property of a thread . Which explains for example why a MessageBox.Show() call made from a worker thread is never modal to the rest of the windows.

Making a window modal against the windows of another process is technically possible but requires lots of effort. The app would have to call AttachThreadInput(), a very unsubtle winapi function that nobody ever calls by accident. Also a great source of deadlock.

Unless you are programming in a boat near the Somali coast, there is no good reason to fear your window getting hijacked.

[STAThread]
        static void Main()
        {
            Form1 form1 = new Form1();
            //here I  suppose the form you want to show  
            Form1 form2 = new Form2();
            form2.ShowDialog(form1);  

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(form1);
        }

Basically you use ShowDialog() when there are no parents for this window. Usually this happens for the main window. If you are opening one window after another, while closing previous, then there will be multiple ShowDialog() s.

If you are showing dialog (which is also a window), then you can specify it's parent to achieve a certain behavior. To example, when alt-tabbing to that window, it's dialog will be shown in front. Think about this as making child-parent relations.

I don't know about the case, when multiple forms are the claiming same parent. But it sounds like a clear mistake, to example:

 public Form1 FormMain = new Form1();
 ...
 // show main form
 FormMain.ShowDialog();
 ...
 // somewhere in the main form - show dialog
 Form2 form2 = new Form2();
 form2.ShowDialog(FormMain);
 ...
 // somewhere in form2 - show dialog
 Form3 form3 = new Form3();
 form3.ShowDialog(FormMain); // wrong, should be form2!

This is not tested because I couldn't recreate a case where the parent is ever anything but null (ie a reference to a third-party un-managed parent), but maybe you could do something like this on your form to set the parent to null if it changes:

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
        this.ParentChanged += MyParentChanged;
    }

    public void MyParentChanged(Object sender, EventArgs e)
    {
        this.Parent = null;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM