ShowDialog wont work

  • Thread starter peter.mcclymont
  • Start date
P

peter.mcclymont

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/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
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?
 
M

microsoft.public.dotnet.framework

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/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    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();
}
 
P

peter.mcclymont

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??
 
P

peter.mcclymont

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?
 
P

peter.mcclymont

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/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
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.
 
P

peter.mcclymont

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/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
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.
 
P

peter.mcclymont

[...]
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!!
 

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