Form Load Randomly Generated Numbers VB .net

Can't "pass" random number to button click

Page 1 of 1

5 Replies - 2956 Views - Last Post: 05 September 2007 - 08:40 PM Rate Topic: -----

#1 webil  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 02-September 07

Form Load Randomly Generated Numbers VB .net

Posted 02 September 2007 - 09:45 AM

Hello,

I have searched these forums in and out and it appears someone is taking the same class as I, and had the same problem, however being a NEWB I don't understand the solution, essentially I am programming a guessing game where the user guesses a randomly generated number, what I attempted to do is run the random number generator on form load, have the user input a guess in a textbox, click a button and compare the guess to the random number. It seems that all works ok except I can not figure out how to "Store" or keep the Randomly generated number when the button is clicked. Here is my Code as you can see I have commented it out on form load and have it generating the random number on button click, just for testing purposes.

code
Option Explicit On
Option Strict On

Imports System.Globalization

Public Class MainForm

	'Exit program
	Private Sub ExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton.Click
		
		Me.Close()

	End Sub
	Private Sub ClearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearButton.Click
		' Clears Guess Box
		GuessTextBox.Clear()

	End Sub
	Private Sub MainForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		'Dim randomGenerator As New Random
		'Dim number As Integer
		'Generates a Random number between 0-10
		'number = randomGenerator.Next(1, 10)

		'Reveal Random Number for testing 
		'ResultsLabel.Text = Convert.ToString(number)

	End Sub
	
	Private Sub GuessButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GuessButton.Click
		Dim number As Integer
		Dim guess As Integer
		Dim randomGenerator As New Random
		Dim isConverted As Boolean
		Dim SunPictureLocation As Integer


		'Generates a Random number between 0-10
		number = randomGenerator.Next(1, 10)

		'Reveal Random Number for testing
		ResultsLabel.Text = Convert.ToString(number)

		'Convert input to integer
		isConverted = Integer.TryParse(GuessTextBox.Text, guess)

		If isConverted = True Then

		End If
		If guess > number Then
			SunPictureBox.SetBounds(SunPictureLocation - SunPictureBox.Height, +200, +200, +200, BoundsSpecified.Y)
			MessageBox.Show("Lower")

		ElseIf guess < number Then
			SunPictureBox.SetBounds(SunPictureLocation - SunPictureBox.Height, -5, -5, -5, BoundsSpecified.Y)
			MessageBox.Show("Higher")

		ElseIf guess = number Then
			MessageBox.Show("Correct!")


		End If

	End Sub

   
	
End Class



Is This A Good Question/Topic? 0
  • +

Replies To: Form Load Randomly Generated Numbers VB .net

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Form Load Randomly Generated Numbers VB .net

Posted 02 September 2007 - 09:54 AM

webil,

Before I help you debug this problem you're going to have to put that code into code blocks (button with the # in the toolbar) so I can easily read it. Thanks :)
Was This Post Helpful? 0
  • +
  • -

#3 webil  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 02-September 07

Re: Form Load Randomly Generated Numbers VB .net

Posted 02 September 2007 - 10:48 AM

Sorry, Thought I had :P

Option Explicit On
Option Strict On

Imports System.Globalization

Public Class MainForm

	'Exit program
	Private Sub ExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton.Click
		
		Me.Close()

	End Sub
	Private Sub ClearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearButton.Click
		' Clears Guess Box
		GuessTextBox.Clear()

	End Sub
	Private Sub MainForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		'Dim randomGenerator As New Random
		'Dim number As Integer
		'Generates a Random number between 0-10
		'number = randomGenerator.Next(1, 10)

		'Reveal Random Number for testing 
		'ResultsLabel.Text = Convert.ToString(number)



	End Sub
	
	Private Sub GuessButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GuessButton.Click
		Dim number As Integer
		Dim guess As Integer
		Dim randomGenerator As New Random
		Dim isConverted As Boolean
		Dim SunPictureLocation As Integer


		'Generates a Random number between 0-10
		number = randomGenerator.Next(1, 10)

		'Reveal Random Number for testing
		ResultsLabel.Text = Convert.ToString(number)

		'Convert input to integer
		isConverted = Integer.TryParse(GuessTextBox.Text, guess)

		If isConverted = True Then

		End If
		If guess > number Then
			SunPictureBox.SetBounds(SunPictureLocation - SunPictureBox.Height, +200, +200, +200, BoundsSpecified.Y)
			MessageBox.Show("Lower")

		ElseIf guess < number Then
			SunPictureBox.SetBounds(SunPictureLocation - SunPictureBox.Height, -5, -5, -5, BoundsSpecified.Y)
			MessageBox.Show("Higher")

		ElseIf guess = number Then
			MessageBox.Show("Correct!")


		End If

	End Sub

  
   
	
End Class



Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Form Load Randomly Generated Numbers VB .net

Posted 02 September 2007 - 11:48 AM

Try something like this. In order to keep the random number upon Form Load, create a Global variable, then set the variable when the form loads. Doing this will make that number available when the Guess button is clicked, somethinkg like

Option Explicit On
Option Strict On

Imports System.Globalization

Public Class MainForm
Private RndNumber As Integer
	'Exit program
	Private Sub ExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton.Click
		
		Me.Close()

	End Sub
	Private Sub ClearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearButton.Click
		' Clears Guess Box
		GuessTextBox.Clear()

	End Sub
	Private Sub MainForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		Dim randomGenerator As New Random
		Dim number As Integer
		'Generates a Random number between 0-10
		number = randomGenerator.Next(1, 10)

		 'Set the global you created
		 RndNumber = number
		'Reveal Random Number for testing
		'ResultsLabel.Text = Convert.ToString(number)
	End Sub
	
	Private Sub GuessButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GuessButton.Click
		 Dim guess As Integer
		 Dim SunPictureLocation As Integer

		 guess = Integer.TryParse(GuessTextBox.Text, New Integer)

		 If guess > RndNumber Then
			SunPictureBox.SetBounds(SunPictureLocation - SunPictureBox.Height, +200, +200, +200, BoundsSpecified.Y)
			MessageBox.Show("Lower")
		ElseIf guess < RndNumber Then
			SunPictureBox.SetBounds(SunPictureLocation - SunPictureBox.Height, -5, -5, -5, BoundsSpecified.Y)
			MessageBox.Show("Higher")
		ElseIf guess = RndNumber Then
			MessageBox.Show("Correct!")
		End If
	End Sub

'Create a new button so they can play again, call this button NewButton, then put this in its Click event
	 Private NewButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewButton.Click
			Dim randomGenerator As New Random
			Dim number As Integer

			 'First reset the global back to 0
			 RndNumber = 0

			'Generates a Random number between 0-10
			number = randomGenerator.Next(1, 10)

			 'Set the global you created
			 RndNumber = number
		   
	End Sub
End Class



That should do what you're looking for. Hope it helps :)
Was This Post Helpful? 0
  • +
  • -

#5 webil  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 02-September 07

Re: Form Load Randomly Generated Numbers VB .net

Posted 05 September 2007 - 07:47 PM

PsychoCoder,
Thank you very much, exactly what I was looking for.
Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Form Load Randomly Generated Numbers VB .net

Posted 05 September 2007 - 08:40 PM

No problem, glad I could help :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1