How can I check the elements of a .NET dialog box when using the automatic DialogResult?

For now I have set up a custom cancel ok dialog with dropdown in C #. The ok and cancel buttons use the DialogResult property, so there is no code for it. Now I need to validate the dropdown list to check that it is not left blank before sending the dialog.

Is it possible?

+1


source to share


5 answers


From here

Double click the Closing field and implement it as follows:



private void Second_Closing(object sender, 
        System.ComponentModel.CancelEventArgs e)
{
    // When the user attempts to close the form, don't close it...
    e.Cancel = (dropdown.selecteditemindex >= 0);
}

      

+2


source


Disable the OK button until the value of the combo box is changed to a valid value.



+1


source


If you want to test something, you always need the code behind the designer. In your case, you can use the "Close" event on the form, check what you need, and if you want, set "e.Cancel = true"; - then the form will not be closed.

0


source


What I did for this was not to set the DialogResult on the OK button, but to put the code behind the button.

private void OkButton_Clicked(object sender, EventArgs e)
{
    this.DialogResult = ValueComboBox.SelectedIndex >= 0
        ? DialogResult.Ok
        : DialogResult.None;
}

      

0


source


You can use the functionality of the OK and Cancel buttons for dialogs, and then put this code in the Clicked handler for the OK button:

private void OkButton_Clicked(object sender, EventArgs e)
{
    if (!IsValid()) {
        this.DialogResult = System.Windows.Forms.DialogResult.None;
    }
}

      

In the above code IsValid()

, this is the method you should implement that validates the input fields in the dialog.

0


source







All Articles