CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Sep 2008
    Posts
    91

    [RESOLVED] Buttons Form DialogResult

    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.

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Buttons Form DialogResult

    How do you close the form? What do you do when the user presses the OK button?
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: Buttons Form DialogResult

    posting your code would help out a lot.

  4. #4
    Join Date
    Sep 2008
    Posts
    91

    Re: Buttons Form DialogResult

    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.

    Code:
                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.
    Last edited by admdev; December 9th, 2008 at 12:07 PM.

  5. #5
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Buttons Form DialogResult

    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().

  6. #6
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: Buttons Form DialogResult

    you have to set the form's dialog result to OK or Cancel for it to close the form.

  7. #7
    Join Date
    Sep 2008
    Posts
    91

    Re: Buttons Form DialogResult

    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.

  8. #8
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: Buttons Form DialogResult

    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.

  9. #9
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: Buttons Form DialogResult

    So you code needs to look like this
    Code:
          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;
          }

  10. #10
    Join Date
    Sep 2008
    Posts
    91

    Re: Buttons Form DialogResult

    Thanks guys for your help, but unfortunatelly, even if I do it like this

    Code:
    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.

  11. #11
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: Buttons Form DialogResult

    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.

  12. #12
    Join Date
    Sep 2008
    Posts
    91

    Re: Buttons Form DialogResult

    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)".

    Code:
                    frmMessage Mymessage = new frmMessage();
    
                    Mymessage.ShowDialog();
    
                    if (Mymessage.ShowDialog() == DialogResult.OK)
                    {
                        //Do something
                        
                    }
                    else
                    {
                        //Do something
                       
                    }
    I changed it to this code.
    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.

  13. #13
    Join Date
    Feb 2010
    Posts
    1

    Re: [RESOLVED] Buttons Form DialogResult

    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 !

  14. #14
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: [RESOLVED] Buttons Form DialogResult

    Admdev... even better is...
    Code:
    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.
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured