469,362 Members | 2,302 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,362 developers. It's quick & easy.

Why when I open this form using ShowDialog() it becomes modal..

I am using the no-arg ShowDialog() method hoping that the window would not be
modal to any other window like the other ShowDialog(IWin32Window) method
does, but when this opens it somehow becomes modal to my main window- even
though I don't see how it could even be getting a reference to my main window.

What I need is to open a new window but still have my main window available
for interaction- the way it opens now it forces focus on this new window....

The last time I tried using just the Show() method it wound up just opening
the window for a split second before it vanishes away- and I wans;t doing
anything with the DialogResult variable.

nothing fancy here... just goes something like this:

on request:
MyDialog dialog = new MyDialog();
dialog.ShowDialog();

(dialog opens but becomes modal to the window that opened it, despite not
passing in a reference to itself)

Any ideas would be greatly appreciated
Nov 16 '05 #1
5 3649
Ok well, using Show() will do what I want for one Form, but the other Form
just flickers on screen then goes away. I tried looking for what difference
causing this problem but can't find any reason... they are not complicated
dialogs at all, one has a Menu and a DataGrid, the other has a TreeView and a
Label. The one with the TreeView is the one that just won't stay open unless
I use ShowDialog, but then of course it becomes modal which is bad...

In both cases I have an global instance variable so there's no question
about the reference to the dialog going out of scope. These objects holding
reference to the dialogs are 100% active before and after these dialogs are
shown.

I am not playing aroud with DialogResult in either of the forms, there is no
Load event or anything bizarre going on in the initialization.... I'm really
stumped here
Nov 16 '05 #2

Sorry, one more update... I added a new windows form to my project, made
absolutely no changes to it so it's just a blank windows form and tried the
same thing on that. It too just flashes on screen.

My main window opens it, and it loks like this:

MyDialog dialog; // class variable

public void ShowMyDialog () {
dialog = new MyDialog();
dialog.Show();
}

simple as that... so is there somethign I am totally misunderstanding about
the way windows Forms work? I am not experienced with GUI development (which
should be clear by now!)

The kicker is that my form holding a DataGrid uses the same idea, and it
displays and stays on screen no problem...

Nov 16 '05 #3
OK I do believe the mystery is finally solved, but I'm still kind of confused.

I was looking in the wrong places- I was thinking there was something wrong
with the Forms- there isn't, there's indeed a very subtle difference in the
way the windows were instantiated.

The situation is this, I have several worker threads when my app initializes
and they all report to one 'manager'. When the manager (who is another new
thread himsself) gets word that all his workers are done, he reports to my
main window so a status can be updated and several controls enabled. This is
done through an event. When this event is triggered it calls a method on my
main app which in turn opens that dialog that flashes on screen and
disappears. So now what I'm thinking is for whatever reason- that dialog
became associated with that manager's thread, so when that thread got
disposed of it took that dialog with it.

What I don't understand is why... why would that dialog belong to the
manager thread if I passed a delegate on behalf of my main window to open
that dialog?

It looks something like this:

// constructor for my main window
public MainWindow {

// standard form init code ...

Manager mgr = new Manager();
mgr.Load += new LoadCompelteDelegate(LoadComplete);
mgr.Start(); // manager spawns workers and begins work
}

// my delegate
public void LoadComplete () {
// other code...

MyDialog dialog = new MyDialog();
dialog.Show();
}

I guess one solution which I don't like is to get rid of Manager and put
that code in my main window, so I can be sure the thread opening the dialog
won't go away.

The other is some solution I found on the web for a slightly different
problem, but may be what I need. I would create another method whose sole
purpose is to open MyDialog. Then in LoadComplete, I would make a delegate
for this method and then call my main window's Invoke method to invoke that
method through it's thread.

I tried it out and it seems to work, but can you make sure I did it right?

// inside of LoadComplete :
OpenDialogDelegate openDialog = new OpenDialogDelegate(OpenDialog);
this.Invoke(openDialog, new object[] { });

anythign wrong with that?
Nov 16 '05 #4
You are creating the dialog in the LoadComplete function, as soon as the
dialog.Show returns the function competes and the dialog goes out of scope
and disappears. If you want the dialog to remain you must declare the
variable dialog at the class level so that it stays in scope when the
function ends.

"MrNobody" wrote:
OK I do believe the mystery is finally solved, but I'm still kind of confused.

I was looking in the wrong places- I was thinking there was something wrong
with the Forms- there isn't, there's indeed a very subtle difference in the
way the windows were instantiated.

The situation is this, I have several worker threads when my app initializes
and they all report to one 'manager'. When the manager (who is another new
thread himsself) gets word that all his workers are done, he reports to my
main window so a status can be updated and several controls enabled. This is
done through an event. When this event is triggered it calls a method on my
main app which in turn opens that dialog that flashes on screen and
disappears. So now what I'm thinking is for whatever reason- that dialog
became associated with that manager's thread, so when that thread got
disposed of it took that dialog with it.

What I don't understand is why... why would that dialog belong to the
manager thread if I passed a delegate on behalf of my main window to open
that dialog?

It looks something like this:

// constructor for my main window
public MainWindow {

// standard form init code ...

Manager mgr = new Manager();
mgr.Load += new LoadCompelteDelegate(LoadComplete);
mgr.Start(); // manager spawns workers and begins work
}

// my delegate
public void LoadComplete () {
// other code...

MyDialog dialog = new MyDialog();
dialog.Show();
}

I guess one solution which I don't like is to get rid of Manager and put
that code in my main window, so I can be sure the thread opening the dialog
won't go away.

The other is some solution I found on the web for a slightly different
problem, but may be what I need. I would create another method whose sole
purpose is to open MyDialog. Then in LoadComplete, I would make a delegate
for this method and then call my main window's Invoke method to invoke that
method through it's thread.

I tried it out and it seems to work, but can you make sure I did it right?

// inside of LoadComplete :
OpenDialogDelegate openDialog = new OpenDialogDelegate(OpenDialog);
this.Invoke(openDialog, new object[] { });

anythign wrong with that?

Nov 16 '05 #5
Hi tony, thanks for your reply !

I apologize- I didn;t copy the code to this message, I just rewrote parts of
it and renamed everything to something simple in an attempt not to confuse
anyone with something irrelevent, and in the process I turned the class
instance variable in my project to a local instance one in this message. So
it was indeed a class instance variable- again sorry for the confusion.

"tony lock" wrote:
You are creating the dialog in the LoadComplete function, as soon as the
dialog.Show returns the function competes and the dialog goes out of scope
and disappears. If you want the dialog to remain you must declare the
variable dialog at the class level so that it stays in scope when the
function ends.

Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by kids_pro | last post: by
14 posts views Thread by I_AM_DON_AND_YOU? | last post: by
7 posts views Thread by Boni | last post: by
5 posts views Thread by Michael.Suarez | last post: by
1 post views Thread by Frank Rizzo | last post: by
reply views Thread by =?Utf-8?B?a20=?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.