Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pass data from one form to another (Custom dialog)

How-to

Pass data from one form to another (Custom dialog)

by  ThatRickGuy  Posted    (Edited  )
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


Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top