I am working on a random number guessing game for my class and so far I have not really been able to figure out where I am going wrong. As far as I can tell my syntax is correct, I think that maybe I am just missing something. Any help would be greatly appreciated.

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;

namespace GuessANumber
{
public partial class GuessANumber : Form
{
    int correctAnswer;
    int randomNumB;
    public GuessANumber()
    {
        InitializeComponent();
        Random RandomNumGernerator = new Random();
        correctAnswer = RandomNumGernerator.Next(1,6);

        Random RandomNum = new Random();            
        randomNumB = RandomNum.Next(1, 5);
        while (randomNumB == correctAnswer)
            randomNumB = RandomNum.Next(1, 6);
    }

    private void exitToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }

    private void numGuess1_CheckedChanged(object sender, EventArgs e)
    {
        if (Convert.ToInt16(numGuess1.Text) == correctAnswer)
            this.lblOutput.Text = "correct answer";
        else
            this.lblOutput.Text = "incorrect answer";
    }

    private void numGuess2_CheckedChanged(object sender, EventArgs e)
    {
        if (Convert.ToInt16(numGuess2.Text) == correctAnswer)
            this.lblOutput.Text = "correct answer";
        else
            this.lblOutput.Text = "incorrect answer";
    }

    private void numGuess3_CheckedChanged(object sender, EventArgs e)
    {
        if (Convert.ToInt16(numGuess3.Text) == correctAnswer)
            this.lblOutput.Text = "correct answer";
        else
            this.lblOutput.Text = "incorrect answer";
    }

    private void numGuess4_CheckedChanged(object sender, EventArgs e)
    {
        if (Convert.ToInt16(numGuess4.Text) == correctAnswer)
            this.lblOutput.Text = "correct answer";
        else
            this.lblOutput.Text = "incorrect answer";
    }

    private void numGuess5_CheckedChanged(object sender, EventArgs e)
    {

        if (Convert.ToInt16(numGuess5.Text) == correctAnswer)
            this.lblOutput.Text = "correct answer";
        else
            this.lblOutput.Text = "incorrect answer";
    }

    private void lblOutput_MouseHover(object sender, EventArgs e)
    {
        lblOutput.Text = "The correct button is not #" + randomNumB.ToString();  
    }
  }
}

What is it doing (wrong)?

When it runs and I make a choice, I get no indication that I made a choice other than the radio button being selected. when I hover over the label it tells me which one is not correct but that is all it does.

did you "attach" any buttons? like a button to activate something?

In the if statement I have a reference to the random number, I'm not positive but when you click on one of the radio buttons you are supposed to get a message stating whether or not you made a correct choice.

I would do away with all the conversion work you have going on in Convert.ToInt16(numGuess5.Text) and the others because it's just unnecessary since they're static.

private void numGuess2_CheckedChanged(object sender, EventArgs e)
    {
        if (correctAnswer == 2)
            this.lblOutput.Text = "correct answer";
        else
            this.lblOutput.Text = "incorrect answer";
    }

and do that for all the others-- 1, 3, 4, 5.
Even easier:

     private void numGuess2_CheckChanged(object sender, EventArgs e) 
     {
         if (correctAnswer == 2) MessageBox.Show("Correct!");
         else MessageBox.Show("Incorrect :(");
     }

Perhaps it's something about the way those labels work. I'm a beginner as well, so this is just my thoughts.

I actually just noticed something interesting: putting the message boxes, they pop up two at a time when a box is clicked, once at start-up, and once on exit. (I made a version of this game really quick.) Changing the event from CheckChanged to Validated helped eliminate the double boxes. Something is wacky with the way the radio buttons CheckChanged event is called, so I'd switch to Validated. Even then, though, the box pops up on exit of the application. Some kind of internal event handling I suppose.

In any case, here's the Form of the game (tooltip included as well), followed by the simple logic.

