Question How to variables between forms

Antone.evans

Member
Joined
Sep 29, 2010
Messages
14
Location
Tucson
Programming Experience
Beginner
I'm sure this gets asked a lot, but I can't seem to find an answer for it. I want to have my Form2 change a variable in Form1.

thanks,
Antone
 
There are several different ways to do this depending on conditions.

If this is something you think you might be doing alot, I would recommend creating a module and setting up properties in the module. Then, it can be accessed using Module.PropertyName = value.

If it's just a one time thing, you should be able to access it using Form1.VariableName = value. This should work as long as you have the variable set up as a public global variable in Form1.
 
Using shared variables is often a recipe for disaster, and usually it is the wrong approach to object oriented programming. I recommend using instance properties; theform.SomeProperty = value, or passing information over method parameters; theform.SomeMethod(value).
'theform' here refers to the instance of the form, which is either a reference you have for a form instance you created, or perhaps you're using default form instances and can refer to the form instance simply by using the form name as qualifier (as in ggunters examples (Form1.member). Default instances is the only thing that is special about forms in this regard, forms are classes, so anything you learn about classes in general is equally valid for form classes. Learning about classes and similar basic programming elements is something I recommend you take on.
 
I did, but I don't see the danger in it. I'm just having a button open up a new form that accepts a password and adjusts a the text property of a public and shared label from the first form. Plus I'm not passing the value to the child, but to the parent. So I'm not sure how to pass to the parent with a method yet. :/
 
Plus I'm not passing the value to the child, but to the parent. So I'm not sure how to pass to the parent with a method yet. :/
If you can explain more about the program flow perhaps we can give better and more relevant advice. For example in a dialog scenario the parent would retrieve data from the child when dialog returns. Normally independent classes would define events and pass information over event parameters, the parent that created the child may subscribe to this event and would get the information whenever the child raises the event. The parent may also pass its own reference to the child, which the child later can use (of course not necessary for default form instances).
 
Okay, well here's what I have. (Some unrelated code omitted)

form 1
VB.NET:
Public Class Form1
    Public Shared userLabel As Label

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        userLabel = lblUsr
        lblUsr.Text = "General User"
        If lblUsr.Text <> "Admin" Then
            BindingNavigatorDeleteItem.Enabled = False
        End If
    End Sub

        Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
        Dim fLogin As New Form2
        fLogin.ShowDialog()
        fLogin = Nothing
    End Sub

    Private Sub btnLogout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogout.Click
        lblUsr.Text = "General User"
        BindingNavigatorDeleteItem.Enabled = False
    End Sub

End Class

login form
VB.NET:
Public Class Form2

    Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        Dim logIn As String
        logIn = Form1.lblUsr.Text

        If txtPassword.Text <> "password" Then
            MessageBox.Show("Error: Incorrect Password")
        Else
            If logIn = "Admin" Then
                MessageBox.Show("Error: You are already logged in.")
            Else
                Form1.lblUsr.Text = "Admin"
                Form1.BindingNavigatorDeleteItem.Enabled = True
                Me.Close()
            End If
        End If
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub
End Class

I'm sure you can tell, but this is for a database editing program.
 
So you're using a dialog, and you should use properties in the dialog form to pass information back and forth. Pass information to the dialog before it is displayed, retrieve information from the dialog after ShowDialog returns.
ShowDialog is a function that returns the result of the dialog as a DialogResult value, for example when you handle this correctly you will be able to see if dialog was cancelled or if login was successful (DialogResult.OK).
 
Back
Top