Get DialogResult from custom dialog
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 ?
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
}
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;
}
ShowDialog
method returns a DialogResult
by itself which you can set automatically by setting the DialogResult
of the buttons in question in your form designer.
精彩评论