problem with multiple forms

G

Guest

I'm writing an app that has a main form where, upon user request, must load a
new form (which will act as a modal dialog). In the new form, the user does
a few things, and then when they are finished, they are returned to the main
form with a result (similar to a DialogResult). This will occur several
times. I've seen many posts about similar actions and I've experimented with
the answers given. However, I can't find a solution that fits my problem.

I'm using the OnLoad event for my main form to hook up events and set up the
2D mapping control. However, I have a flag that only allows the OnLoad
events to occur the very first time the Dialog Loads (not sure that's a good
idea):

private void mainForm_OnLoad(...)
{
if (firstTime)
{
...Initialize
}
}

The user presses a button on the main form, which calls a private method
which loads the secondary form like this:

private void Launch()
{
this.Text = "";
this.Hide(); // Do I need this?

DialogResult res;
// Create new form
MyDialog dlg = new MyDialog();
if ((res = dlg.ShowDialog()) == DialogResult.OK)
{
// Update Info from User Action
UpdateInfo();
this.Text = "AppName";
this.ShowDialog();
}
else if (res == DialogResult.Cancel)
{
this.Text = "AppName";
this.ShowDialog();
}
}

It works fine the first time, however, the second time it stops at the
ShowDialog call with the following error: An unhandled exception of type
'System.ArgumentException' occurred in System.Windows.Forms.dll

Anyone know why that would happen (or need more information to help figure
it out)?? :)

Thanks
-G
 
R

Ray Fink

No need to Hide the main form... this.ShowDialog() is probably the
issue. I think the following is what you're trying to accomplish.

private void Launch()
{
this.Text = "";

// Create new form
MyDialog dlg = new MyDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
// Update Info from User Action
UpdateInfo();
}
this.Text = "AppName";
}

-- Ray
 
G

Guest

That seemed to work for some cases. However, now I'm getting the same error
again. Another complication to the process is that sometimes, the user will
have to go back to the main form (to the map) and click on a location on the
map and then send that back to the second form for validation. There is a
button on the second form that the user must press to go into this new mode.
Therefore, if this button is pressed, the GUI is set in a mode that hides the
menu and only allows certain buttons to be used. After the user specifies a
location, they press a button (on the main form) that reloads the second GUI
again. Then, again, they can choose to get a new location or use the one
they have.

So, it's sort of a mess that I'll have to clean up eventually.

The problem occurs on the reload of the Main form, it's an ArgumentException
when ShowDialog is called. I also added code to the Load event that sets the
z-order of the app to be the foremost form (which I got from Peter Foot's
blog).

The code looks something like this:

// Launch new form from MainForm
private void Launch()
{
this.Text = "";

MyDialog dlg;
DialogResult res;

res = dlg.ShowDialog();

if (res == DialogResult.OK)
{
DoProcessing();
if (SpecifyMode)
ExitSpecifyMode();
}

// Enters a Specify Mode
else if (res == DialogResult.Abort)
{
EnterSpecifyMode();
}

else
{
if (SpecifyMode)
ExitSpecifyMode();
}

ShowDialog();
}

// MyDialog
private void bSpecify_Click(...)
{
// Had to use Abort cuz there is no "specify"
DialogResult = DialogResult.Abort;
}




----------------


Anyone have any ideas, or a better way to do such a task?

Thanks
-G
 
G

Guest

Bump. :)

Has anyone used the hard keys to launch a second form

I'm doing it, to initiate the same action as described in my previous post,
but when I try to reload the main form, it seems that all events and
everything are released and it won't reload correctly.

Thanks
-G
 
S

Serg Kuryata [MS]

I agree with Ray. The call to the ShowDialog() at the end of the Launch()
method causes the problem. You cannot call ShowDialog() again for a form
that has already been loaded (which is the case with your main form).

Please try the same implementation of the Launch() method as you posted
here, only without the call to this.ShowDialog(). Something like this:

private void Launch()
{
this.Text = "";

MyDialog dlg;
DialogResult res;

res = dlg.ShowDialog();

if (res == DialogResult.OK)
{
DoProcessing();
if (SpecifyMode)
ExitSpecifyMode();
}

// Enters a Specify Mode
else if (res == DialogResult.Abort)
{
EnterSpecifyMode();
}

else
{
if (SpecifyMode)
ExitSpecifyMode();
}

//!!! ShowDialog(); <- remove this line
}


