* UITextBox: 修改整数、浮点数大小离开判断为实时输入判断

This commit is contained in:
Sunny 2022-11-12 22:13:12 +08:00
parent 265fe51009
commit 71a4664122
2 changed files with 205 additions and 102 deletions

View File

@ -31,11 +31,11 @@ namespace Sunny.UI
{ {
private bool canEmpty; private bool canEmpty;
private int decLength = 2; private int decLength = 2;
private bool hasMaxValue; //private bool hasMaxValue;
private bool hasMinValue; //private bool hasMinValue;
private string mask = "0.00"; private string mask = "0.00";
private double maxValue = int.MaxValue; //private double maxValue = int.MaxValue;
private double minValue = int.MinValue; //private double minValue = int.MinValue;
private UITextBox.UIEditType _uiEditType = UITextBox.UIEditType.String; private UITextBox.UIEditType _uiEditType = UITextBox.UIEditType.String;
public UIEdit() public UIEdit()
@ -138,6 +138,14 @@ namespace Sunny.UI
{ {
DrawWaterMark(); DrawWaterMark();
} }
if (Text.IsValid())
{
if (Type == UITextBox.UIEditType.Integer || Type == UITextBox.UIEditType.Double)
{
CheckMaxMin();
}
}
} }
private void waterMarkContainer_Click(object sender, EventArgs e) private void waterMarkContainer_Click(object sender, EventArgs e)
@ -369,35 +377,35 @@ namespace Sunny.UI
} }
} }
[DefaultValue(false)] [Browsable(false), DefaultValue(false)]
public bool HasMaxValue public bool HasMaxValue { get; set; }
{ //{
get => hasMaxValue; // get => hasMaxValue;
set // set
{ // {
if (hasMaxValue != value) // if (hasMaxValue != value)
{ // {
hasMaxValue = value; // hasMaxValue = value;
CheckMaxMin(); // CheckMaxMin();
Invalidate(); // Invalidate();
} // }
} // }
} //}
[DefaultValue(false)] [Browsable(false), DefaultValue(false)]
public bool HasMinValue public bool HasMinValue { get; set; }
{ //{
get => hasMinValue; // get => hasMinValue;
set // set
{ // {
if (hasMinValue != value) // if (hasMinValue != value)
{ // {
hasMinValue = value; // hasMinValue = value;
CheckMaxMin(); // CheckMaxMin();
Invalidate(); // Invalidate();
} // }
} // }
} //}
[DefaultValue(0)] [DefaultValue(0)]
public double DoubleValue public double DoubleValue
@ -429,33 +437,33 @@ namespace Sunny.UI
} }
} }
[DefaultValue(int.MaxValue)] //[DefaultValue(int.MaxValue)]
public double MaxValue //public double MaxValue
{ //{
get => maxValue; // get => maxValue;
set // set
{ // {
maxValue = value; // maxValue = value;
if (maxValue < minValue) // if (maxValue < minValue)
minValue = maxValue; // minValue = maxValue;
CheckMaxMin(); // CheckMaxMin();
Invalidate(); // Invalidate();
} // }
} //}
//
[DefaultValue(int.MinValue)] //[DefaultValue(int.MinValue)]
public double MinValue //public double MinValue
{ //{
get => minValue; // get => minValue;
set // set
{ // {
minValue = value; // minValue = value;
if (minValue > maxValue) // if (minValue > maxValue)
maxValue = minValue; // maxValue = minValue;
CheckMaxMin(); // CheckMaxMin();
Invalidate(); // Invalidate();
} // }
} //}
private string DecimalToMask(int iDecimal) private string DecimalToMask(int iDecimal)
{ {
@ -581,46 +589,30 @@ namespace Sunny.UI
if (_uiEditType == UITextBox.UIEditType.Integer) if (_uiEditType == UITextBox.UIEditType.Integer)
{ {
if (Text == "" && CanEmpty) return; if (Text == "" && CanEmpty) return;
if (!int.TryParse(Text, out var a)) return;
if (!int.TryParse(Text, out var a)) if (a > MaxValue)
Text = @"0"; a = (int)MaxValue;
if (a < MinValue)
if (hasMaxValue) a = (int)MinValue;
{
var m = (int)Math.Floor(maxValue);
if (a > m)
a = m;
}
if (hasMinValue)
{
var m = (int)Math.Ceiling(minValue);
if (a < m)
a = m;
}
Text = a.ToString(); Text = a.ToString();
SelectionStart = Text.Length;
} }
if (_uiEditType == UITextBox.UIEditType.Double) if (_uiEditType == UITextBox.UIEditType.Double)
{ {
if (Text == "" && CanEmpty) return; if (Text == "" && CanEmpty) return;
if (!double.TryParse(Text, out var a)) return;
if (!double.TryParse(Text, out var a)) if (a > MaxValue)
Text = a.ToString("f" + decLength); a = MaxValue;
if (hasMaxValue) if (a < MinValue)
{ a = MinValue;
if (a > maxValue)
a = maxValue;
}
if (hasMinValue)
{
if (a < minValue)
a = minValue;
}
Text = a.ToString("f" + decLength); Text = a.ToString("f" + decLength);
SelectionStart = Text.Length;
} }
} }
@ -688,24 +680,134 @@ namespace Sunny.UI
{ {
if (Text == "" && CanEmpty) return; if (Text == "" && CanEmpty) return;
if (StringIndexIsChar(Text, 0, '.')) //if (StringIndexIsChar(Text, 0, '.'))
Text = @"0" + Text; // Text = @"0" + Text;
//
if (StringIndexIsChar(Text, Text.Length - 1, '.')) //if (StringIndexIsChar(Text, Text.Length - 1, '.'))
Text = Text + @"0"; // Text = Text + @"0";
//
if (StringIndexIsChar(Text, 0, '+') || StringIndexIsChar(Text, 0, '+')) //if (StringIndexIsChar(Text, 0, '+') || StringIndexIsChar(Text, 0, '+'))
if (StringIndexIsChar(Text, 1, '.')) // if (StringIndexIsChar(Text, 1, '.'))
Text = Text.Insert(1, @"0"); // Text = Text.Insert(1, @"0");
if (!double.TryParse(Text, out var doubleValue)) if (!double.TryParse(Text, out var doubleValue))
Text = mask; Text = mask;
else
Text = doubleValue.ToString("f" + decLength); Text = doubleValue.ToString("f" + decLength);
} }
CheckMaxMin(); //CheckMaxMin();
//Invalidate(); //Invalidate();
} }
private double max = int.MaxValue;
private double min = int.MinValue;
static internal double EffectiveMax(double _max)
{
double maxSupported = double.MaxValue;
if (_max > maxSupported)
{
return maxSupported;
}
return _max;
}
static internal double EffectiveMin(double _min)
{
double minSupported = double.MinValue;
if (_min < minSupported)
{
return minSupported;
}
return _min;
}
[DefaultValue(int.MaxValue)]
[Description("最大日期"), Category("SunnyUI")]
public double MaxValue
{
get
{
return EffectiveMax(max);
}
set
{
//if (value != max)
{
if (value < EffectiveMin(min))
{
value = EffectiveMin(min);
}
// If trying to set the maximum greater than Max, throw.
if (value > double.MaxValue)
{
value = double.MaxValue;
}
max = value;
if (Type == UITextBox.UIEditType.Integer)
max = Math.Min(max, int.MaxValue);
//If Value (which was once valid) is suddenly greater than the max (since we just set it)
//then adjust this...
if (IntValue > max)
{
IntValue = (int)max;
}
if (DoubleValue > max)
{
DoubleValue = max;
}
}
}
}
[DefaultValue(int.MinValue)]
[Description("最小日期"), Category("SunnyUI")]
public double MinValue
{
get
{
return EffectiveMin(min);
}
set
{
if (value != min)
{
if (value > EffectiveMax(max))
{
value = EffectiveMax(max);
}
// If trying to set the minimum less than Min, throw.
if (value < double.MinValue)
{
value = double.MinValue;
}
min = value;
if (Type == UITextBox.UIEditType.Integer)
min = Math.Max(min, int.MinValue);
//If Value (which was once valid) is suddenly less than the min (since we just set it)
//then adjust this...
if (IntValue < min)
{
IntValue = (int)min;
}
if (DoubleValue < min)
{
min = value;
DoubleValue = min;
}
}
}
}
} }
} }

