Quantcast

Jump to content

» «
Photo

Visual Basic Help

3 replies to this topic
Indi
  • Indi

    Don't cross the streams.

  • Andolini Mafia Family
  • Joined: 22 Nov 2008
  • None

#1

Posted 30 October 2012 - 01:45 AM

I have this in my code editor... currently:
CODE
Public Class Form1
   Dim num As Integer
   Private Sub GetNumber()
       num = Math.Ceiling(Rnd() * 20)
   End Sub
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       Randomize()
       GetNumber()
   End Sub
   Private Sub Guess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Guess.Click
       Select Case (Val(Guess.Text))
           Case Is > num
               Msg.Text = Guess.Text & " is too high"
           Case Is < num
               Msg.Text = Guess.Text & " is too low"
           Case Is = num
               Msg.Text = Guess.Text & " is correct" & _
               "I have thought of another number - Try again!"
               GetNumber()
       End Select
       Guess.Text = ""
   End Sub
End Class


I'm creating a guessing program/game that you enter a number into a textbox, and you have to go along with it until the program says that you are either high, or low, depending on what number you choose.

http://puu.sh/1kl5x

My design looks like the above. My label is named "Msg," and my button is named "Guess."

The program is simply not working and I can't figure out why, whenever I input a number into my textbox it always says "Guess is too low."

nightwalker83
  • nightwalker83

    Don't mind me

  • Members
  • Joined: 10 Oct 2004
  • None

#2

Posted 30 October 2012 - 07:34 AM Edited by nightwalker83, 30 October 2012 - 07:37 AM.

Here is the version I created in C# a few years ago! You get the gist of things and be able to convert the code.

CODE

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

/* Programmer: Aaron Spehr Date: 23rd March 2010 (4BC#) Modified: 04/05/2010
 * Description: 4BC# Lesson 11: Multi-Form Projects (Visual Studio C# V.2008)
 * Additional Topics: Methods, Events, Testing & Debugging (to Output Window)
 * Modularisation; Events (Form_Load/KeyUp); Variables (Local, Class, Public)
 */                                                                        

namespace MultiForms_Partial
{
   public partial class mainForm : Form
   {
       public mainForm()
       {
           InitializeComponent();
       }
       
       // Declare Class-Level variables (note: declare variables as locally as possible)
       private int cRandomNum;         // random num for current game  (user to guess)
       private int cNumGuesses;        // store number of guesses for the current game
       private int cTotalGames;        // store total number of games played in session
       private int cTotalGuesses;      // store the total number of guesses
       // Note: Private Class Variables - available to all code in this class (ie Form)
       // Note: Public Variables - available to any code anywhere (not a good idea... )
       // Note: Use 'Accessor Methods' in 'Property Blocks' to pass data between Forms.

       
       private int GetRandomNum()
       // by DG 23/03/10 - method to generate and return random number between 1 and 100
       {
           Random objRandom = new Random();            //create instance of Random Class
           // get random number between 1 and 100 (seed random generator - N/R test...)
           //int RandomNum = objRandom.Next(1, 101);     //Min = 1, Max = 100 (Note: 101)
           int RandomNum = objRandom.Next(1, 4);     //for testing (Min = 1, Max = 3)
           return RandomNum;           //Note: Methods that are not void return a value
       }
       
       
       private void mainForm_Load(object sender, EventArgs e)
       //by DG 23/03/10 - Modularise - call General Method 'SetUpNewGame'
       {
           Console.WriteLine(GetRandomNum().ToString());   //Debugging... (test)
           SetUpNewGame();     //called by 'Form_Load' & 'playButton_Click' events    
       }


       private void playButton_Click(object sender, EventArgs e)
       //by DG 23/03/10 - Modularise - call General Method 'SetUpNewGame'
       {
           Console.WriteLine(GetRandomNum().ToString());   //Debugging... (test)
           SetUpNewGame();     //called by Form_Load & playButton_Click events
       }


