• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 184
  • Last Modified:

VB.NET Pass variable from 2nd form to main form

I am running into an issue where I need to pass value from a 2nd form that I open to my original main form.  What I did was create a lookup form that has a datagridview.  From my main form, I click on a button that would open up the 2nd form.  If I  double click on which ever row from the datagridview on the 2nd form, I want to transfer the ID of that rowindex back to the main form and input that to the txtID.Text from the main form, then close the 2nd form.

I did a bit of research but can't seem to get what I need working.  Can anyone assisst me with some example?

1 Solution
Jorge PaulinoIT Pro/DeveloperCommented:
Here's a way ...

' First Form
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim frm As New Form2
        If frm.ID <> 0 Then
        End If
    End Sub
End Class
' Second Form
Public Class Form2
    Private _ID As Integer
    Public Property ID() As Integer
            Return _ID
        End Get
        Set(ByVal value As Integer)
            _ID = value
        End Set
    End Property
    ' Close button
    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.ID = 123
    End Sub
End Class

Open in new window

holemaniaAuthor Commented:
Awesome.  That worked!!
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.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now