AruaS
AruaS

Reputation: 43

VB.NET: Passing variable between forms, error

I want to pass the value of an exact variable to another form... To be more precise I'm building a game in which a player answer a few questions so I want to pass the variable that contains the correct answer to another form which works as the end game window and displays the score... I'm using this kind of code in the end game window:

Public Class Form2

Public Property CCount As Integer

Private Sub Form5_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Label1.Text = CCcount
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    CCount = 0
    Form1.Show()
    Me.Close()
End Sub

End class

And this in the main form:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

'Lines of code
Form2.CCount +=1
'Lines of code
'Me.Hide()
'Form2.Show()

EndSub

The first time that I'm "playing the game" everything works fine, but if try to replay it the displaying scores never changes... It remains the same as the first run.. Any ideas how I can fix this out?

Upvotes: 1

Views: 77

Answers (1)

Fabio
Fabio

Reputation: 32445

You assign value of CCount to the Label only ones when form was loaded.
Load event raised only one time. Put label updating code inside CCount Property Setter

Private _CCount As Integer
Public Property CCount As Integer
    Get
        Return _CCount
    End Get
    Set (value As Integer)
        If _CCount = value Then Exit Property
        _CCount = value
        Me.Label1.Text = _CCount.ToString()
    End Set
End Property

Upvotes: 3

Related Questions