COP-17/WinFormsProject/WinFormsLibrary/NumberTextBox.cs

93 lines
2.2 KiB
C#
Raw Permalink Normal View History

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 WinFormsLibrary
{
public partial class NumberTextBox : UserControl
{
2023-10-04 22:43:22 +04:00
public int? maxValue = null;
public int? minValue = null;
2023-10-05 11:23:18 +04:00
public string errorText = "";
2023-09-21 10:49:09 +04:00
public NumberTextBox()
{
InitializeComponent();
2023-09-21 10:49:09 +04:00
}
2023-10-04 22:43:22 +04:00
public int? MinValue
{
get { return minValue; }
set
{
if (value == null) return;
minValue = value;
numericUpDown.Minimum = (int)value;
}
}
2023-09-21 10:49:09 +04:00
2023-10-04 22:43:22 +04:00
public int? MaxValue
2023-09-21 10:49:09 +04:00
{
2023-10-04 22:43:22 +04:00
get { return maxValue; }
set
2023-09-21 10:49:09 +04:00
{
2023-10-04 22:43:22 +04:00
if (value == null) return;
maxValue = value;
numericUpDown.Maximum = (int)value;
}
}
2023-09-21 10:49:09 +04:00
2023-10-05 11:23:18 +04:00
public decimal? Value
2023-10-04 22:43:22 +04:00
{
2023-10-05 11:23:18 +04:00
get {
if (CheckRanges(numericUpDown.Value))
{
return numericUpDown.Value;
}
else
{
return null;
}
}
2023-10-04 22:43:22 +04:00
set
{
2023-10-05 11:23:18 +04:00
if (CheckRanges(value))
2023-09-21 10:49:09 +04:00
{
2023-10-05 11:23:18 +04:00
numericUpDown.Value = (decimal)value;
2023-09-21 10:49:09 +04:00
}
2023-10-05 11:23:18 +04:00
}
}
2023-09-21 10:49:09 +04:00
2023-10-05 11:23:18 +04:00
private bool CheckRanges(decimal? value)
{
if (MinValue == null || MaxValue == null)
{
errorText = "Ошибка, диапазоны не заданы.";
return false;
}
2023-09-21 10:49:09 +04:00
2023-10-05 11:23:18 +04:00
if (value < MinValue || value > MaxValue)
{
errorText = "Ошибка, значение не входит в заданный диапазон.";
return false;
2023-10-04 22:43:22 +04:00
}
2023-10-05 11:23:18 +04:00
errorText = "Ошибок нет.";
return true;
2023-09-21 10:49:09 +04:00
}
2023-10-04 22:43:22 +04:00
public event EventHandler DateChanged
2023-09-21 10:49:09 +04:00
{
2023-10-04 22:43:22 +04:00
add { numericUpDown.ValueChanged += value; }
remove { numericUpDown.ValueChanged -= value; }
}
}
}