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?
 
Fernando SotoConnect With a Mentor RetiredCommented:
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
 
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
 
muffintwinklyAuthor Commented:
Thank you very much!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.