       private void SetUpNewGame()
       {
           // Method by DG 23/03/10 - reset Number of Guesses, increment Total Games,
           // use GetRandomNum method to generate a random number between 1 and 100,
           // disable 'playButton', clear Labels & TextBox, and set focus to TextBox

           // reset class-level variable, that tracks number of guesses, back to zero
           cNumGuesses = 0;  // new game just started (number of guesses must be zero)

           // use class-level variable to keep track of total number of games played...
           cTotalGames = cTotalGames + 1;      // OR cTotalGames += 1; OR cTotalGames++;

           // display number of games to user on the Form at top of Group Box
           gameGroupBox.Text = "Game No. " + cTotalGames.ToString();
           
           cRandomNum = GetRandomNum();    // call method to generate new random number

           playButton.Enabled = false;     // disable playButton (enable at end of game)
           resultLabel.Text = "";          // clear Labels & TextBox ready for new game
           guessLabel.Text = "";
           numberTextBox.Text = "";        
           numberTextBox.Focus();          // set Text Box focus ready for user input...
       }


       private void numberTextBox_KeyPress(object sender, KeyPressEventArgs e)
       // by DG 23/03/10 - check if user has guessed the number (after Enter Key pressed)
       {
          // Note: Enter Key N/A via KeyPress (use KeyUp event re. Bradley C# Text p.593)
       }


       private void numberTextBox_KeyUp(object sender, KeyEventArgs e)
       // by DG 23/03/10 - check if user has guessed the number (after Enter Key pressed)
       {
           int GuessNum = 0;           //local variable to store user's current guess

           // if the Enter Key is pressed... (also make sure Text Box is not empty)
           if (e.KeyCode == Keys.Enter && numberTextBox.Text != "")      
           {
               //MessageBox.Show("Enter Key pressed")   //Debugging - Test 'Enter Key'
               Console.WriteLine("Enter Key pressed");  //Debugging - Immediate Window

               try
               {
                   GuessNum = int.Parse(numberTextBox.Text);      // input guess number
                   Console.WriteLine("Number guessed: " + GuessNum);    //Debugging...

                   //Student Ex. Do RANGE VALIDATION - Number to be between 1 and 100
                                       
                   PlayTheGame(GuessNum, cRandomNum);              // Call Method
               }
               catch (Exception)
               {
                   MessageBox.Show("Please enter a whole number between 1 and 100");
                   numberTextBox.Text = "";    // clear invalid data from Text Box      
               }
           }
       }


       private void PlayTheGame(int GuessNum, int randomNum)       // General Method
       //by DG 23/03/10 - Play Game (compare GuessNum to RandomNum and advise user)
       {

        cNumGuesses = cNumGuesses + 1;              // increment number of guesses (+1)
        guessLabel.Text = cNumGuesses.ToString();   // display number of guesses so far
               
        numberTextBox.SelectAll();                  // select text ready for re-entering
               
        if (GuessNum == randomNum)                 // if user guesses correctly then
            {   resultLabel.Text = "CORRECT";       // game over - prepare new game!!

                updateSummaryData(cNumGuesses);      // ToDo: write General Method
          
                playButton.Enabled = true;          // re-enable the 'playButton'
                playButton.Focus();                 // move focus to 'playbutton'
            }
        else {
               if (GuessNum < randomNum) {
                    resultLabel.Text = "Too Low";       // guess is too low
                }
                else {
                    resultLabel.Text = "Too High";      // guess is too high
               }
            }
       }
       private void exitButton_Click(object sender, EventArgs e)
       // by DG 23/03/10 - close mainForm and exit application
       {
           this.Close();
       }
   }
}

riquenunes
  • riquenunes

    Soldier

  • Members
  • Joined: 28 Apr 2008
  • Brazil

#3

Posted 01 November 2012 - 06:23 PM

It's because you're checking if the button text is equal/higher/lower than the variable num instead of the textbox text.

Indi
  • Indi

    Don't cross the streams.

  • Andolini Mafia Family
  • Joined: 22 Nov 2008
  • None

#4

Posted 01 November 2012 - 10:24 PM

Thanks man.

It worked.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users