Click to See Complete Forum and Search --> : [RESOLVED] Buttons Form DialogResult
admdev
December 9th, 2008, 09:13 AM
Hi all,
I am using a form to display a message and get the dialogresult of the button clicked (OK or Cancel). However, for some reason, I have to click the buttons twice before the form closes. Has anybody experienced this behavior? I think it should behave the same way as a normal Messagebox, you know, you click the buttons once only, then the Messagebox closes.
Thanks in advance.
cilu
December 9th, 2008, 09:26 AM
How do you close the form? What do you do when the user presses the OK button?
eclipsed4utoo
December 9th, 2008, 10:41 AM
posting your code would help out a lot.
admdev
December 9th, 2008, 11:04 AM
When the OK is clicked, I am just executing some IF statements, which check if the user slected the proper selections before the form closes. See code below.
if (rbTest1Checked)
{
if ((this.rbCif.Checked || this.rbLdp.Checked) == false)
{
MessageBox.Show("Do this.", "Message!!!",
MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
}
else if (rbTest2.Checked)
{
if ((this.rbGen.Checked || this.rbEnco.Checked) == false)
{
MessageBox.Show("Do this.", "Message!!!",
MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
if ((this.rbCif.Checked || this.rbLdp.Checked) == false)
{
MessageBox.Show("PleaDo this.", "Message!!!",
MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
}
}
When the cancel is clicked, I am simlpy closing the form as follows.
this.Close();
Thanks.
BigEd781
December 9th, 2008, 12:12 PM
There is no code in the OK button that would close the form. I can't imagine why it would not close if the only statement the in the cancel button's click event is this.Close().
MadHatter
December 9th, 2008, 12:43 PM
you have to set the form's dialog result to OK or Cancel for it to close the form.
admdev
December 10th, 2008, 07:14 AM
MadHatter,
You mean the buttons DialogResult right? I already have them that way and the form is closing fine when I click on either button. My issue is that I have to click the buttons twice in order for the form to close.
Thanks.
MadHatter
December 10th, 2008, 07:59 AM
setting the Form's DialogResult causes it to close (not the button's dialog result). setting the form's Ok & Cancel button to a button causes the DialogResult.Cancel to be assigned when the cancel button is clicked, but not the Ok button, you have to set it explicitly inside the button's click handler.
dannystommen
December 10th, 2008, 08:27 AM
So you code needs to look like this
private void btnCancel_Click(object sender, EventArgs e) {
//don't call the Close() method, the next line will close you form
DialogResult = DialogResult.Cancel;
}
private void btnOk_Click(object sender, EventArgs e) {
//do something with user input
DialogResult = DialogResult.OK;
}
admdev
December 10th, 2008, 09:53 AM
Thanks guys for your help, but unfortunatelly, even if I do it like this
private void btnCancel_Click(object sender, EventArgs e) {
//don't call the Close() method, the next line will close you form
DialogResult = DialogResult.Cancel;
}
private void btnOk_Click(object sender, EventArgs e) {
//do something with user input
DialogResult = DialogResult.OK;
}
I am still needing to click either button twice in order for the form to close. Could this be a setting on the form itself?
Thanks.
MadHatter
December 10th, 2008, 09:55 AM
set a breakpoint on DialogResult = DialogResult.Ok and step through the processes until the form either closes or is brought back to focus. most likely there is something keeping it open, else the event handler is not wired up.
admdev
December 10th, 2008, 12:36 PM
Found the problem.
I was using the code below. So what it was doing, it was reopening the form as soon as it hit the "if (Mymessage.ShowDialog() == DialogResult.OK)".
frmMessage Mymessage = new frmMessage();
Mymessage.ShowDialog();
if (Mymessage.ShowDialog() == DialogResult.OK)
{
//Do something
}
else
{
//Do something
}
I changed it to this code.
if (costinit.DialogResult == DialogResult.OK)//This is the line I changed.
{
//MessageBox.Show("OK");
}
else
{
//MessageBox.Show("Cancel");
return;
}
Thanks for all the help provided.
Soudaniet
March 28th, 2010, 06:02 AM
i know this was posted like million years ago, but like i came across this article today, someone else might have the same problem and come looking for help sometime in the future,
well it seems that the FormClosing event doesn't take the first Ok dialog result and pops the message again tille the second Ok to exit..
it's not really about e.cancel or anything like that, it's more about the form itself, it needs two loops to get to close, here's a nice way to do it without worrying too much ;-)
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult dr = MessageBox.Show("Something", "Something", MessageBoxButtons.OKCancel);
if(dr == DialogResult.Ok)
{
this.Dispose(); //save it the trouble of looping and dispose it right a way
Application.Exit(); //don't use this.Colse() it will just keep triggering the same event
}
}
Cheers !
rliq
March 28th, 2010, 10:26 PM
Admdev... even better is...
using (frmMessage myMessage = new frmMessage())
{
if (myMessage.Equals(DialogResult.OK))
{
// do your OK code
}
else
{
// do your cancel code
}
}
Not only does this look neater, but also your Form will be disposed of after the using{} goes out of scope.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.