View File

@ -41,6 +41,7 @@
* 2022-09-16: V3.2.4 * 2022-09-16: V3.2.4
* 2022-09-16: V3.2.4 Button可能不显示的问题 * 2022-09-16: V3.2.4 Button可能不显示的问题
* 2022-11-03: V3.2.6 * 2022-11-03: V3.2.6
* 2022-11-12: V3.2.8
******************************************************************************/ ******************************************************************************/
using System; using System;
@ -545,7 +546,7 @@ namespace Sunny.UI
edit.SelectAll(); edit.SelectAll();
} }
public void CheckMaxMin() internal void CheckMaxMin()
{ {
edit.CheckMaxMin(); edit.CheckMaxMin();
} }
@ -728,7 +729,7 @@ namespace Sunny.UI
set => edit.MinValue = value; set => edit.MinValue = value;
} }
[DefaultValue(false)] [DefaultValue(false), Browsable(false)]
[Description("是否判断最大值显示"), Category("SunnyUI")] [Description("是否判断最大值显示"), Category("SunnyUI")]
public bool MaximumEnabled public bool MaximumEnabled
{ {
@ -736,7 +737,7 @@ namespace Sunny.UI
set => HasMaximum = value; set => HasMaximum = value;
} }
[DefaultValue(false)] [DefaultValue(false), Browsable(false)]
[Description("是否判断最小值显示"), Category("SunnyUI")] [Description("是否判断最小值显示"), Category("SunnyUI")]
public bool MinimumEnabled public bool MinimumEnabled
{ {