 |
 |

06-10-2012, 12:24 AM
|
|
Newcomer
|
|
Join Date: Jun 2012
Posts: 5
|
|
High Low Game
|
So I pretty much built the game, that was the easy part. I am having trouble entering a name on my first form(startup) and keeping track of the score on second form(game) and displaying it both combined on the first page with the current users score. Also the way it set up now if you are running the game for the first time it says can't find LowScores.Text.
Start Up
Code:
Public Class StartUp
Private Sub txtName_keydown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles txtName.KeyDown
If e.KeyCode = Keys.Enter Then
Me.Visible = False
Game.Visible = True
End If
End Sub
Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub btnPlay_Click(sender As Object, e As System.EventArgs) Handles btnPlay.Click
Me.Visible = False
Game.Visible = True
' writes a name to a sequential access file
' declare a StreamWriter variable
Dim outFile As IO.StreamWriter
' open the file for append
outFile = IO.File.AppendText("LowScores.txt")
' write the name on a separate line in the file
outFile.WriteLine(txtName.Text)
' close the file
outFile.Close()
' clear the Name box, then set the focus
txtName.Text = String.Empty
txtName.Focus()
End Sub
Private Sub StartUp_Load(sender As Object, e As System.EventArgs) Handles Me.Load
' reads names from a sequential access file
' and displays them in the interface
' declare variables
Dim inFile As IO.StreamReader
Dim strName As String
' clear previous names from the Contestants box
lblLowScores.Text = String.Empty
' determine whether the file exists
If IO.File.Exists("LowScores.txt") = True Then
'open the file for input
inFile = IO.File.OpenText("LowScores.txt")
'process the loopinstructions until the end of the file
Do Until inFile.Peek = -1
'read a name
strName = inFile.ReadLine
'display the name
lblLowScores.Text = lblLowScores.Text &
strName & ControlChars.NewLine
Loop
'close the file
inFile.Close()
Else
MessageBox.Show("Can't find the Low Scores.txt file",
"Show lowest scores",
MessageBoxButtons.OK,
MessageBoxIcon.Information)
End If
End Sub
End Class
Game
Code:
'High Low - Game
Imports System.Text.RegularExpressions
Public Class Game
Public Num As New Random
Public int As Integer
Public intCount As Integer
Private Sub main()
If txtNum.Text <> Nothing Then
If Convert.ToInt32(txtNum.Text) = int Then
Me.Size = New Size(350, 300)
lblInstructions.Hide()
If MessageBox.Show("Good job. you got it in " & intCount & " tries. Would you like to play again??", "you won!!", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) = DialogResult.Yes Then
Me.Size = New Size(350, 300)
lblInstructions.Show()
txtNum.Text = Nothing
intCount = 1
int = Num.Next(1, 10)
ElseIf DialogResult.No Then
StartUp.Visible = True
Me.Close()
Me.Size = New Size(350, 300)
lblInstructions.Show()
End If
txtNum.Text = Nothing
intCount = 1
ElseIf Convert.ToInt32(txtNum.Text) < int Then
MessageBox.Show("Higher")
txtNum.Text = Nothing
intCount += 1
ElseIf Convert.ToInt32(txtNum.Text) > int Then
MessageBox.Show("Lower")
txtNum.Text = Nothing
intCount += 1
End If
Else
MessageBox.Show("enter some text")
End If
End Sub
Private Sub Game_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
intCount = 1
int = Num.Next(1, 10)
End Sub
Private Sub txtbox1_keydown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles txtNum.KeyDown
If e.KeyCode = Keys.Enter Then
main()
End If
End Sub
Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStartOver.Click
txtNum.Text = Nothing
intCount = 1
int = Num.Next(1, 10)
End Sub
Private Sub txtBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtNum.TextChanged
txtNum.Text = Regex.Replace(txtNum.Text, "([aA-zZ]|\s|\e|\(|\)|\!|\@|\#|\$|\%|\^|\~|\`|\,|\:|\;|\&|\<|\>|\?|\'|\"")", "")
End Sub
Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
StartUp.Close()
Me.Close()
End Sub
End Class
|
Last edited by passel; 06-10-2012 at 11:06 PM.
|

06-10-2012, 11:11 PM
|
 |
Sinecure Expert
Super Moderator * Guru *
|
|
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 7,714
|
|
|
Certainly looks like VB.Net.
Not sure why you would scroll down and post in the Legacy VB (VB6 (1998) and earlier) General forum.
I've moved the post to .Net Game Programming forum.
|
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
|

06-11-2012, 09:32 AM
|
 |
Fabulous Florist
Forum Leader * Guru *
|
|
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,416
|
|
Good background information is in Bucky's Passing variables and controls using WinForms and VB .NET. The short story: if you want to send information from one form to another, you're going to have to do so via properties or method parameters.
I've attached a project that demonstrates the "get a name from one form to the other" aspect of your question. When you put the button on StartForm, it gathers all of its information into a PlayerInformation type. Before showing the StartForm, it puts that PlayerInformation into the form's property. When the StartForm loads, it uses the information from that property to populate its own controls.
There's a million ways to do this, but as long as you understand the basic "information is sent via property sets and method parameters" point.
|
|

06-11-2012, 09:30 PM
|
|
Newcomer
|
|
Join Date: Jun 2012
Posts: 5
|
|
|
Thanks for your help. I will try it out and let you know how it goes.
|
|

06-12-2012, 08:29 AM
|
|
Newcomer
|
|
Join Date: Jun 2012
Posts: 5
|
|
|
I figured it out with your help and the info. in Bucky's passing variables. It seems to work for the most part, it did create a few kinks that have to be worked out.
|
|

06-12-2012, 01:28 PM
|
|
Newcomer
|
|
Join Date: Jun 2012
Posts: 5
|
|
|
So for some reason the score always returns 0 instead of the number of tries. The username works fine. But, I am having a hard time getting the intCount to display as thin my label.
|
|

06-12-2012, 03:13 PM
|
|
Newcomer
|
|
Join Date: Jun 2012
Posts: 5
|
|
|
sorry, never-mind I figured it out. Thanks for the help though.
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
|
|
 |
|