Question Using the same variable in multiple forms

neoxyn

New member
Joined
Jul 20, 2008
Messages
1
Programming Experience
Beginner
My problem is as following. I'm designing some kind of game and the user has two options:load game or new game. If someone pressed new game, it should move the character to location x=100 and y =100 on map.

I use this code

Form1: Main menu with new game and load game button.

VB.NET:
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Hide()
        Form3.ShowDialog()

    End Sub

End Class


Form3: Character create

VB.NET:
Public Class Form3

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        username = TextBox1.Text
        Left = 100
        Top = 100
        Me.Hide()
        Form2.ShowDialog()
    End Sub
End Class

Form2: Gamemap form

VB.NET:
Public Class Form2

  Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Label4.Text = username
        Label1.Top = Top
        Label1.Left = Left
    End Sub
End Class

Module1: for using variables throughout forms

VB.NET:
Module Module1
    Public username As String
    Public left As Integer
    Public top As Integer

End Module



If I click new game, enter character name,it will show the character name correctly, but the character (label1) for some reason spawns to location x= 213 - y= 269.. I have no idea why and I get no errors.

I Hope someone can help me, I'm really new to this. Sorry if I posted this in the wrong section. I'm using Visual Basic 2008.
 
Last edited by a moderator:
Back
Top