Hope this helps.
Thank you,
Sergiy.

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
| Thread-Topic: problem with multiple forms
| thread-index: AcT+cqTbiiy3ZWoDQQaJhsUzZzyQuw==
| X-WBNR-Posting-Host: 209.197.59.82
| From: "=?Utf-8?B?R1MtU0U=?=" <[email protected]>
| References: <[email protected]>
<[email protected]>
| Subject: Re: problem with multiple forms
| Date: Wed, 19 Jan 2005 14:03:01 -0800
| Lines: 148
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Newsgroups: microsoft.public.dotnet.framework.compactframework
| NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.1.29
| Path: cpmsftngxa10.phx.gbl!TK2MSFTNGXA03.phx.gbl
| Xref: cpmsftngxa10.phx.gbl
microsoft.public.dotnet.framework.compactframework:69186
| X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework
|
| That seemed to work for some cases. However, now I'm getting the same
error
| again. Another complication to the process is that sometimes, the user
will
| have to go back to the main form (to the map) and click on a location on
the
| map and then send that back to the second form for validation. There is
a
| button on the second form that the user must press to go into this new
mode.
| Therefore, if this button is pressed, the GUI is set in a mode that hides
the
| menu and only allows certain buttons to be used. After the user
specifies a
| location, they press a button (on the main form) that reloads the second
GUI
| again. Then, again, they can choose to get a new location or use the one
| they have.
|
| So, it's sort of a mess that I'll have to clean up eventually.
|
| The problem occurs on the reload of the Main form, it's an
ArgumentException
| when ShowDialog is called. I also added code to the Load event that sets
the
| z-order of the app to be the foremost form (which I got from Peter Foot's
| blog).
|
| The code looks something like this:
|
| // Launch new form from MainForm
| private void Launch()
| {
| this.Text = "";
|
| MyDialog dlg;
| DialogResult res;
|
| res = dlg.ShowDialog();
|
| if (res == DialogResult.OK)
| {
| DoProcessing();
| if (SpecifyMode)
| ExitSpecifyMode();
| }
|
| // Enters a Specify Mode
| else if (res == DialogResult.Abort)
| {
| EnterSpecifyMode();
| }
|
| else
| {
| if (SpecifyMode)
| ExitSpecifyMode();
| }
|
| ShowDialog();
| }
|
| // MyDialog
| private void bSpecify_Click(...)
| {
| // Had to use Abort cuz there is no "specify"
| DialogResult = DialogResult.Abort;
| }
|
|
|
|
| ----------------
|
|
| Anyone have any ideas, or a better way to do such a task?
|
| Thanks
| -G
|
| "Ray Fink" wrote:
|
| > No need to Hide the main form... this.ShowDialog() is probably the
| > issue. I think the following is what you're trying to accomplish.
| >
| > private void Launch()
| > {
| > this.Text = "";
| >
| > // Create new form
| > MyDialog dlg = new MyDialog();
| > if (dlg.ShowDialog() == DialogResult.OK)
| > {
| > // Update Info from User Action
| > UpdateInfo();
| > }
| > this.Text = "AppName";
| > }
| >
| > -- Ray
| >
| > GS-SE wrote:
| > > I'm writing an app that has a main form where, upon user request,
must load a
| > > new form (which will act as a modal dialog). In the new form, the
user does
| > > a few things, and then when they are finished, they are returned to
the main
| > > form with a result (similar to a DialogResult). This will occur
several
| > > times. I've seen many posts about similar actions and I've
experimented with
| > > the answers given. However, I can't find a solution that fits my
problem.
| > >
| > > I'm using the OnLoad event for my main form to hook up events and set
up the
| > > 2D mapping control. However, I have a flag that only allows the
OnLoad
| > > events to occur the very first time the Dialog Loads (not sure that's
a good
| > > idea):
| > >
| > > private void mainForm_OnLoad(...)
| > > {
| > > if (firstTime)
| > > {
| > > ...Initialize
| > > }
| > > }
| > >
| > > The user presses a button on the main form, which calls a private
method
| > > which loads the secondary form like this:
| > >
| > > private void Launch()
| > > {
| > > this.Text = "";
| > > this.Hide(); // Do I need this?
| > >
| > > DialogResult res;
| > > // Create new form
| > > MyDialog dlg = new MyDialog();
| > > if ((res = dlg.ShowDialog()) == DialogResult.OK)
| > > {
| > > // Update Info from User Action
| > > UpdateInfo();
| > > this.Text = "AppName";
| > > this.ShowDialog();
| > > }
| > > else if (res == DialogResult.Cancel)
| > > {
| > > this.Text = "AppName";
| > > this.ShowDialog();
| > > }
| > > }
| > >
| > > It works fine the first time, however, the second time it stops at
the
| > > ShowDialog call with the following error: An unhandled exception of
type
| > > 'System.ArgumentException' occurred in System.Windows.Forms.dll
| > >
| > > Anyone know why that would happen (or need more information to help
figure
| > > it out)?? :)
| > >
| > > Thanks
| > > -G
| > >
| >
|
 

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