简体   繁体   中英

returning user entered values from a custom dialog box c#

I have created a custom dialog box in C#. I have a combobox, an Ok button and a Cancel button on the dialog box form. When the user clicks the ok button, I would like to return the selectedItem from the combobox. My dialog box form is created and works. It returns the correct dialog results. I just cannot find a way to get the combobox selected item before the form is disposed of.

Make it a public property of the form:

public string SelectedItem {
    get {
        return comboBox.SelectedItem.Text;
    }
}

..then your ShowDialog call should be something like this:

if (yourDialog.ShowDialog() == DialogResult.OK) {
    var selectedItem = yourDialog.SelectedItem;
    // do stuff with it here
}

This assumes that your OK button has a DialogResult value of OK set in its properties.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM