[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework

ShowDialog wont work

peter.mcclymont@gmail.com

10/3/2008 3:07:00 AM

Hi All,

I have a WPF app written in .NET 3.5.

The App.xaml file has a startup method on it like this (the line
Startup=App_Startup),

<Application x:Class="iC3.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presenta...
xmlns:x="http://schemas.microsoft.com/winfx/2006/...
Startup="App_Startup">
<Application.Resources>

</Application.Resources>
</Application>

And within that startup method I am doing a number of checks to make
sure the application can start successfully. This is before the main
window is created and displayed.

A couple of those checks require custom dialogs to be displayed one
after the other to allow the user to change some settings, something
like this,

CustomDialog cd = new CustomDialog();
cd.ShowDialog();

if (cd.DialogResult == true)
{
CustomDialog2 cd2 = new CustomDialog2();
cd2.ShowDialog();
}

Well, anyway that is the gist. The problem is that CustomDialog2 does
not display. I can step over the ShowDialog method and it seems to do
nothing. If I check DialogResult after the call to cd2.ShowDialog() it
is null.

The other thing I did was swap the dialogs over, e.g. displayed
CustomDialog2 then CustomDialog. CustomDialog was the one to fail this
time, so it seems that the second dialog to display will fail. It
doesn't seem to relate to the actual dialog classes

Any ideas?
8 Answers

yeye.yang

10/3/2008 10:12:00 AM

0

On 3 oct, 05:06, "peter.mcclym...@gmail.com"
<peter.mcclym...@gmail.com> wrote:
> Hi All,
>
> I have a WPF app written in .NET 3.5.
>
> The App.xaml file has a startup method on it like this (the line
> Startup=App_Startup),
>
> <Application x:Class="iC3.App"
>     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presenta...
>     xmlns:x="http://schemas.microsoft.com/winfx/2006/...
>     Startup="App_Startup">
>     <Application.Resources>
>
>     </Application.Resources>
> </Application>
>
> And within that startup method I am doing a number of checks to make
> sure the application can start successfully. This is before the main
> window is created and displayed.
>
> A couple of those checks require custom dialogs to be displayed one
> after the other to allow the user to change some settings, something
> like this,
>
> CustomDialog cd = new CustomDialog();
> cd.ShowDialog();
>
> if (cd.DialogResult == true)
> {
>     CustomDialog2 cd2 = new CustomDialog2();
>     cd2.ShowDialog();
>
> }
>
> Well, anyway that is the gist. The problem is that CustomDialog2 does
> not display. I can step over the ShowDialog method and it seems to do
> nothing. If I check DialogResult after the call to cd2.ShowDialog() it
> is null.
>
> The other thing I did was swap the dialogs over, e.g. displayed
> CustomDialog2 then CustomDialog. CustomDialog was the one to fail this
> time, so it seems that the second dialog to display will fail. It
> doesn't seem to relate to the actual dialog classes
>
> Any ideas?

Well, I dont think DialogResult returns a bool.
Try this:
CustomDialog cd = new CustomDialog();
if (cd.ShowDialog() == DialogResult.OK)
{
CustomDialog2 cd2 = new CustomDialog2();
cd2.ShowDialog();
}

peter.mcclymont@gmail.com

10/5/2008 8:19:00 PM

0

On Oct 3, 11:11 pm, "microsoft.public.dotnet.framework"
<yeye.y...@yahoo.fr> wrote:
> On 3 oct, 05:06, "peter.mcclym...@gmail.com"
>
>
>
> <peter.mcclym...@gmail.com> wrote:
> > Hi All,
>
> > I have a WPF app written in .NET 3.5.
>
> > The App.xaml file has a startup method on it like this (the line
> > Startup=App_Startup),
>
> > <Application x:Class="iC3.App"
> >     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presenta...
> >     xmlns:x="http://schemas.microsoft.com/winfx/2006/...
> >     Startup="App_Startup">
> >     <Application.Resources>
>
> >     </Application.Resources>
> > </Application>
>
> > And within that startup method I am doing a number of checks to make
> > sure the application can start successfully. This is before the main
> > window is created and displayed.
>
> > A couple of those checks require custom dialogs to be displayed one
> > after the other to allow the user to change some settings, something
> > like this,
>
> > CustomDialog cd = new CustomDialog();
> > cd.ShowDialog();
>
> > if (cd.DialogResult == true)
> > {
> >     CustomDialog2 cd2 = new CustomDialog2();
> >     cd2.ShowDialog();
>
> > }
>
> > Well, anyway that is the gist. The problem is that CustomDialog2 does
> > not display. I can step over the ShowDialog method and it seems to do
> > nothing. If I check DialogResult after the call to cd2.ShowDialog() it
> > is null.
>
> > The other thing I did was swap the dialogs over, e.g. displayed
> > CustomDialog2 then CustomDialog. CustomDialog was the one to fail this
> > time, so it seems that the second dialog to display will fail. It
> > doesn't seem to relate to the actual dialog classes
>
> > Any ideas?
>
> Well, I dont think DialogResult returns a bool.
> Try this:
> CustomDialog cd = new CustomDialog();
> if (cd.ShowDialog() == DialogResult.OK)
> {
>     CustomDialog2 cd2 = new CustomDialog2();
>     cd2.ShowDialog();
>
> }

Well, yes and no.

DialogResult is a 'bool?' which means it can be true, or false, or
null. There is no DialogResult enum though.

DialogResult has a property called 'Value' which is the actual value
of the boolean.

It also has another property called 'HasValue' which I guess would be
false if the value is actually null.

But yes to your point I changed it to be basically this,

CustomDialog cd = new CustomDialog();
cd.ShowDialog();

if (cd.DialogResult.HasValue == true && cd.DialogResult.Value == true)
{
CustomDialog2 cd2 = new CustomDialog2();
cd2.ShowDialog();

}

My gut says though that this is not strictly necessary. It made no
difference.

Any other ideas??

peter.mcclymont@gmail.com

10/5/2008 9:19:00 PM

0

On Oct 3, 11:11 pm, "microsoft.public.dotnet.framework"
<yeye.y...@yahoo.fr> wrote:
> On 3 oct, 05:06, "peter.mcclym...@gmail.com"
>
>
>
> <peter.mcclym...@gmail.com> wrote:
> > Hi All,
>
> > I have a WPF app written in .NET 3.5.
>
> > The App.xaml file has a startup method on it like this (the line
> > Startup=App_Startup),
>
> > <Application x:Class="iC3.App"
> >     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presenta...
> >     xmlns:x="http://schemas.microsoft.com/winfx/2006/...
> >     Startup="App_Startup">
> >     <Application.Resources>
>
> >     </Application.Resources>
> > </Application>
>
> > And within that startup method I am doing a number of checks to make
> > sure the application can start successfully. This is before the main
> > window is created and displayed.
>
> > A couple of those checks require custom dialogs to be displayed one
> > after the other to allow the user to change some settings, something
> > like this,
>
> > CustomDialog cd = new CustomDialog();
> > cd.ShowDialog();
>
> > if (cd.DialogResult == true)
> > {
> >     CustomDialog2 cd2 = new CustomDialog2();
> >     cd2.ShowDialog();
>
> > }
>
> > Well, anyway that is the gist. The problem is that CustomDialog2 does
> > not display. I can step over the ShowDialog method and it seems to do
> > nothing. If I check DialogResult after the call to cd2.ShowDialog() it
> > is null.
>
> > The other thing I did was swap the dialogs over, e.g. displayed
> > CustomDialog2 then CustomDialog. CustomDialog was the one to fail this
> > time, so it seems that the second dialog to display will fail. It
> > doesn't seem to relate to the actual dialog classes
>
> > Any ideas?
>
> Well, I dont think DialogResult returns a bool.
> Try this:
> CustomDialog cd = new CustomDialog();
> if (cd.ShowDialog() == DialogResult.OK)
> {
>     CustomDialog2 cd2 = new CustomDialog2();
>     cd2.ShowDialog();
>
> }

No it returns a 'bool?'.

The bool? has 2 properties, a Value property, and a HasValue property.
But from what I have seen you can just check whether it is true or
false. It is either true, false or null, and I have stepped through
and seen that all working.

Just to be on the safe side I changed it to this,

If (cd.DialogResult.HasValue == true && cd.DialogResult.Value == true)

But that didn't make any difference.

Any other ideas?

peter.mcclymont@gmail.com

10/5/2008 10:06:00 PM

0

Hi All,

Here is a new development. I decided to create a dummy project in
visual studio to verify that it was not my code causing this issue. It
still happens in this new project. So here is the app.xaml

<Application x:Class="DialogTest.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presenta...
xmlns:x="http://schemas.microsoft.com/winfx/2006/...
Startup="Application_Startup">
<Application.Resources>

</Application.Resources>
</Application>

And here is the app.xaml.cs

public partial class App : Application
{
private void Application_Startup(object sender,
StartupEventArgs e)
{
Window1 window = new Window1();
window.Show();

Window1 window2 = new Window1();
window2.ShowDialog();

Window1 window3 = new Window1();
window3.ShowDialog();
}
}

Window1 is just a dummy window I made, which is a window with a single
button on it. The code behind has not been touched on the Window1
class.

So yeah, as soon as window2.ShowDialog() is called it just drops out
and DialogResult is null. Also the same thing happens on the call to
window3.ShowDialog();

This seems quite strange to me and I wouldn't have thought there would
be any trouble doing something like this. Also no exceptions occur or
anything like that so it doesn't give me much to go by.

Thanks.



peter.mcclymont@gmail.com

10/5/2008 10:10:00 PM

0

Hi All,

Here is a new development. I decided to create a dummy project in
visual studio to verify that it was not my code causing this issue. It
still happens in this new project. So here is the app.xaml

<Application x:Class="DialogTest.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presenta...
xmlns:x="http://schemas.microsoft.com/winfx/2006/...
Startup="Application_Startup">
<Application.Resources>

</Application.Resources>
</Application>

And here is the app.xaml.cs

public partial class App : Application
{
private void Application_Startup(object sender,
StartupEventArgs e)
{
Window1 window = new Window1();
window.ShowDialog();

Window1 window2 = new Window1();
window2.ShowDialog();

Window1 window3 = new Window1();
window3.ShowDialog();
}
}

Window1 is just a dummy window I made, which is a window with a single
button on it. The code behind has not been touched on the Window1
class.

So yeah, as soon as window2.ShowDialog() is called it just drops out
and DialogResult is null. Also the same thing happens on the call to
window3.ShowDialog();

This seems quite strange to me and I wouldn't have thought there would
be any trouble doing something like this. Also no exceptions occur or
anything like that so it doesn't give me much to go by.

Thanks.

Peter Duniho

10/5/2008 10:25:00 PM

0

On Thu, 02 Oct 2008 20:06:47 -0700, peter.mcclymont@gmail.com
<peter.mcclymont@gmail.com> wrote:

> [...]
> Well, anyway that is the gist. The problem is that CustomDialog2 does
> not display. I can step over the ShowDialog method and it seems to do
> nothing. If I check DialogResult after the call to cd2.ShowDialog() it
> is null.
>
> The other thing I did was swap the dialogs over, e.g. displayed
> CustomDialog2 then CustomDialog. CustomDialog was the one to fail this
> time, so it seems that the second dialog to display will fail. It
> doesn't seem to relate to the actual dialog classes
>
> Any ideas?

The problem here is that the Application object is automatically shutting
down your program when the first dialog is closed. Once that happens, no
other windows will be shown, including the second dialog.

If you want to write your application this way, then in your Shutdown
handler, you should set the Application.ShutdownMode property to
ShutdownMode.OnExplicitShutdown. Then, in your main window override the
OnClosed() method and call the Application.Shutdown() method so that the
application will quit when that window is closed (or alternatively, call
that method where appropriate for shutting down the application).

For example, in your Application sub-class ("App" by default):

private void App_Startup(object sender, StartupEventArgs e)
{
Window dialog = new Dialog1();

ShutdownMode = ShutdownMode.OnExplicitShutdown;

if (dialog.ShowDialog() == true)
{
Window window = new Window1();

window.Show();
}
else
{
Shutdown();
}
}

(I only show a single dialog here, but I hope the general technique is
clear)

Then in your main window class:

protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);

