C# Button Click and Private Variables - c#

So I'm trying to make a window forms guess the number game, simple but when I click guess no matter what the label goes up by one. I think it may be due to my variables as despite having them global userGuess still comes up as a local variable...`
Commenting out userScore removes the problem, but I still do not understand why the logic is failing
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;
using System.Drawing;
namespace Guess_The_Number_Form
{
public partial class Form1 : Form
{
private int userScore;
private int randNum;
private int userGuess;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
txtBoxGuess.Hide()
;
}
private void toolTip1_Popup(object sender, PopupEventArgs e)
{
}
private void btnRandom_Click(object sender, EventArgs e)
{
Random rand = new Random();
int randNum = rand.Next(0, 10);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
userGuess = Convert.ToInt32(txtBoxGuess.Text);
}
private void txtBoxGuess_Enter(object sender, EventArgs e)
{
}
private void btnGuess_Click(object sender, EventArgs e)
{
if (userGuess == randNum)
{
// userScore++;
lbluserScore.Text = userScore.ToString();
lbluserScore.Text = $"{userScore}";
}
else if (userGuess != randNum)
{
userScore--;
lbluserScore.Text = userScore.ToString();
lbluserScore.Text = $"{userScore}";
}
else if (userScore < 0)
{
lbluserScore.Text = Color.Red.ToString();
}
}
}
}

You need to change this:
private void btnRandom_Click(object sender, EventArgs e)
{
Random rand = new Random();
int randNum = rand.Next(0, 10);
}
to
private void btnRandom_Click(object sender, EventArgs e)
{
Random rand = new Random();
randNum = rand.Next(0, 10);
}
That way, you'll set the member variable randNum rather than a local variable randNum. You also want to check that you really are assigning the user-inputted value into userGuess properly. My guess is that it's not, which means you never change the value of either of those variables, and the program thinks the user always guessed the right value.

Related

Checking if the correct label was clicked?

to start of Im not good at programming and I am completely new to it. With that said, I am trying to make a game, where a pattern of labels show up (by the speciffic labels changing colors), and then the user has to click that speciffic pattern after it has been shown. I have already made the pattern show, and put into a list. The problem I now have is how I am going to check if the correct label was licked, acording to the random pattern that has been made. Sorry if my code seems clumsy, but here it is (sorry that there are no commemts yet also):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Spil
{
public partial class Form1 : Form
{
Random rnd = new Random();
Label[] labelArray;
int turn = 1;
int lives = 3;
List<Label> orderList = new List<Label>();
public Form1()
{
InitializeComponent();
labelArray = new Label []{ label1, label2, label3, label4, label5, label6, label7, label8, label9 };
}
private void DisplayOrder()
{
for (int i = 0; i < labelArray.Length; i++)
{
labelArray[i].BackColor = Color.Blue;
}
for (int i = -2; i < turn; i++)
{
int chosenNumber = rnd.Next(0, 9);
labelArray[chosenNumber].BackColor = Color.Green;
Thread.Sleep(1000);
labelArray[chosenNumber].BackColor = Color.Blue;
orderList.Add(labelArray[chosenNumber]);
}
}
private void Click0(object sender, EventArgs e)
{
}
private void Click1(object sender, EventArgs e)
{
}
private void Click2(object sender, EventArgs e)
{
}
private void Click3(object sender, EventArgs e)
{
}
private void Click4(object sender, EventArgs e)
{
}
private void Click5(object sender, EventArgs e)
{
}
private void Click6(object sender, EventArgs e)
{
}
private void Click7(object sender, EventArgs e)
{
}
private void Click8(object sender, EventArgs e)
{
}
private void Click9(object sender, EventArgs e)
{
}
private void Form1_Shown(object sender, EventArgs e)
{
System.Timers.Timer t = new System.Timers.Timer(100);
t.Elapsed += t_Elapsed;
t.Start();
}
void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
((System.Timers.Timer)sender).Stop();
DisplayOrder();
}
}
}
You can have all your labels registered for the same click event and use the sender parameter to identify the clicked label.
for (int i = 0; i < labelArray.Length; i++)
{
labelArray[i].BackColor = Color.Blue;
labelArray[i].Click += label_Click;
}
void label_Click(object sender, EventArgs e)
{
string name = ((Label)sender).Name;
}
You need to generate the Click even for each individual label,you can find the events tab here with the properties tab (in case you didn't know).Simply lick your label in the designer and navigate to the label_click event and double click it.

Using Visual Basic to Make Average Test Score Generator in C#

I'm trying to create a calculator for an assignment that takes three integer inputs and takes the average and returns that to user. I'm using Visual Basic (it is required) to make the GUI. I'm having trouble with two things, first, I cannot get the aVer to divide by 3 because it is not a integer and second, I do not know how to get an output with the average in the last textbox.
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
private string userInfo1;
private string userInfo2;
private string userInfo3;
private string aVer;
private string num1;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int aVer = 0;
aVer = Int32.Parse(userInfo1 + userInfo2 + userInfo3);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
int userInfo1 = 0;
userInfo1 = Int32.Parse(textBox1.Text);
}
private void button2_Click(object sender, EventArgs e)
{
Close();
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
int userInfo2 = 0;
userInfo2 = Int32.Parse(textBox2.Text);
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
int userInfo3 = 0;
userInfo3 = Int32.Parse(textBox3.Text);
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
int num = Int32.Parse(aVer);
MessageBox.Show(num.ToString());
}
private void label3_Click(object sender, EventArgs e)
{
}
}
}
You are declaring your variables multiple times. The variables inside the function calls will hide the variables you have defined outside in the class declaration, and the class variables will never be set to a value.
It doesn't appear that you are ever using any of the variables in their string form, so all the private string declarations should be changed to private int declarations. This also makes the int userInfo1 = 0; declarations unnecessary. Having them actually breaks the ability to see the values in the button1_Click function.

