This seems to come up about twice a week, so I thought I'd post this as a FAQ.
If you have 2 forms, say Form1 is your primary form, and Form2 has a list box that you want the user to select a value from.
In the code of the Form2 class you would add:
Code:
Public Function GetSelection() As String
Me.ShowDialog()
Return Me.ListBox1.SelectedIndex
End Function
On Form1, when you want to get that value you can then use:
Code:
dim frm2 as Form2
SomeStringVariable = frm2.GetSelection
What it does:
when Form1 calls frm2.GetSelection it does so Synchronously. In other words, it will wait for the Getselection function to return a value.
When the Getselection function fires, it shows the form modally. Which means it will wait for the form to close before continuing that code. So when the user clicks "OK" and the form closes, the GetSelection function returns the value of the ListBox selection.
Things to improve/change in your app:
You may want to add a check to see if the user pressed cancel, of the "X" button instead of "OK" before returning the value. You'll also want to name your forms/objects appropriately. And it doesn't have to be a list box, you could even return an array or data table full of values if you needed more then one value.
-Rick