Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# ShowDialog calling another ShowDialog

Tags:

c#

winforms

I'm not sure if this is allowed but I have a main form that calls another form with ShowDialog which attempts to call another form the same.

So, for example:

form1 -> form2.showdialog -> form3.showdialog

I am getting an error at the showdialog of form3:

var ssscu = new SSS.SssTwainSimple.MainForm();
ssscu.ShowDialog();

This is the error:

{"Form showDialog tried to set an ineligible form as its owner. Forms cannot own themselves or their owners.\r\nParameter name: owner"}

I'm not sure I understand what the problem is, am I not allowed to call a showdialog from a form that is a dialog?

Thanks, Eroc

like image 657
ErocM Avatar asked Nov 02 '10 18:11

ErocM


3 Answers

am I not allowed to call a showdialog from a form that is a dialog?

You certainly are allowed, this ought to work.

But I do think it is strange that you create a dialog-form called MainForm.

I expect it is something caused by other stuff happening in events. What does mainForm do in it's constructor/Load-event ?

like image 83
Henk Holterman Avatar answered Oct 20 '22 22:10

Henk Holterman


It is a very unusual exception. The form already has an owner when it got created by the constructor. You didn't use the ShowDialog(owner) override so Windows Forms has to find an owner for itself. It finds the exact same form back. That's technically possible, but you'd have to write some fairly odd code. To diagnose this, add this code to the form:

    protected override void OnHandleCreated(EventArgs e) {
        base.OnHandleCreated(e);
    }

And set a breakpoint on it. If my guess is correct, this will break before the ShowDialog call. Look at the call stack to see what statement is getting that window created.

like image 41
Hans Passant Avatar answered Oct 20 '22 21:10

Hans Passant


Try replace .ShowDialog(); with .ShowDialog(this);

Edit: I have not tried myself, but this is where I would start looking.

like image 35
Albin Sunnanbo Avatar answered Oct 20 '22 21:10

Albin Sunnanbo