the use of DialogResult question.

Hi,
can you please explain the use of DialogResult in this code?  I have two forms.  Form1 opens Form2 as a modal dialog using a button.  For that action, I used f2.ShowDialog(); when 'open' button is clicked on Form1.  
Now, in Form2 when I click 'Save' button, the values in Form2 will be saved.

private void button1_Click(object sender, EventArgs e)
{
            // Do somthing for saving result.        
            this.DialogResult = DialogResult.OK;            
 }

But, when I add the last line(this.DialogResult = DialogResult.OK; ), Form2 is closed.  I don't understand the last line of code.  I don't know how this line of code close Form2.  What does 'DialogResult.OK' really mean in this code?  
muffintwinklyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi muffintwinkly;

You can use this property to determine how a dialog box is closed in order to  properly process the actions performed in the dialog box by the caller.

Form.DialogResult Property can be found here.
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.dialogresult.aspx

Possible values for DialogResult and there meaning can be found here
http://msdn.microsoft.com/en-us/library/system.windows.forms.dialogresult.aspx

Fernando
0
Fernando SotoRetiredCommented:
Setting the Form.DialogResult Property to anything other then DialogResult.None will hide the form after executing the command.
0
muffintwinklyAuthor Commented:
Thanks, it helped me alot already.  But, I want to know what know is what 'DialogResult.Ok' means.  I read MSDN, but still don't understand what 'The dialog box return value is OK ' means. - I know that this value is from Form2.  but, does it mean 'Form2 is opened correctly?' or something else?  I guee that 'this.DialogResult' means Form2's DialogResult.  What is 'DialogResult.OK' and why do whe this value to Form2's DialogResult?
0
Fernando SotoRetiredCommented:
H muffintwinkly;

To your question, "I know that this value is from Form2.  but, does it mean 'Form2 is opened correctly?' or something else?",

Something else. It has any meaning that the programmer want to give to it. The DialogResult.OK can have the meaning that the OK button was clicked on the Dialog form or you can give it the meaning you want as I did in the snippet below where I assigned the DialogResult.OK to the btnLogin button.

In the snippet below I show how you can use a dialog box. Note how I determined which button was click on the dialog box from Form1 and How I was able to access some of the controls in dialog box from Form1.

Fernando



Form1 the main form
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace WindowsFormsApplication19
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            LoginForm f = new LoginForm();
            DialogResult dResult = f.ShowDialog();
 
            if (dResult == DialogResult.OK)
            {
                // Because the value of DialogResult.OK was assigned to the LoginForm.DialogResult property when 
                // the btnLogin button was clicked I can detect that here because I have a reference to that form.
                // I can also access its public and internal properties here as well
                MessageBox.Show(String.Format("Username = {0} Password = {1}", f.userName, f.password), "Data From Login Form");
            }
            else
            {
                MessageBox.Show("The Cancel button was clicked on the Login Form.");
            }
        }
    }
}
 
================================================================
 
The Dialog form LoginForm
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace WindowsFormsApplication19
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
        }
 
        internal String userName = "";
        internal String password = "";
 
        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (txtUserName.Text == String.Empty || txtPassword.Text == String.Empty)
            {
                MessageBox.Show("Both Username and Password must be filled in.\nCorrect Problem or Click on Canel to exit without logging in.", "Error");
                return;
            }
 
            userName = txtUserName.Text;
            password = txtPassword.Text;
            this.DialogResult = DialogResult.OK;
        }
 
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
    }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
muffintwinklyAuthor Commented:
Thank you very much!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.