ProgLab1_winforms/GUI/GuessForm.cs

102 lines
2.8 KiB
C#

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);
Console.WriteLine(Math.Abs(answer - inputNum));
if (Math.Abs(answer - inputNum) < 0.1)
{
}
else
{
tryes--;
}
if(tryes <= 0)
{
this.Close();
}
}
tryCount.Text = $"Количество попыток: {tryes}";
}
}
}