Problem with DialogResult property

  • Thread starter Thread starter Paul K
  • Start date Start date
P

Paul K

I'm designing some forms in C#, and I'm having a problem
reading the DialogResult property of a dialog. Here's
the code I use to create a new instance of the dialog:

mUserMaint = new formUserMaint();
mUserMaint.InitForm(false,mUser);
if (mUserMaint.ShowDialog(formMain.ActiveForm) ==
DialogResult.OK)
{
mUser = mUserMaint.UpdatedUser;
UpdateForm(false);
}
mUserMaint.Dispose();

Here is the code from the "submit" button on
formUserMaint:

private void btnSave_Click(object sender,
System.EventArgs e)
{
try
{
Some processing here...

this.DialogResult = DialogResult.OK;
}
Exception processing here...
}

Does anyone have any idea I am doing incorrectly? Thanks!
 
Paul K said:
I'm designing some forms in C#, and I'm having a problem
reading the DialogResult property of a dialog. Here's
the code I use to create a new instance of the dialog:

mUserMaint = new formUserMaint();
mUserMaint.InitForm(false,mUser);
if (mUserMaint.ShowDialog(formMain.ActiveForm) ==
DialogResult.OK)
{
mUser = mUserMaint.UpdatedUser;
UpdateForm(false);
}
mUserMaint.Dispose();

Here is the code from the "submit" button on
formUserMaint:

private void btnSave_Click(object sender,
System.EventArgs e)
{
try
{
Some processing here...

this.DialogResult = DialogResult.OK;
\\\
this.Close()
///

}
Exception processing here...
}

Does anyone have any idea I am doing incorrectly? Thanks!
 
Herfriend,

I tried closing the form as you suggested, but the
result is the same. Any other ideas?

Paul
 
Paul,
I'm designing some forms in C#, and I'm having a problem
reading the DialogResult property of a dialog.
What specifically is the problem you are having?

Setting Form.DialogResult in a form you use ShowDialog on will cause that
form to be closed and the value you set DialogResult to be returned by the
ShowDialog method.
Here's the code I use to create a new instance of the dialog:
The code you gave performs as I would expect with VS.NET 2003.

The only thing I normally do different than your code is within the Designer
I set the button's DialogResult property to the result I want that button to
return, rather than handling the click event and setting the form's
DialogResult. This allows you to avoid handling the click event if you do
not need to otherwise. I also set the Form's AcceptButton & CancelButton
properties, which then enable the keyboard shortcuts for Enter & Escape.

Hope this helps
Jay
 
Jay,

I tried just setting the dialogresult of the accept
button on the form to OK, but the dialogresult always
returns Cancel. I'm using VS.NET 2002, so that may be
part of the issue. I'll try poking around some and seeing
if this is a common problem. Thanks!

Paul
 
Paul,
You have until tomorrow to upgrade to VS.NET 2003.

http://msdn.microsoft.com/vstudio/howtobuy/upgrade/vstudio03/default.aspx

Are you certain you do not have a crossed event handlers someplace?

I tried you code in VS.NET 2002 and it works as expected there also.

I would put a break point on every place you reference DialogResult.Cancel
and run your program, I suspect you are entering the cancel event handler
when you didn't really mean to.

Hope this helps
Jay
 
Back
Top