Hi all,
I'm having a little issue working with this Guessing Game I'm trying to make, I've already mad it in a console on C++ but im finding Windows forms a bit tedious =/

What issues am I having?
Well, Im ussing combo boxes for generating a random number, so user gets to choose from the combobox whether he/she'd like to generate a random number between 1-5, 1-10 or 1-20 I also have another combobox for the number of guesses (1,3,5).

I also have a test program where a user ticks the test box and he/she gets to choose whether to enter the guess number or not, here's what I have so far:

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;
using System.IO;

namespace NumberGuessingGame_08029490
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            testNumberTextBox.Visible = false;
            enterGuessNumberLabel.Visible = false;
        }
        //Declare the variables.
            int testDecimal, guessDecimal;
            

    


        private void testCheckBox_CheckedChanged(object sender, EventArgs e)
        {

                       
            testNumberTextBox.Visible = true;
            enterGuessNumberLabel.Visible = true; 

            if (testCheckBox.Checked == true)
            {
                testDecimal = int.Parse(testNumberTextBox.Text);
            }

            else 
            {
                testDecimal = rangeNumbersComboBox.SelectedIndex;
            }

                
            
        }



        private void guessButton_Click(object sender, EventArgs e)
        {



            //Convert inputs to values.
            guessDecimal = int.Parse(guessNumberTextBox.Text);


            //Actions (test).

            if (guessDecimal < testDecimal)
            {
                guessResultTextBox.Text = "Your Guess is too low, try again";
            }
            if (guessDecimal > testDecimal)
            {
                guessResultTextBox.Text = "Your Guess is too high, try again";
            }
            if (guessDecimal == testDecimal)
            {
                guessResultTextBox.Text = "Correct!!!";
            }
        }


          

        private void rangeNumbersComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            if (rangeNumbersComboBox.SelectedIndex == 0)       
            {
                Random randomNum = new Random();
                int randomNumFive = randomNum.Next(1, 5);

            }
            if (rangeNumbersComboBox.SelectedIndex == 1)
            {
                Random randomNum = new Random();
                int randomNumTen = randomNum.Next(1, 10);
               

            }
            if (rangeNumbersComboBox.SelectedIndex == 2)
            {
                Random randomNum = new Random();
                int randomNumTwenty = randomNum.Next(1, 20);
                

            }
        }

Im getting errors when I tick the check box saying: "Input string was not in correct format"

and whenever I choose a value from the range combobox its always 0!!! :O


I'm wondering if I'm going to have to data bind the values for each combobox value, if so how do i do this? Ive looked on the web, nothing.


Help!!! xxxx

Hi,

Firstly the error you are getting is probably a result of the int.Parse(). If the textbox is empty then the parse will fail.
I highly recommend using int.TryParse instead. It outputs the result to a variable and returns a bool to indicate if the parse succeeded. It wont throw an exception if the input is incorrectly formatted:

if (testCheckBox.Checked == true)
            {
                if(!int.TryParse(testNumberTextBox.Text, out testDecimal))
                {
                    MessageBox.Show("Invalid input");
                } 
            }
            else 
            {
                testDecimal = rangeNumbersComboBox.SelectedIndex;
            }
        }

I have used the boolean returned to show a messagebox if the parse fails.

Also, you may want to change:

testNumberTextBox.Visible = true;
            enterGuessNumberLabel.Visible = true;

to

testNumberTextBox.Visible = testCheckBox.Checked;
            enterGuessNumberLabel.Visible = testCheckBox.Checked;

This way if you uncheck the box it will hide the controls.

When you say you get zero when selecting a value from the range box, do you mean the selectedIndex is zero or that the random number is always zero?

Edited 6 Years Ago by Geekitygeek: typo

Thanks for your advice so far Ryshad...
Well I'm not sure why it keeps returning zero, I think it could have something to do with the seleted index, because i've tested the random number before by outputting it into a textbox and it works (all three of them). I think the problem is that the selected index is returning 0 because im using it as testdecimal, but I cant think of another way to do this.

You arent actually doing anything with the random numbers. You store them in either randomNumFive, randomNumTen or randomNumTwenty but they only exist within the if statement. Are you sure you didnt mean to store those as testDecimal?

This article has been dead for over six months. Start a new discussion instead.