Hi Everyone!

I have been experiencing problems in displaying my varibable Gcoins in form3 from form1.

here is what i have in my form3 code.

Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim frm1 As New Form1
        Dim points As Integer
        gcoinsamount = Val(frm1.Label2.Text)
        Label1.Text = gcoinsamount.ToString
        points = Val(Label1.Text)
        Label2.Text = points.ToString("N")
        frm1 = Nothing
    End Sub

and here is the codes in form1. (Kinda messy)

Public Class Form1
    Dim rnd As New Random()
    Dim value1 As Integer = Rnd.Next(1, 3)
    Dim value2 As Integer = Rnd.Next(1, 3)
    Dim timercount1 As Integer = 80
    Dim scorepoint As Integer = 100
    Dim GcoinPoints As Integer = 50
    Dim gcoinpoints2 As Integer

    Private Sub form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       
        Timer2.Interval = 1000 'The number of miliseconds in a second
        Timer2.Enabled = True 'Start the timer

        Label2.Text = GcoinPoints.ToString
        RichTextBox1.Text = scorepoint.ToString
        
        Dim frm2 As New Form2()
        If (frm2.ShowDialog = Windows.Forms.DialogResult.OK) Then
            UserNameLabel.Text = frm2.UserNameTextBox.Text 'which displays the username onto the label'
        End If
    End Sub
    Public Function FindGcoins(ByVal gcoinpoints2 As Integer) As Integer
        'Calculate the amount of Gcoins

        Return scorepoint / 2
    End Function
    Private Sub Button1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.MouseEnter
        ' User moves mouse pointer over the button, change the color
        Button1.BackColor = Color.Yellow
       
    End Sub

    Private Sub Button1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.MouseLeave
        ' User moved mouse pointer away from the button, change the color back to red
        Button1.BackColor = Color.Red
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim rnd As New Random()
        Dim value1 As Integer = rnd.Next(1, 3)
        Dim value2 As Integer = rnd.Next(1, 3)
        
        timercount1 = 80 'Reset to 80 seconds
        lblOutput.Text = timercount1.ToString() 'Reset the output display to 80
        Timer2.Enabled = True
        gcoinpoints2 += -20

        If value1 < value2 Then
            Label1.Text = "You win"
            MessageBox.Show("You win")
            scorepoint += 40
            gcoinpoints2 = FindGcoins(gcoinpoints2)
            RichTextBox1.Text = scorepoint.ToString
            Label2.Text = gcoinpoints2.ToString()
        End If

        If value1 = value2 Then
            Label1.Text = "Draw"
            MessageBox.Show("Draw")
            scorepoint += 20
            gcoinpoints2 = FindGcoins(gcoinpoints2)
            RichTextBox1.Text = scorepoint.ToString
            Label2.Text = gcoinpoints2.ToString()
        End If

        If value1 > value2 Then
            Label1.Text = "You lose"
            MessageBox.Show("You lose")
            scorepoint += -40
            gcoinpoints2 = FindGcoins(gcoinpoints2)
            RichTextBox1.Text = scorepoint
            Label2.Text = gcoinpoints2.ToString()
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        timercount1 = 80 'Reset to 80 seconds
        lblOutput.Text = timercount1.ToString() 'Reset the output display to 80
        Timer2.Enabled = True

        If value1 < value2 Then
            Label1.Text += "You win"
            MessageBox.Show("You win")

        End If

        If value1 = value2 Then
            Label1.Text += "Draw"
            MessageBox.Show("Draw")

        End If

        If value1 > value2 Then
            Label1.Text += "You lose"
            MessageBox.Show("You lose")

        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        timercount1 = 80 'Reset to 80 seconds
        lblOutput.Text = timercount1.ToString() 'Reset the output display to 80
        Timer2.Enabled = True

        If value1 < value2 Then
            Label1.Text = "You win"
            MessageBox.Show("You win")

        End If

        If value1 = value2 Then
            Label1.Text = "Draw"
            MessageBox.Show("Draw")
        End If

        If value1 > value2 Then
            Label1.Text = "You lose"
            MessageBox.Show("You lose")
        End If
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        MessageBox.Show("Good Bye And Hopefully See You Again")
        Me.Close()
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Dim frm2 As New Form2()

        If (frm2.ShowDialog = Windows.Forms.DialogResult.OK) Then
            UserNameLabel.Text = frm2.UserNameTextBox.Text 'which displays the username onto the label'
        End If
        frm2 = Nothing
    End Sub

    
    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Dim frm1 As New Form1
        Form3.Show()
        frm1.Close()
    End Sub
   

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        lblOutput.Text = timercount1.ToString() 'show the countdown in the label
        If timercount1 = 0 Then 'Check to see if it has reached 0, if yes then stop timer and display done
            Timer2.Enabled = False
            lblOutput.Text = "Times up"
            MessageBox.Show("Please choose A selection") 'Display message to users reminding their selection'
        Else 'If timercount is higher then 0 then subtract one from it
            timercount1 -= 1

        End If
    End Sub

    
    Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
        Label1.ForeColor = Color.Black
    End Sub

    Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
        Label1.ForeColor = Color.Green
    End Sub

    Private Sub Label5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label5.Click

    End Sub


    Private Sub NewGameToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewGameToolStripMenuItem.Click
        With RichTextBox1
            .Clear()
            .Focus()
        End With
        timercount1 = 80 'Reset to 80 seconds
        lblOutput.Text = timercount1.ToString() 'Reset the output display to 80
        Timer2.Enabled = True
    End Sub
End Class

The problem is that when ever i get into form3, it won't display my value Gcoins in the label i want.

Any one can give my some solutions to this?

Thank you!!!

Recommended Answers

All 2 Replies

On Form1 you are declaring your variables "Locally", therefore only Form1 has access to them.

To declare something "Globally" you need to change "Dim" to "Public".
Example of a "Global Declaration" on Form1:

Public Gcoins As String = "1,000,000 gazillion & a couple billions."

And to get the variable from another form, I used the following:

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        MsgBox(Form1.Gcoins)
    End Sub

Thank you very much Codeorder!!! It works now!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.