Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

the use of DialogResult question.

Posted on 2008-11-05
5
Medium Priority
?
623 Views
Last Modified: 2012-05-05
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?  
0
Comment
Question by:muffintwinkly
  • 3
  • 2
5 Comments
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 22889073
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
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 22889101
Setting the Form.DialogResult Property to anything other then DialogResult.None will hide the form after executing the command.
0
 

Author Comment

by:muffintwinkly
ID: 22889371
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
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 200 total points
ID: 22890178
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
 

Author Closing Comment

by:muffintwinkly
ID: 31513651
Thank you very much!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses
Course of the Month21 days, 3 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question