Question Accessing Declared Form From Another

Darkicon

New member
Joined
Jul 18, 2009
Messages
1
Programming Experience
1-3
Basically what I need to do is change a textbox on Form1 (AKA mainwin) from Form2 (AKA loginwin) using data that is inputted into Form2. I've done this before with applications by declaring Form1 in Form2, but this time I just get a NullReferenceException error.

Here's the code I'm currently using for Form2. Maybe someone here can tell me why it's not working.

VB.NET:
[COLOR="Blue"]Public Class[/COLOR] loginwin
    [COLOR="Blue"]Dim[/COLOR] f1 [COLOR="Blue"]As[/COLOR] mainwin [COLOR="SeaGreen"]'Form1 (mainwin) is declared as f1.[/COLOR]

    [COLOR="Blue"]Private Sub[/COLOR] Button1_Click([COLOR="Blue"]ByVal[/COLOR] sender [COLOR="Blue"]As[/COLOR] System.Object, [COLOR="Blue"]ByVal[/COLOR] e [COLOR="Blue"]As[/COLOR] System.EventArgs) Handles login.Click
        [COLOR="Blue"]Try[/COLOR]
            [COLOR="Blue"]If[/COLOR] usrn.Text.Length >= 1 [COLOR="Blue"]And[/COLOR] psw.Text.Length >= 1 [COLOR="Blue"]Then[/COLOR]
                f1.address.Text = [COLOR="DarkRed"]"[U]http://[/U]"[/COLOR] & usrn.Text & [COLOR="Sienna"]":"[/COLOR] & psw.Text & _
 [COLOR="DarkRed"]"@www.<address>.com/"[/COLOR] [COLOR="SeaGreen"]'The address textbox is located in mainwin.[/COLOR]
            [COLOR="Blue"]End If[/COLOR]
        [COLOR="Blue"]Catch[/COLOR] ex [COLOR="Blue"]As[/COLOR] Exception
            MsgBox([COLOR="DarkRed"]"An unknown error has occurred."[/COLOR], MsgBoxStyle.SystemModal, [COLOR="DarkRed"]"Error"[/COLOR]) [COLOR="SeaGreen"]'This catches the NullReferenceException error.[/COLOR]
        [COLOR="Blue"]End Try[/COLOR]
    [COLOR="Blue"]End Sub[/COLOR]
[COLOR="Blue"]End Class[/COLOR]

I can't figure out why it's not working. I've used this same method before and it always worked.

I added the syntax coloring to make it easier to read.
 
You are making assumptions about how variables and forms behave that are simply not true. I suggest that you follow the link in my signature about communicating between forms.
 
Back
Top