I made a number guessing game but there is a problem. When the same number was entered in the Textbox, I thought of printing an error message with the MessageBox, but I could not. I tried Contain and IndexOf but it didn't. For example, I entered the number
1 in the textbox first, how can I prevent entering the number 1 again?
I made a number guessing game but there is a problem. When the same number was entered in the Textbox, I thought of printing an error message with the MessageBox, but I could not. I tried Contain and IndexOf but it didn't. For example, I entered the number
1 in the textbox first, how can I prevent entering the number 1 again?
Pretty simple. Write code that keeps track of the the numbers entered by adding he number to a list. Check if the number exists in the list. If you have already written the code and the code is not functioning as expected and you want a community code
review, then share the code.
I made a number guessing game but there is a problem. When the same number was entered in the Textbox, I thought of printing an error message with the MessageBox, but I could not. I tried Contain and IndexOf but it didn't. For example, I entered the number
1 in the textbox first, how can I prevent entering the number 1 again?
private void MoveToTheNextQuestion()
{
previousAnswers.Clear();
MessageBox.Show("Correct answer! Get ready for a new question.");
}
private bool IsAnswerCorrect(char answer)
{
var result = false;
char randomAnswer = '3';
if (randomAnswer == answer)
result = true;
return result;
}
List<char> previousAnswers = new List<char>();
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
// get the new answer
var newAnswer = e.KeyChar;
if (char.IsDigit(newAnswer) || char.IsControl(newAnswer))
{
if(previousAnswers.Count > 0)
{
// get the previous answer
var previousAnswer = previousAnswers.Last();
if(previousAnswer == newAnswer)
{
//-- ignore the answer
e.Handled = true;
}
else
{
previousAnswers.Add(newAnswer);
//-- check the correctness of the answer
if (IsAnswerCorrect(newAnswer) == true)
//-- move to the next question
MoveToTheNextQuestion();
else
//-- allow them to try again
textBox1.Text = string.Empty;
}
}
else
{
previousAnswers.Add(newAnswer);
//-- check the correctness of the answer
if (IsAnswerCorrect(newAnswer) == true)
//-- move to the next question
MoveToTheNextQuestion();
else
//-- allow them to try again
textBox1.Text = string.Empty;
}
}
else
{
// disable non-numeric entries
e.Handled = true;
}
}
. When the same number was entered in the Textbox, I thought of printing an error message with the MessageBox, but I could not. I tried Contain and IndexOf but it didn't. For example, I entered the number 1 in the textbox first, how can I prevent entering the
number 1 again?
Can you show me you code?
I made demo for you as a reference.
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="guess" OnClick="Button1_Click"/>
List<int> num = new List<int>();
protected void Button1_Click(object sender, EventArgs e)
{
var num1 = (List<int>)Session["num"];
if (num1 == null)
{
if (Convert.ToInt32(TextBox1.Text) == 5)
{
Response.Write("you guessed right");
TextBox1.Text = string.Empty;
}
else
{
Response.Write("You guessed wrong, please re-enter");
num.Add(Convert.ToInt32(TextBox1.Text));
Session["num"] = num;
TextBox1.Text = string.Empty;
}
}
else
{
if (num1.Contains(Convert.ToInt32(TextBox1.Text)))
{
Response.Write("You enter the same number, please re-enter");
TextBox1.Text = string.Empty;
}
else
{
if (Convert.ToInt32(TextBox1.Text) == 5)
{
Response.Write("you guessed right");
TextBox1.Text = string.Empty;
}
else
{
Response.Write("You guessed wrong, please re-enter");
var a = (List<int>)Session["num"];
a.Add(Convert.ToInt32(TextBox1.Text));
Session["num"] = a;
TextBox1.Text = string.Empty;
}
}
}
}
Best regards,
Sam
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
I entered the number 1 in the textbox first, how can I prevent entering the number 1 again?
What if the '1' that they enter is the start of the number '17'? Do you want to stop that? I don't understand what the question means. What do you want to happen?
1) The user enters '1' and clicks OK
2) Your program tells them the number is wrong
3) The user enters '1' and clicks OK
4) What do you want to happen? They have already typed the number, you can't prevent them. Do you want to disable the '1' key on the keyboard????? Do you want to disable the 'OK' button if the input box holds a number they have already typed? What if
they typed it 5 minutes ago, now they have no idea why they can't click 'OK'
Can you explain what it means to 'prevent entering the number 1 again'. Why would that be anything but frustrating for the user?
None
0 Points
2 Posts
How do I prevent the same number from being entered in C# Form Textbox
Apr 26, 2020 12:55 AM|RawShed|LINK
All-Star
53021 Points
23607 Posts
Re: How do I prevent the same number from being entered in C# Form Textbox
Apr 26, 2020 01:22 PM|mgebhard|LINK
Pretty simple. Write code that keeps track of the the numbers entered by adding he number to a list. Check if the number exists in the list. If you have already written the code and the code is not functioning as expected and you want a community code review, then share the code.
Contributor
4232 Points
1147 Posts
Re: How do I prevent the same number from being entered in C# Form Textbox
Apr 26, 2020 05:33 PM|Kulrom|LINK
HTH
My website: ASP.NET Custom Software Development
Contributor
3370 Points
1409 Posts
Re: How do I prevent the same number from being entered in C# Form Textbox
Apr 27, 2020 07:42 AM|samwu|LINK
Hi RawShed,
Can you show me you code?
I made demo for you as a reference.
Best regards,
Sam
None
0 Points
2 Posts
Re: How do I prevent the same number from being entered in C# Form Textbox
Apr 27, 2020 08:00 AM|RawShed|LINK
I did my job with these codes thanks for your help
Participant
1620 Points
927 Posts
Re: How do I prevent the same number from being entered in C# Form Textbox
Apr 27, 2020 08:04 AM|PaulTheSmith|LINK
What if the '1' that they enter is the start of the number '17'? Do you want to stop that? I don't understand what the question means. What do you want to happen?
1) The user enters '1' and clicks OK
2) Your program tells them the number is wrong
3) The user enters '1' and clicks OK
4) What do you want to happen? They have already typed the number, you can't prevent them. Do you want to disable the '1' key on the keyboard????? Do you want to disable the 'OK' button if the input box holds a number they have already typed? What if they typed it 5 minutes ago, now they have no idea why they can't click 'OK'
Can you explain what it means to 'prevent entering the number 1 again'. Why would that be anything but frustrating for the user?