Passing values between forms

Experts,

I have two forms, frmA and frmB.

frmA opens frmB, the user selects a value from a datagrid in frmB. How do I pass the selected value back to frmA and close frmB?
I understand how to pass values to forms you are going to open but I can't figure out how to pass values to already open forms.

Thanks, Bob
bobbailey22Asked:
Who is Participating?
 
SourabhmConnect With a Mentor Commented:

 Public Sub frmAButton_Click(...)

          dim oFrmB as New frmB

          dim ValueRecvd as string

          oFrmB.ShowDialog()

          ValueRecvd = oFrmB.ValuePAssed

End Sub

' Declare public variable in frmB
When the frmB is closed store the datagrid selected value in public variable

Public ValuePassed as string

    Private Sub frmB_OnClosed(...)
              ValuePassed = <datagrid selcted value>
    End Sub

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Many ways to go about this...

Public Class frmA

    Public Sub Button1_Click(...)
        Dim B As New frmB
        B.A = Me
        B.Show()
    End Sub

    Public Sub Foo(ByVal someValue As String)
         ' do something in here with someValue...
    End Sub

End Class

Public Class frmB

    Public A As frmA

    Public Sub Button1_Click(...)
        If Not (A Is Nothing) Then
            A.Foo("some value selected by the user...")
            Me.Close()
        End If
    End Sub

End Class
0
 
Brian CroweDatabase AdministratorCommented:
I would suggest adding a public readonly property to the form with the datagrid that stores the current datagrid "value" and is updated whenever datagrid.currentcellchanged.  This property is then visible to the other form.  If you need some example code let me know.
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
bobbailey22Author Commented:
Idle_Mind - the line: "B.A = Me" gives an error of " A is not a member of B"

Am I not understanding something?

bob
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Did you add this line:

    Public A As frmA

to frmB?
0
 
bobbailey22Author Commented:
Idle_Mind : I know that you are very good and you have helped me in the passed but on this one Sourabhm's solution made more since to me as it seemed more simple and I am not as good as you are. Thanks anyway.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Lol...no problem bobbailey22!

As I said at the beginning of my first post:

    "Many ways to go about this..."

It's up to you to decide what works best for you.

=)
0
 
bobbailey22Author Commented:
Thanks man.
I do have a question if either of you donsn't mind taking the time...

I don't understand why Sourabhm's solution works. It would seem to me that when  frmB is closed the public variable "ValuePassed" (which is declared in frmB) would get destroyed? I guess I don't get how frmB's public varivable is availible to frmA after frmB is closed.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Because the instance of frmB is not disposed of until the reference, oFrmB, goes out of scope.
0
 
bobbailey22Author Commented:
So when does it go out of scope? When the app is closed? Should I have some kind of clean up code for this?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
It goes out of scope when the sub frmAButton_Click() is exited because that is where is was declared.
0
 
bobbailey22Author Commented:
Oh, I see. Thanks man. I wish I could give you points for this!!!!!!!
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Don't worry about it...tis no big.
0
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.

All Courses

From novice to tech pro — start learning today.