Windows Forms - Pass through the ShowDialog() method

O

osmarjunior

I have a sequence of commands like this:

Form1 frm = new Form1();
frm.LoadData();

Boolean confirm = (frm.ShowDialog() == DialogResult.OK);

The LoadData method loads information from database. The problem is,
when I was debugging the application, it doesn't show the form. It just
pass through the ShowDialog() method, and my flag confirm becomes
false, because the form does not appear. What can be happening?

Regards.

Junior.
 
O

osmarjunior

OK. But i need the form be Modal.

I put a comment on the LoadData method line, and I tried the Show()
method. The form appears ok. If I change Show() by ShowDialog(), the
form is closing... I put a MessageBox on FormClosed event to verify
that...

Something is closing the form, but I don't have anything on Load() or
Shown() event of that form...
 
J

Jay B. Harlow [MVP - Outlook]

Junior,
Can you post a short but complete program that demonstrates the problem:
http://www.yoda.arachsys.com/csharp/complete.html

Specifically the code in Form1 that is preventing it from being shown.

FWIW: I use Form.ShowDialog all the time & have never seen it not display a
dialog.

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


|I have a sequence of commands like this:
|
| Form1 frm = new Form1();
| frm.LoadData();
|
| Boolean confirm = (frm.ShowDialog() == DialogResult.OK);
|
| The LoadData method loads information from database. The problem is,
| when I was debugging the application, it doesn't show the form. It just
| pass through the ShowDialog() method, and my flag confirm becomes
| false, because the form does not appear. What can be happening?
|
| Regards.
|
| Junior.
|
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


osmarjunior said:
I have a sequence of commands like this:

Form1 frm = new Form1();
frm.LoadData();

Boolean confirm = (frm.ShowDialog() == DialogResult.OK);

Even as there is nothing wrong with the above code I would suggest you to
move the LoadData call to inside the constructor.

Otherwise the code should work as expected, What you do in LoadData ? Where
are you filling your controls with teh data?
Are you using Form_Load event ?

Put breakpoints in both Load & Closing events of Form1
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

This will solve nothing, Show is akin ShowDialog and the OP clearly stated
that he needs the form to be modal (as well as know the result of its
showing)
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

It does not matter, there will always be a result

From MSDN:
When a form is displayed as a modal dialog box, clicking the Close button
(the button with an X at the upper-right corner of the form) causes the form
to be hidden and the DialogResult property to be set to DialogResult.Cancel.
Unlike modeless forms, the Close method is not called by the .NET Framework
when the user clicks the close form button of a dialog box or sets the value
of the DialogResult property. Instead the form is hidden and can be shown
again without creating a new instance of the dialog box. Because a form
displayed as a dialog box is not closed, you must call the Dispose method of
the form when the form is no longer needed by your application.
 
O

osmarjunior

OK. I read the posts and thanks for the help. I didn't found any
solution 100% stable yet. The code I can't post here, because it's a
form with about 15 TextBoxes and other controls. It's too much code to
post here.

I just wanna make one question to you, guys: how do you free your
winforms from the memory? I always create a static method into the form
class to execute, like this:

public static Customer Execute()
{
FormCustomer frm = new FormCustomer();

if (frm.ShowDialog() == DialogResult.OK)
return frm.Customer; // returns the customer object for further
use
else
return null; // returns null, means the user
cancel the window
}

And when should I dispose the form, in the close event?
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

The form will be marked to be disposed as soon as the method ends.
It will be disposed the next time the GC runs.
 
J

Jay B. Harlow [MVP - Outlook]

osmarjunior,
Normally I simply wrap the entire method in a using statement:

public static Customer Execute()
{
using (FormCustomer frm = new FormCustomer())
{

if (frm.ShowDialog() == DialogResult.OK)
return frm.Customer; // returns the customer object
for further use
else
return null; // returns null, means
the user cancel the window
}
}

This ensures that the dispose method will be called when you are done with
it. It also allows the Execute method to (indirectly) access controls with
the CustomerForm.

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


|
| OK. I read the posts and thanks for the help. I didn't found any
| solution 100% stable yet. The code I can't post here, because it's a
| form with about 15 TextBoxes and other controls. It's too much code to
| post here.
|
| I just wanna make one question to you, guys: how do you free your
| winforms from the memory? I always create a static method into the form
| class to execute, like this:
|
| public static Customer Execute()
| {
| FormCustomer frm = new FormCustomer();
|
| if (frm.ShowDialog() == DialogResult.OK)
| return frm.Customer; // returns the customer object for further
| use
| else
| return null; // returns null, means the user
| cancel the window
| }
|
| And when should I dispose the form, in the close event?
|
 
V

Vincent

osmarjunior said:
I have a sequence of commands like this:

Form1 frm = new Form1();
frm.LoadData();

Boolean confirm = (frm.ShowDialog() == DialogResult.OK);

The LoadData method loads information from database. The problem is,
when I was debugging the application, it doesn't show the form. It just
pass through the ShowDialog() method, and my flag confirm becomes
false, because the form does not appear. What can be happening?

Regards.

Junior.

Hi,

Using the code:

"
Boolean confirm = (frm.ShowDIalog() == DialogResult.OK);
"

Seems to work fine in C# 2005. Haven't tried it in 2003 yet.

Is your form capable of being shown at all? Or does it work if you
don't use LoadData(), and stops working if you do use that?

Cheers,
Vincent.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top