Application.Current.Shutdown();
}

Alternatively, you could use ShutdownMode.OnMainWindowClose. That would
allow you to avoid having to override the OnClosed() method, but you'd
have to either set the MainWindow property explicitly or make sure that
you create the main window first (and if you don't want to create the main
window until you're sure you're going to show it, you'd need to explicitly
set MainWindow to null after creating each dialog but before showing it).

Pete

Peter Duniho

10/5/2008 10:30:00 PM

0

On Sun, 05 Oct 2008 13:19:07 -0700, peter.mcclymont@gmail.com
<peter.mcclymont@gmail.com> wrote:

> [...]
>> Well, I dont think DialogResult returns a bool.
>> Try this:
>> CustomDialog cd = new CustomDialog();
>> if (cd.ShowDialog() == DialogResult.OK)
>> {
>>     CustomDialog2 cd2 = new CustomDialog2();
>>     cd2.ShowDialog();
>>
>> }
>
> Well, yes and no.
>
> DialogResult is a 'bool?' which means it can be true, or false, or
> null. There is no DialogResult enum though.

There is in the System.Windows.Forms namespace. In that namespace, there
is a DialogResult enumeration and that's what the ShowDialog() method
returns. My guess is that "yeye.yang@yahoo.fr" didn't realize you're
writing a WPF application instead of a Forms application.

