How to return a selected value on a form from a method of a class in VB.net

I'm writing a class library in VB.net (using Visual Studio 2015 RC).  How can I write code for a method of a class to open a form, allow the user to select an item in a listbox on the form, and have the function(the method) return the ValueMember value of the listbox and close the form?
LVL 1
Declan_BasileITAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
Your form needs a custom on change event. When the value is changed fire it. In your class hook into that event.
0
Jacques Bourgeois (James Burger)PresidentCommented:
Open the Form with a ShowDialog command instead of Show. This freezes the calling method for as long as the form is visible on the screen.

When the user makes a selection, do not Close the Form, simply Hide it.

This will bring back the execution in you method. Because the Form is still in memory, the method can read the value in the ListBox and then Close the Form.

Dim frm As New YourForm
Dim value As Integer 'Or whatever
frm.ShowDialog
value=CInt(frm.ListBox1.SelectedValue)
frm.Close
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ste5anSenior DeveloperCommented:
Please don't query the values from your form directly. This is tight  coupling, which is makes code reuse and testability harder.

When an event sounds like overkill and you're only interested in a result, then create your own DialogResult class for this dialog and return it after the ShowDialog call.
0
Declan_BasileITAuthor Commented:
This was a simple solution and it worked great!  Thank you.
0
ste5anSenior DeveloperCommented:
Well, when it works for you, fine. But it ignores the basic prinicples of OOP design.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.