I have a list box with text items in it. When I click on the OK button on the form it returns the string selected. I want to utilize the listbox DoubleClick event to fire the OK button. I've called the handler method for the OK button, but it just runs the code and the form stays open. I need it to act as if the user clicked the OK button. This method worked in vba. Any ideas how to do it in a C# windows form?
Solved! Go to Solution.
Solved by norman.yuan. Go to Solution.
The fact that your DoubleClick event handler calls the OK button's handler method and the form/dialog box does not close as if OK button is clicked indicates that the OK button has its property "Dialogresult" being set either OK, or Cancel, which means closing/hiding the form where the button is hosted.
If you want to let the listbox's DoubleClick and the OK button's Click event handler share the same handling method, you need to set the button's DialogResult prperty to None, and then iin the handling method to call Form.DialogResilt=OK/Cancel.
Norman Yuan
Thanks Norman, that was simple. I did a test and you don't need to set the button's DialogResult to None. It worked both ways. Seems like setting the property in the Click event just overrides what was previously set.