Passing variables between forms

pettrer

Well-known member
Joined
Sep 5, 2008
Messages
92
Programming Experience
10+
Hi,

I've used the instruction on this great page Passing variables and controls using WinForms and VB.NET [Archive] - Xtreme Visual Basic Talk to pass variables between forms on VB.Net Winforms (instead of using global variables, properties, or public variables).

Basically, I call a form using this code

VB.NET:
If StudId > 0 And AntId > 0 Then
                Dim nyform As New frmStud(StudId, AntId, Me)
                nyform.Show()
            Else
                MsgBox("Errortext. " & StudId & " " & AntId)
            End If

and fetches the variable using this code

VB.NET:
Private sStudId As Integer
Private sAnt As Integer
Private anropande As Form
Public Sub New(ByVal id As Integer, ByVal ant As Integer, ByVal namn As Form)
        MyBase.New()
        InitializeComponent()
        sStudId = id
        sAnt = ant
        anropande = namn
    End Sub
.

However, I now face a problem. I have a form that calls another form when the user clicks a button. This new form gets its variables using the above method. This form then calls a third form, passing some variables. Now, depending on what the user selects in this third form, the first form needs to get the variables back (so basically, it's more like a function that returns values).

I can't seem to accomplish this using the method I described, as that would mean creating a new Form1 class, and I just want to return the variables. What should I do? (I guess the same problem arises when it's just a matter of two forms, as in any master-detail scenario).

When I'm at it, a small question: I need to close the second form after the third form delivered its variables back to Form1. I guess the only way to access it, such as Form2.Close(), is to send the entire form as a variable (as I did in the code above). I guess this is heavy on the server and it doesn't look nice, when all I need is a reference to it (something as "Me.Caller.Close()" would do nicely too). Any suggestions (sending the Name property doesn't work for me, as the name itself can't be closed)?

Thanks for all help!

Pettrer
 
Instead of passing the Form around ByVal, use ByRef

VB.NET:
Public Class Form1


	If StudId > 0 And AntId > 0 Then
		Dim nyform As New frmStud(StudId, AntId, Me)
                nyform.Show()
	Else
                MsgBox("Errortext. " & StudId & " " & AntId)
	End If

End Class


Public Class Form2
	Private sStudId As Integer
	Private sAnt As Integer
	Private anropande As Form


	Public Sub New(ByVal ID As Integer, ByVal ANT As Integer, [B]ByRef [/B]NAMN As Form)
		MyBase.New()
		InitializeComponent()
		sStudId = ID
		sAnt = ANT
		anropande = NAMN
	End Sub

	Public Sub OpenThirdForm()
		Dim frm3 as New Form3(anropande, Me)
		frm3.Show()
	End Sub

End Class


Public Class Form3
	
	Private Form1Ref As Form
	Private Form2Ref As Form

	Public Sub New([B]ByRef[/B] Form1Reference As Form,[B]ByRef[/B] Form2Reference As Form)
		MyBase.New()
		InitializeComponent()
		Form1Ref = Form1Reference
		Form2Ref = Form2Reference
	End Sub

End Class
 
Thank you for your reply (and cool to hear from someone from Minnesota!). I don't quite get it though - are you answering my second question only or am I mistaken?

All the best,

Pettrer
 
Back
Top