Sign in to follow this  
Mashew

C# Button Troubles

Recommended Posts

Mashew    122
So, I am trying to make a Guess the Number game on C# where you enter the number in a textbox and hit the button and it will tell if it is right or not. Well, the button does nothing. (Yes, I have checked, everything has the right name.) Here is the 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; using System.Threading; namespace Guessthenumber { public partial class Form1 : Form { int rawr; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { l1.Text = "Click the button when you are ready!"; Random num = new Random(); rawr = num.Next(2000000); Thread.Sleep(1); } private void textBox1_TextChanged(object sender, EventArgs e) { } private void progressBar1_Click(object sender, EventArgs e) { } private void b1_Click(object sender, EventArgs e) { if (tb1.Text != Convert.ToString(rawr)) { l1.Text = "wrong!"; tb1.Clear(); } else if (tb1.Text == Convert.ToString(rawr)) { l1.Text = "right!"; l1.Text = Convert.ToString(rawr); Thread.Sleep(10000); } } private void l1_Click(object sender, EventArgs e) { } } }

Share this post


Link to post
Share on other sites
glaeken    294
Have you set a break point inside the b1_clicked event handler to make sure that it's not just your if statements failing? Did you setup the event handler correctly? What does your corresponding designer file look like? Does it contain a line like this for your button? :

this.b1.Click += new System.EventHandler(this.b1_Click);

Also, why are you using an if-else statement? You could just do:

if (tb1.Text != Convert.ToString(rawr))
{
l1.Text = "wrong!";
tb1.Clear();
}
else //right answer
{
l1.Text = "right!";
l1.Text = Convert.ToString(rawr);
Thread.Sleep(10000);
}

Share this post


Link to post
Share on other sites
Mashew    122
Thank you! It works now!

(I have been having trouble with this for like 3 days and finally, I was like "Fine, I will ask". I should have asked like 3 days ago.)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this