?
Solved

Passing values between forms

Posted on 2005-04-25
13
Medium Priority
?
385 Views
Last Modified: 2010-04-23
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
0
Comment
Question by:bobbailey22
13 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 13861265
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
 
LVL 34

Expert Comment

by:Brian Crowe
ID: 13861456
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
 

Author Comment

by:bobbailey22
ID: 13862491
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 13863638
Did you add this line:

    Public A As frmA

to frmB?
0
 
LVL 4

Accepted Solution

by:
Sourabhm earned 2000 total points
ID: 13864983

 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
 

Author Comment

by:bobbailey22
ID: 13868318
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 13868338
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
 

Author Comment

by:bobbailey22
ID: 13868526
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 13869505
Because the instance of frmB is not disposed of until the reference, oFrmB, goes out of scope.
0
 

Author Comment

by:bobbailey22
ID: 13869699
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 13869911
It goes out of scope when the sub frmAButton_Click() is exited because that is where is was declared.
0
 

Author Comment

by:bobbailey22
ID: 13870467
Oh, I see. Thanks man. I wish I could give you points for this!!!!!!!
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 13870981
Don't worry about it...tis no big.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question