Answer the question
In order to leave comments, you need to log in
How to make sure that after the messagebox is closed, the following ones do not open?
Good afternoon, I had to write a program to guess the number, and the script does not work as it should. The messagebox opens, gives out information, and after I close it, the rest of the messageboxes open sequentially, and I need that after closing the messagebox, I could again enter a number.
i enter a number on the form, when kolvopotytok = 3: a messagebox opens, such and such info is displayed, and when I close it, I don’t get to the form, but the next messagebox opens, from the loop where kolvopotytok = 2, etc.
I hope I explained clearly
How to make it so that I would be returned to the form after closing the messagebox
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 template
{
public partial class Form2 : Form
{
private int r;
public Form2(int digitfrom, int digitto)
{
Random rnd = new Random();
r = rnd.Next(digitfrom, digitto);
InitializeComponent();
}
bool IsDigitsOnly(string str)
{
foreach (char c in str)
{
if (c < '0' || c > '9')
return false;
}
return true;
}
private void button1_Click(object sender, EventArgs e)
{
if (IsDigitsOnly(answer.Text) == false)
{
MessageBox.Show("Некорректный ввод данных", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
answer.Text = "";
}
else
{
int a = Convert.ToInt32(answer.Text);
int kolvopotytok = 3;
if (kolvopotytok == 3)
{
if (a > r)
{
MessageBox.Show("Число которое загадал ИИ меньше вашего!\nУ вас осталось 2 попытки.");
answer.Text = "";
kolvopotytok--;
}
else if (a < r)
{
MessageBox.Show("Число которое загадал ИИ больше вашего!\nУ вас осталось 2 попытки.");
kolvopotytok--;
answer.Text = "";
}
else
{
MessageBox.Show("Поздравляем!!! Вы угадали число с первого раза.");
goto here;
}
}
if (kolvopotytok == 2)
{
if (a > r)
{
MessageBox.Show("Число которое загадал ИИ меньше вашего!\n\nУ вас осталась 1 попытка.");
answer.Text = "";
kolvopotytok--;
}
else if (a < r)
{
MessageBox.Show("Число которое загадал ИИ больше вашего!\nУ вас осталась 1 попытка.");
kolvopotytok--;
answer.Text = "";
}
else
{
MessageBox.Show("Поздравляем!!! Вы угадали число со второго раза.");
goto here;
}
}
if (kolvopotytok == 1)
{
if (a > r)
{
MessageBox.Show("Число которое загадал ИИ меньше вашего!\nУ вас осталось 0 попыток.");
answer.Text = "";
kolvopotytok--;
}
else if (a < r)
{
MessageBox.Show("Число которое загадал ИИ больше вашего!\nУ вас осталось 0 попыток.");
kolvopotytok--;
answer.Text = "";
}
else
{
MessageBox.Show("Поздравляем!!! Вы угадали число с третьего раза.");
goto here;
}
}
if (kolvopotytok == 0)
{
MessageBox.Show("К сожелению вы проиграли\nЗагаданное число: " + r);
goto here;
}
here:;
this.Close();
}
}
}
}
Answer the question
In order to leave comments, you need to log in
I’m not sure that I understood the problem correctly, so if it turns out that my answer doesn’t fit, feel free to clarify and ask in the comments, we’ll figure out how to fix it.
The code in the button1_Click handler is executed sequentially. After the first condition works, you decrease the value by one and the next if becomes true too.
The first thing you need to do is set the number of retries to a private variable.
kolvopotytok is not the most appropriate name (and although I, for example, can understand what the number of attempts means here), it is better to use names in English. Let's take countOfAttempts. Since you want to keep track of the number, you need to create another variable to account for their initial (maximum) number:
public Form2(int digitfrom, int digitto)
{
InitializeComponent();
Random rnd = new Random();
r = rnd.Next(digitfrom, digitto);
maxCountOfAttempts = countOfAttempts = 3;
}
private int r;
private int maxCountOfAttempts, countOfAttempts;
private bool CheckResult(int a)
{
if (a == r)
{
MessageBox.Show($"Поздравляем!!! Вы угадали число с {countOfAttempts - 1} раза.");
return true;
}
else if (a > r)
{
MessageBox.Show($"Число которое загадал ИИ меньше вашего!\nУ вас осталось {countOfAttempts - 1} попытки.");
return false;
}
else
{
MessageBox.Show($"Число которое загадал ИИ больше вашего!\nУ вас осталось {countOfAttempts - 1} попытки.");
return false;
}
}
private void button1_Click(object sender, EventArgs e)
{
if (IsDigitsOnly(answer.Text))
{
int a = Convert.ToInt32(answer.Text);
if (CheckResult(a))
this.Close();
else
{
countOfAttempts--;
if (countOfAttempts == 0)
{
MessageBox.Show("К сожелению вы проиграли\nЗагаданное число: " + r);
this.Close();
}
}
}
else
{
MessageBox.Show("Некорректный ввод данных", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
answer.Clear();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question