namespace GuessingGame
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.radioButton1 = new System.Windows.Forms.RadioButton();
            this.radioButton2 = new System.Windows.Forms.RadioButton();
            this.radioButton3 = new System.Windows.Forms.RadioButton();
            this.radioButton4 = new System.Windows.Forms.RadioButton();
            this.radioButton5 = new System.Windows.Forms.RadioButton();
            this.label1 = new System.Windows.Forms.Label();
            this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
            this.SuspendLayout();
            // 
            // radioButton1
            // 
            this.radioButton1.AutoSize = true;
            this.radioButton1.Location = new System.Drawing.Point(35, 42);
            this.radioButton1.Name = "radioButton1";
            this.radioButton1.Size = new System.Drawing.Size(31, 17);
            this.radioButton1.TabIndex = 0;
            this.radioButton1.TabStop = true;
            this.radioButton1.Text = "1";
            this.radioButton1.UseVisualStyleBackColor = true;
            this.radioButton1.Validated += new System.EventHandler(this.radioButton1_Validated);
            // 
            // radioButton2
            // 
            this.radioButton2.AutoSize = true;
            this.radioButton2.Location = new System.Drawing.Point(35, 65);
            this.radioButton2.Name = "radioButton2";
            this.radioButton2.Size = new System.Drawing.Size(31, 17);
            this.radioButton2.TabIndex = 0;
            this.radioButton2.TabStop = true;
            this.radioButton2.Text = "2";
            this.radioButton2.UseVisualStyleBackColor = true;
            this.radioButton2.Validated += new System.EventHandler(this.radioButton2_Validated);
            // 
            // radioButton3
            // 
            this.radioButton3.AutoSize = true;
            this.radioButton3.Location = new System.Drawing.Point(35, 88);
            this.radioButton3.Name = "radioButton3";
            this.radioButton3.Size = new System.Drawing.Size(31, 17);
            this.radioButton3.TabIndex = 0;
            this.radioButton3.TabStop = true;
            this.radioButton3.Text = "3";
            this.radioButton3.UseVisualStyleBackColor = true;
            this.radioButton3.Validated += new System.EventHandler(this.radioButton3_Validated);
            // 
            // radioButton4
            // 
            this.radioButton4.AutoSize = true;
            this.radioButton4.Location = new System.Drawing.Point(35, 111);
            this.radioButton4.Name = "radioButton4";
            this.radioButton4.Size = new System.Drawing.Size(31, 17);
            this.radioButton4.TabIndex = 0;
            this.radioButton4.TabStop = true;
            this.radioButton4.Text = "4";
            this.radioButton4.UseVisualStyleBackColor = true;
            this.radioButton4.Validated += new System.EventHandler(this.radioButton4_Validated);
            // 
            // radioButton5
            // 
            this.radioButton5.AutoSize = true;
            this.radioButton5.Location = new System.Drawing.Point(35, 134);
            this.radioButton5.Name = "radioButton5";
            this.radioButton5.Size = new System.Drawing.Size(31, 17);
            this.radioButton5.TabIndex = 0;
            this.radioButton5.TabStop = true;
            this.radioButton5.Text = "5";
            this.radioButton5.UseVisualStyleBackColor = true;
            this.radioButton5.Validated += new System.EventHandler(this.radioButton5_Validated);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(24, 168);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(75, 13);
            this.label1.TabIndex = 1;
            this.label1.Text = "Take a guess!";
            this.label1.MouseHover += new System.EventHandler(this.label1_MouseHover);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(346, 328);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.radioButton5);
            this.Controls.Add(this.radioButton4);
            this.Controls.Add(this.radioButton3);
            this.Controls.Add(this.radioButton2);
            this.Controls.Add(this.radioButton1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.RadioButton radioButton1;
        private System.Windows.Forms.RadioButton radioButton2;
        private System.Windows.Forms.RadioButton radioButton3;
        private System.Windows.Forms.RadioButton radioButton4;
        private System.Windows.Forms.RadioButton radioButton5;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.ToolTip toolTip1;

        }
    }



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

    namespace GuessingGame
    {
        public partial class Form1 : Form
        {
            int correctAnswer;
            int incorrectAnswer;


            public Form1()
            {
                InitializeComponent();
                Random gen = new Random();
                correctAnswer = gen.Next(1,6);
                while ((incorrectAnswer = gen.Next(1,6)) == correctAnswer);
            }

            private void label1_MouseHover(object sender, EventArgs e)
            {
                string text = "The correct answer is NOT " + incorrectAnswer;
                toolTip1.SetToolTip(label1, text);
            }

            private void radioButton5_Validated(object sender, EventArgs e)
            {
                if (correctAnswer == 5) MessageBox.Show("Correct!");
                else MessageBox.Show("Incorrect!");
            }

            private void radioButton4_Validated(object sender, EventArgs e)
            {
                if (correctAnswer == 4) MessageBox.Show("Correct!");
                else MessageBox.Show("Incorrect!");
            }

            private void radioButton3_Validated(object sender, EventArgs e)
            {
                if (correctAnswer == 3) MessageBox.Show("Correct!");
                else MessageBox.Show("Incorrect!");
            }

            private void radioButton2_Validated(object sender, EventArgs e)
            {
                if (correctAnswer == 2) MessageBox.Show("Correct!");
                else MessageBox.Show("Incorrect!");
            }

            private void radioButton1_Validated(object sender, EventArgs e)
            {
                if (correctAnswer == 1) MessageBox.Show("Correct!");
                else MessageBox.Show("Incorrect!");
            }

        }
    }

I tried to use the code you have with the Validated events and the modified random number generator. I was not able to get anyof those changes to work so is there some way to modify what I have to make it work the way it's supposed to?

I'm not sure which version of Visual Studio you are using, but they should be similar. in 12 Beta, click on of the radio buttons and go to the Events Tab (should be a tab next to the Properties tab where you change the text and whatnot). Right click the "CheckChanged" event and press Reset. That will un-associate the event with your method. Then, scroll down to the Validated event and double click it. This will (at least in VS12B), create a method stub for the radioButton_Validated event. This is where you can put the code. Here is the project file for the version I made, if you want to take a look at it. Once again, this is using VS 12 Beta, so I'm not sure if it's compatible with your version.

project file in zip

I have Visual Studio 2010, it won't read the file. I did download the 2011 beta once I get some dvd's to burn the ISO I can try it and see what you have.

I was able to get yours to work just fine in 2011 and I did try your code in my program, it still does not work. I think I will just wait and ask my instructor maybe he'll be able to figure out why my code is not working, thanks for all the help.

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.