Bobski Bobski - 22 days ago 18
Vb.net Question

Passing parameters between two forms in VB.Net

I currently have about 5 forms in my application. I'm building a 6th form - frmSummary however, I'd like to be able to access it from all forms. in frmSummary I am planning to add a DataGridView, where I'll be displaying data related to that form. I'm thinking that I should either create a global variable such as

dim FrmName as String


In each form I would have a cmdSummary button so that On click_event, I would do something like

frmName ="CustomerInfo"


Currently the way my application is set up is that I hve a mdiForm and within it, each form is a child so on opening new forms I do something like...

Private Sub cmdSummary_Click(sender As Object, e As EventArgs) Handles cmdSummary.Click
Dim NewMDIChild As New frmClientEligibilityReferral()

frmName = "CustomerInfo" --since this will be comeing from frmCustomerInfo
NewMDIChild.MdiParent = MDIform1
NewMDIChild.Show()
MDIForm1.Show()

End Sub


So I do something like that on opening my new form. My question is how can I pass the parameter to my form frmSummary....here's currently what I'm trying to accomplish....

Private Sub FrmSummary_Load(sender As Object, e As EventArgs) Handles Me.Load

Me.MdiParent = MDIForm1

InitializeComponent()

'Here I want to call a function to load the datagridView(with g_frmName)see below...
call LoadDataGrid(frmName)
End Sub


Is something like that a smart idea? Or should I/Can I directly call the function from the previous form?
Just trying to see if I'm on the right track, if not, how can i do it in a sound way?

Answer

If there is only one frmSummary, you could make it a singleton.

In frmSummary, put the following code:

Private Shared _instance As frmSummary

Private Sub New()
    ' This call is required by the designer.
    InitializeComponent()
    ' Add any initialization after the InitializeComponent() call.
End Sub

Public Shared Function GetInstance() As frmSummary
    If _instance Is Nothing Then
        _instance = New frmSummary()
    End If
    Return _instance
End Function

Public Sub PutDataInGrid(data As Object)
    Me.DataGridView1.' put data in it
End Sub

And you would access it from other forms like this

Dim myFrmSummary = frmSummary.GetInstance()
myFrmSummary.PutDataInGrid(myData)
Comments