> [...]
> if (cd.DialogResult.HasValue == true && cd.DialogResult.Value == true)
> {
> CustomDialog2 cd2 = new CustomDialog2();
> cd2.ShowDialog();
>
> }
>
> My gut says though that this is not strictly necessary. It made no
> difference.

You are correct that checking HasValue first is unnecessary. By comparing
to "true", you cause the compiler to implicitly cast the "true" from
"bool" to "bool?" which allows the comparison to proceed even if the
DialogResult property returns "null" (why it would ever do that, I'm not
sure, but they made it nullable for some reason so I assume it's possible
in some situation).

That's a long way of saying that there wasn't anything wrong with your
if() statement, as stepping through your code showed you (you had already
confirmed that you were calling cd2.ShowDialog()). :)

Pete

peter.mcclymont@gmail.com

10/5/2008 11:00:00 PM

0

On Oct 6, 11:25 am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
> On Thu, 02 Oct 2008 20:06:47 -0700, peter.mcclym...@gmail.com  
>
> <peter.mcclym...@gmail.com> wrote:
> > [...]
> > Well, anyway that is the gist. The problem is that CustomDialog2 does
> > not display. I can step over the ShowDialog method and it seems to do
> > nothing. If I check DialogResult after the call to cd2.ShowDialog() it
> > is null.
>
> > The other thing I did was swap the dialogs over, e.g. displayed
> > CustomDialog2 then CustomDialog. CustomDialog was the one to fail this
> > time, so it seems that the second dialog to display will fail. It
> > doesn't seem to relate to the actual dialog classes
>
> > Any ideas?
>
> The problem here is that the Application object is automatically shutting  
> down your program when the first dialog is closed.  Once that happens, no  
> other windows will be shown, including the second dialog.
>
> If you want to write your application this way, then in your Shutdown  
> handler, you should set the Application.ShutdownMode property to  
> ShutdownMode.OnExplicitShutdown.  Then, in your main window override the  
> OnClosed() method and call the Application.Shutdown() method so that the  
> application will quit when that window is closed (or alternatively, call  
> that method where appropriate for shutting down the application).
>
> For example, in your Application sub-class ("App" by default):
>
>          private void App_Startup(object sender, StartupEventArgs e)
>          {
>              Window dialog = new Dialog1();
>
>              ShutdownMode = ShutdownMode.OnExplicitShutdown;
>
>              if (dialog.ShowDialog() == true)
>              {
>                  Window window = new Window1();
>
>                  window.Show();
>              }
>              else
>              {
>                  Shutdown();
>              }
>          }
>
> (I only show a single dialog here, but I hope the general technique is  
> clear)
>
> Then in your main window class:
>
>          protected override void OnClosed(EventArgs e)
>          {
>              base.OnClosed(e);
>
>              Application.Current.Shutdown();
>          }
>
> Alternatively, you could use ShutdownMode.OnMainWindowClose.  That would  
> allow you to avoid having to override the OnClosed() method, but you'd  
> have to either set the MainWindow property explicitly or make sure that  
> you create the main window first (and if you don't want to create the main  
> window until you're sure you're going to show it, you'd need to explicitly  
> set MainWindow to null after creating each dialog but before showing it).
>
> Pete

Thank you so much, that is exactly the answer there. I am not used to
a framework having some control of that kind of stuff. It's all
working now!!