Results 1 to 5 of 5

Thread: [RESOLVED] Modal ListBox ?

  1. #1

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,429

    Resolved [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.

  2. #2
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,206

    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.

  3. #3
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    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.

  4. #4

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,429

    Re: Modal ListBox ?

    Quote Originally Posted by Sitten Spynne View Post
    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.

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,043

    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
  •  



Click Here to Expand Forum to Full Width