Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Visual Basic (Classic) FAQ


Pass data from one form to another (Custom dialog) by ThatRickGuy
Posted: 10 Feb 05

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:


Public Function GetSelection() As String
 Return Me.ListBox1.SelectedIndex
End Function

On Form1, when you want to get that value you can then use:


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.


Back to Visual Basic (Classic) FAQ Index
Back to Visual Basic (Classic) Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close