1

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?

Community
  • 1
  • 1
ispiro
  • 26,556
  • 38
  • 136
  • 291
  • Hijacked by a form in your app? How? The Application.Run did not yet run... – rene Dec 10 '13 at 12:19
  • Can you provide a scenario which you are afraid of please? – Sinatr Dec 10 '13 at 12:20
  • @rene As I said "Not by this application, obviously". – ispiro Dec 10 '13 at 12:21
  • @Sinatr I have already faced that problem when showing a modal dialog over another modal dialog in a specific case, and having a dialog close unexpectedly. I don't remember the exact details. It took me quite some time to discover the problem. Since then - I always specify the Owner. – ispiro Dec 10 '13 at 12:23
  • Can't you get hold of the desktop window and provide that as an owner? – rene Dec 10 '13 at 12:27
  • @rene a) I'm looking for the _correct_ way. That doesn't seem like it. b) That is [not simple as it might seem](http://stackoverflow.com/questions/8364758/get-handle-to-desktop-shell-window). – ispiro Dec 10 '13 at 12:30
  • I can't stand drive-by down-voting. Can whoever did it at least leave a comment as to why? Does this question "not show any research effort" or is it "unclear or not useful"? Come on people. – rory.ap Dec 10 '13 at 12:49
  • Maybe not the definitive answer but Raymond Chen has some insights http://blogs.msdn.com/b/oldnewthing/archive/2010/03/15/9978691.aspx – rene Dec 10 '13 at 12:51

4 Answers4

3

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.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Thanks.​​​​​​​​​​​​​​​​​​​​​​​​​ And :) @ the Somali coast. – ispiro Dec 10 '13 at 17:36
0
[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);
        }
BRAHIM Kamel
  • 13,492
  • 1
  • 36
  • 47
  • Yes. I have mentioned that option in my question - `Or should I create a Form and use that as the Owner without showing it?` but that's my question - is this really needed? (And might this not introduce any subtle bugs? - Because it's somewhat illogical.) – ispiro Dec 10 '13 at 12:24
  • AFAIK it's the most appropriate for your case – BRAHIM Kamel Dec 10 '13 at 12:25
0

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!
Sinatr
  • 20,892
  • 15
  • 90
  • 319
0

This is not tested because I couldn't recreate a case where the parent is ever anything but null (i.e. 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;
    }
}
rory.ap
  • 34,009
  • 10
  • 83
  • 174