Looping back to start

I am building this program for my CIT class and want to have it loopback and create a new number when the number is guessed correctly. I read over the section in my textbook that covers this but I am a little confused as to how to add this and where exactly.
Here is the code I have now:
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 Guess_My_Number
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int number;
private void Form1_Load(object sender, EventArgs e)
{
// Generate the number.
Random generator = new Random();
number = generator.Next(0, 100);
MessageBox.Show("Can you guess the number I am thinking of
between 1 and 100?");
}
private void guessButton_Click(object sender, EventArgs e)
{
// Get the guess from the textbox.
int guess = Convert.ToInt32(guessTextbox.Text);
// Check if the number is right.
if (guess > number)
{
MessageBox.Show("Too high, try again.");
}
if (guess < number)
{
MessageBox.Show("Too low, try again.");
}
if (guess == number)
{
MessageBox.Show("Congratulations, you guessed my number!");
}
}
private void exitButton_Click(object sender, EventArgs e)
{
// Clost the program.
this.Close();
}
}
}
namespace Guess_My_Number
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int number;
Random generator = new Random();
private void Form1_Load(object sender, EventArgs e)
{
GenerateNewNumber();
}
private void GenerateNewNumber()
{
number = generator.Next(0, 100);
MessageBox.Show("Can you guess the number I am thinking of
between 1 and 100?");
}
private void guessButton_Click(object sender, EventArgs e)
{
// Get the guess from the textbox.
int guess = Convert.ToInt32(guessTextbox.Text);
// Check if the number is right.
if (guess > number)
{
MessageBox.Show("Too high, try again.");
}
if (guess < number)
{
MessageBox.Show("Too low, try again.");
}
if (guess == number)
{
MessageBox.Show("Congratulations, you guessed my number!");
GenerateNewNumber();
}
}
private void exitButton_Click(object sender, EventArgs e)
{
this.Close();
}
}
}

C# number guessing game

