Добавить min(max) ограничения для ввода чисел в калькулятор - C#
Формулировка задачи:
Всем привет. Написал программу "калькулятор" с помощью форм, но что-то не получается добавить ограничение на ввод больших ( маленьких ) чисел. Например, больше 10000000, чтобы не возможно было ввести.
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
private string operation = "none";
private double a;
private bool clear = false;
public Form1()
{
InitializeComponent();
}
private int b = 0;
//label
private void label1_Click(object sender, EventArgs e)
{
}
//Ввод цифр от 0 до 9
private void Zero_Click(object sender, EventArgs e)
{
if (this.clear == true)
textBox.Clear();
textBox.Text += (sender as Button).Text;
this.clear = false;
}
//Очистить
private void button4_Click(object sender, EventArgs e)
{
textBox.Text = "";
textBox.Focus();
}
// Ввод запятой один раз
private void Zapetaya_Click(object sender, EventArgs e)
{
string z = textBox.Text;
int i = z.IndexOf(",", 0, z.Length); // поиск подстроки "," в строке Z
int j = z.IndexOf("-", 0, z.Length); // поиск подстроки "-" в строке Z
if (!(i != (-1) || z.Length == 0 || (j == 0 && z.Length == 1)))
{
textBox.Text += ",";
}
}
// Выход
private void Exit_Click(object sender, EventArgs e)
{
Close();
}
//Сложение
private void button1_Click(object sender, EventArgs e)
{
{
if (textBox.Text != "")
{
a = double.Parse(textBox.Text);
b = 1; //флаг того, что выполняется операция сложения
textBox.Text = "";
}
else MessageBox.Show("Введите первое число!", "Сообщение");
textBox.Focus();
}
}
//Вычитание
private void button2_Click(object sender, EventArgs e)
{
a = double.Parse(textBox.Text);
b = 2; //флаг того, что выполняется операция вычитание
textBox.Text = "";
label1.Text = "Ввод данных:";
}
//Умножение
private void button5_Click(object sender, EventArgs e)
{
a = double.Parse(textBox.Text);
b = 3; //флаг того, что выполняется операция умножение
textBox.Text = "";
label1.Text = "Ввод данных:";
}
//Деление
private void button6_Click(object sender, EventArgs e)
{
a = double.Parse(textBox.Text);
b = 4; //флаг того, что выполняется операция деление
textBox.Text = "";
label1.Text = "Ввод данных:";
}
//Вычисление синуса
private void button7_Click(object sender, EventArgs e)
{
a = double.Parse(textBox.Text);
b = 5; //флаг того, что выполняется операция sin
textBox.Text = "";
}
//Вычисление косинуса
private void button8_Click(object sender, EventArgs e)
{
a = double.Parse(textBox.Text);
b = 6; //флаг того, что выполняется операция cos
textBox.Text = "";
}
//Извлечение корня (sqrt);
private void button9_Click(object sender, EventArgs e)
{
a = double.Parse(textBox.Text);
b = 7; //флаг того, что выполняется операция извлечения корня
textBox.Text = "";
}
//Возведение в квадрат
private void button10_Click(object sender, EventArgs e)
{
a = double.Parse(textBox.Text);
b = 8; //флаг того, что выполняется операция возведения в квадрат
textBox.Text = "";
}
// 1/x
private void button11_Click(object sender, EventArgs e)
{
a = double.Parse(textBox.Text);
b = 9; //флаг того, что выполняется операция возведения в квадрат
textBox.Text = "";
}
//Factorial
private void button12_Click(object sender, EventArgs e)
{
if (textBox.Text == "")
MessageBox.Show("Введите операнд!");
textBox.Select();
try
{
this.a = double.Parse(textBox.Text);
Exception ex = new Exception();
if (this.a > 100)
throw ex;
double result = 1;
this.operation = "none";
for (int i = 1; i <= this.a; i++)
{
result *= i;
}
this.a = result;
textBox.Text = this.a.ToString();
textBox.Select();
textBox.SelectAll();
}
catch
{
MessageBox.Show("Неподходящее значение поля");
this.a = 0;
textBox.Clear();
textBox.Select();
}
this.clear = true;
}
//Равно
private void button3_Click(object sender, EventArgs e)
{
switch (b)
{
case 1:
{
if (a > 100)
MessageBox.Show("Неподходящее значение поля");
else
textBox.Text = (a + Convert.ToDouble(textBox.Text)).ToString("G4");
label1.Text = "Результат:";
}
break;
case 2:
{
textBox.Text = (a - Convert.ToDouble(textBox.Text)).ToString("G4");
label1.Text = "Результат:";
}
break;
case 3:
{
textBox.Text = (a * Convert.ToDouble(textBox.Text)).ToString("G4");
label1.Text = "Результат:";
}
break;
case 4:
{
textBox.Text = (a / Convert.ToDouble(textBox.Text)).ToString("G4");
label1.Text = "Результат:";
}
break;
//sin
case 5:
{
a = ((a * Math.PI) / 180);
textBox.Text = (Math.Sin(a)).ToString("G4");
label1.Text = "Результат:";
}
break;
//cos
case 6:
{
a = ((a * Math.PI) / 180);
textBox.Text = (Math.Cos(a)).ToString("G4");
label1.Text = "Результат:";
}
break;
//sqrt
case 7:
{
textBox.Text = (Math.Sqrt(a)).ToString("G4");
label1.Text = "Результат:";
}
break;
//sqr
case 8:
{
textBox.Text = (a * a).ToString("G4");
label1.Text = "Результат:";
}
break;
// 1/х
case 9:
{
textBox.Text = (1 / a).ToString("G4");
label1.Text = "Результат:";
}
break;
}
}
// Запрет ввода с клавиатуры букв
private void textBox_KeyPress_1(object sender, KeyPressEventArgs e)
{
if ((Char.IsDigit(e.KeyChar) || (e.KeyChar == (char)Keys.Back) || (e.KeyChar == ',') || (e.KeyChar == '-')) & (e.KeyChar != '.'))
{
string s = textBox.Text;
if (e.KeyChar == ',')
{
int i = s.IndexOf(",", 0, s.Length);
if (i != (-1))
{
e.KeyChar = (char)Keys.None;
}
}
if (e.KeyChar == '-')
{
int i = s.Length;
if (i > 0)
{
e.KeyChar = (char)Keys.None;
}
}
}
else
e.KeyChar = (char)Keys.None;
}
}
}Решение задачи: «Добавить min(max) ограничения для ввода чисел в калькулятор»
textual
Листинг программы
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.Text != "")
{
double m = Convert.ToDouble(textBox1.Text);
if (m > 100000) textBox1.Text = "";
}