-
Jan 11th, 2017, 12:09 PM
#1
Thread Starter
PowerPoster
[RESOLVED] Modal ListBox ?
Hi,
Is it possible to make a Modal Form with just a ListBox, so that selecting an item:
1. Returns the selected item.
2. Closes the form.
Poppa.
Along with the sunshine there has to be a little rain sometime.
-
Jan 11th, 2017, 12:27 PM
#2
Re: Modal ListBox ?
Certainly creating a modal form with just list box is no problem and then having it close when an item is selected could be done by using the SelectedIndexChanged event, the issue is pushing the selected item back to the calling form. What type of information are we talking about? It might be as simple as sending a variable/object/datarow to the form ByRef.
-
Jan 11th, 2017, 12:28 PM
#3
Re: Modal ListBox ?
Yes, but not quite with the same behavior. What you can do is "Display a form modally and have it dismiss itself when a selection is made, then figure out what was selected."
Whether or not a form is modal isn't decided by the form itself, it's decided by whoever calls it. So when you show this form, use ShowDialog() instead of Show(). That returns a DialogResult. So the form with the ListBox has to set its own DialogResult property. Generally that closes the form for you, and causes ShowDialog() to return.
The form should have a public property that represents the item that was selected. The form that shows it will check this after ShowDialog() returns.
The form itself would handle the ListBox's SelectedIndexChanged event to tell when an item is selected. It would set the public property to that item. Then it would set its DialogResult to... something. In this context, it doesn't really matter what. Usually you use that to indicate "OK" or "Cancel" but in this case you can only have "OK".
This seems kind of frustrating, in the long run, as a user might accidentally select something. Consider waiting until they click an "OK" button.
-
Jan 11th, 2017, 12:39 PM
#4
Thread Starter
PowerPoster
Re: Modal ListBox ?
Originally Posted by Sitten Spynne
Yes, but not quite with the same behavior. What you can do is "Display a form modally and have it dismiss itself when a selection is made, then figure out what was selected."
This seems kind of frustrating, in the long run, as a user might accidentally select something. Consider waiting until they click an "OK" button.
Thanks Sitten Spynne,
I didn't want to sit for hours trying to invent a wheel which was already broken...
Thanks for the advice I'll try to sort something out from that.
Poppa.
Along with the sunshine there has to be a little rain sometime.
-
Jan 11th, 2017, 03:21 PM
#5
Re: [RESOLVED] Modal ListBox ?
It's fairly common to have a double click dismiss a form, but using a single click might be problematic. It's quite easy to select something in a listbox, so it's also easy to do it by accident when what you really meant to do was scroll. Therefore, just using SelectedIndexChanged might be too aggressive, while a double click might work better.
Might not, too, because it might feel wrong. You'd just have to try it out and see. It wouldn't be terribly difficult to put the code to close the form and return whatever was selected (perhaps via a property on the form) into some method, and try calling it from SelectedIndexChanged (single click) or the double click event, to get a feel for the situation.
My usual boring signature: Nothing
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|