I'm trying to create a simple number guessing game in C#. The program generates a random number when you click the "Generate random number" button. Once you enter your guess in the textbox and click "Guess", the program lets you know if you've guessed right or wrong.
The problem is that I can't pass the randomly generated number to myFunction() so it can validate the user's guess. Here's the code and it's a bit of a mess; and thank you all in advance for your help.
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 Number_guessing_game
{
public partial class Form1 : Form
{
int montH;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
myFunction(int montH);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
Random rnd = new Random();
int montH = rnd.Next(1, 10);
}
void myFunction(int montH)
{
int guess = int.Parse(textBox1.Text);
if (guess == montH)
{
MessageBox.Show("You win!");
}
if (guess != montH)
{
MessageBox.Show("You lose!");
}
}
}
}
The problem is in your button2_Click function. Inside the function after you generate a random number, you are creating a new int and assigning the random number to that, instead of the already declared class-level variable. Change your button2_Click to the following:
private void button2_Click(object sender, EventArgs e)
{
montH = new Random().Next(1,10);
}
Another few things I will mention, because montH is a class-level variable, it is accessible by every function in your class, so you don't need to pass it as a parameter to myFunction(), in fact, button1_Click could validate the result for you:
private void button1_Click(object sender, EventArgs e)
{
if (int.Parse(TextBox1.Text) == montH)
{
MessageBox.Show("You Win!!");
}
else
{
MessageBox.Show("You Lose...");
}
}
Simply define your montH as a class level variable(as you did), then use following code:
int montH;
private void button2_Click(object sender, EventArgs e)
{
Random rnd = new Random();
montH = rnd.Next(1, 10); //<--- there is no need to redefine montH by int monthH
}
private void GussButton_Click(object sender, EventArgs e)
{
myFunction(month);
}
If in your button2_click event handler want to use it there isn't need to redefine it by int montH;,
because int montH; cause define a new montH variable in method level.
Change your code to:
private void button2_Click(object sender, EventArgs e)
{
Random rnd = new Random();
montH = rnd.Next(1, 10);
}
Try this below:
namespace Number_guessing_game
{
public partial class Form1 : Form
{
public int montH;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
myFunction(montH);
}
private void Form1_Load(object sender, EventArgs e)
{
montH=0;
}
private void button2_Click(object sender, EventArgs e)
{
Random rnd = new Random();
montH = rnd.Next(1, 10);
}
void myFunction(montH)
{
int guess = int.Parse(textBox1.Text);
if (guess == montH)
{
MessageBox.Show("You win!");
}
if (guess != montH)
{
MessageBox.Show("You lose!");
}
}
}
}
Don't create a new montH in button2_Click()... otherwise the new montH only alive in this function
private void button2_Click(object sender, EventArgs e)
{
Random rnd = new Random();
montH = rnd.Next(1, 10);
}
I hope the below works for you ! What you dont need to do is actually put your montH in your Function itself since it is a seen variable to the class. When you are comparing the guess ==montH it is already taking the montH int assigned and checking from your montH variable.
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 Number_guessing_game
{
public partial class Form1 : Form
{
int montH;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
myFunction();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
Random rnd = new Random();
int montH = rnd.Next(1, 10);
}
void myFunction()
{
int guess = int.Parse(textBox1.Text);
if (guess == montH)
{
MessageBox.Show("You win!");
}
if (guess != montH)
{
MessageBox.Show("You lose!");
}
}
}
}

adding a counter class to winform

I have a project that wants
A method that returns the current count.
A Constructor that sets the count to zero.
I have the first few down but need help with the return count to 0 and then the constructor. I need to do this by adding a counter class but I'm confused about the way to add it.
Can some one help me out?
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 Project10TC
{
public partial class Form1 : Form
{
int zero = 0;
int i = 1;
public Form1()
{
InitializeComponent();
}
private EventHandler myCounter;
// end of Form class
private class myCounter()
{
myCounter = new myCounter( );
}
private void exitToolStripMenuItem1_Click(object sender, EventArgs e)
{
this.Close();
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("Teancum Clark\nCS 1400\n Project 10");
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = (++i).ToString();
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = (--i).ToString();
}
private void button3_Click(object sender, EventArgs e)
{
textBox1.Text = (zero).ToString();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
public class Counter
{
public int Value { get; private set; }
public void Increment()
{
Value = Value + 1;
}
public void Decrement()
{
if (Value > 0) Value = Value - 1;
}
public Counter()
{
Value = 0;
}
}

Categories

Resources