ProgLab1_winforms/GUI/GuessForm.cs

102 lines
3.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using laba3.Subprograms;
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 ProgLab1.GUI
{
public partial class GuessForm : Form
{
private double answer;
private int tryes;
private Label tryCount;
private TextBox input;
public void SetAnswer(double answer) { this.answer = answer; tryes = 3; tryCount.Text = $"Количество попыток: {tryes}"; }
public GuessForm(Form onCloseForm)
{
InitializeComponent();
this.FormClosing += new FormClosingEventHandler((object sender, FormClosingEventArgs e) => { e.Cancel = true; this.Hide(); onCloseForm.Show(); });
input = new TextBox();
input.Top = 100;
input.Left = 50;
input.Width = 300;
Label inputLabel = new Label();
inputLabel.Top = 70;
inputLabel.Left = 50;
inputLabel.Text = "Введите число:";
Button confirmButton = new Button
{
Text = "Подтвердить",
Width = 100,
Height = 30,
Top = 100,
Left = 400
};
tryCount = new Label();
tryCount.Top = 40;
tryCount.Left = 50;
tryCount.Width = 400;
tryCount.Text = $"Количество попыток: {tryes}";
confirmButton.Click += ConfirmButton_Click;
this.Controls.Add(inputLabel);
this.Controls.Add(input);
this.Controls.Add(confirmButton);
this.Controls.Add(tryCount);
}
private bool ValidateInput(TextBox textBox)
{
if (double.TryParse(textBox.Text, out _))
{
return true;
}
else
{
return false;
}
}
private void ConfirmButton_Click(object sender, EventArgs e)
{
bool isValid = ValidateInput(input);
if (!isValid)
{
input.BackColor = Color.Red;
}
else
{
input.BackColor = SystemColors.Window;
double inputNum = double.Parse(input.Text);
if (Math.Abs(answer - inputNum) < 0.01)
{
Dialogs.GameOver("Победа!\nВы угадали значение функции!");
this.Close();
}
else
{
tryes--;
tryCount.Text = $"Количество попыток: {tryes}";
if (tryes <= 0)
{
Dialogs.GameOver($"Попытки кончились :(\nВерный ответ был {answer:F2}");
this.Close();
}
}
}
}
}
}