Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get DialogResult from custom dialog

Tags:

c#

.net

winforms

I have some custom made dialog that have on it Set Button , I want when i exit from newBlockForm.ShowDialog(this); to get the dialog result if the user pressed on that button or not .

Like i would do in winforms dialog

if(MessageBox.Show("Exit?", "Close UP", 
     MessageBoxButtons.YesNo)== DialogResult.Yes)

Any idea how i do so ?

like image 225
Night Walker Avatar asked Feb 16 '11 07:02

Night Walker


2 Answers

You can use the DialogResult Property of the Button on your Dialog form and set it to DialogResult Enumeration like:

//in your dialog form
button1.DialogResult = DialogResult.OK;

then in your main form :

//Create an instance of your dialog form
Form2 testDialog = new Form2();

// Show testDialog as a modal dialog and determine if DialogResult = OK.
if (testDialog.ShowDialog(this) == DialogResult.OK)
{
   //do processing
}
else
{
   //do processing
}
like image 127
Shekhar_Pro Avatar answered Oct 08 '22 16:10

Shekhar_Pro


Map the AcceptButton property on the Form to Set button in the designer.

Or in the Set button click handler you could set some value.

        private void HandleOnSetButtonClick(object sender, EventArgs e)
        {
            this.IsSetClicked = true;
            this.Close();

            //or 
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }   

        public Boolean IsSetClicked
        {
           get;
           private set;
        }
like image 44
Vijay Sirigiri Avatar answered Oct 08 '22 17:10

Vijay Sirigiri