1

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?

4
  • That's not how you increment an integer in VB.NET.. Commented Dec 11, 2015 at 15:07
  • 1
    @cybermonkey, because of curiosity what is the vb.net style for increment integer? Commented Dec 11, 2015 at 18:42
  • @Fabio Form2.CCount = Form2.CCount+1. See this. Commented Dec 11, 2015 at 19:19
  • 1
    @cybermonkey, OP's code is ok. check this += Operator (Visual Basic). Commented Dec 11, 2015 at 19:36

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot mate, for both the solution and the explanation!! It worked perfectly!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.