1. 程式人生 > 其它 >handycontrol中NumericUpDown無法顯示自定義錯誤提示的解決辦法

handycontrol中NumericUpDown無法顯示自定義錯誤提示的解決辦法

最新的版本,好像是3.2.

NumericUpDown控制元件設定了ErrorStr屬性,不管是否帶hc名稱空間,設定的字串都不會生效,看了下原始碼,修改了NumericUpDown.cs中的一句,如下:

官方原始碼

public virtual bool VerifyData()
        {
            OperationResult<bool> result;

            if (VerifyFunc != null)
            {
                result = VerifyFunc.Invoke(_textBox.Text);
            }
            else
            {
                if (!string.IsNullOrEmpty(_textBox.Text))
                {
                    if (double.TryParse(_textBox.Text, out var value))
                    {
                        if (value < Minimum || value > Maximum)
                        {
                            result =
OperationResult.Failed(Properties.Langs.Lang.OutOfRange); } else { result = OperationResult.Success(); } } else { result = OperationResult.Failed(Properties.Langs.Lang.FormatError); } } else if (InfoElement.GetNecessary(this)) { result = OperationResult.Failed(Properties.Langs.Lang.IsNecessary); } else { result = OperationResult.Success(); } } SetCurrentValue(ErrorStrProperty, result.Message); SetCurrentValue(IsErrorProperty, ValueBoxes.BooleanBox(!result.Data)); return result.Data; }

修改後

public virtual bool VerifyData()
        {
            OperationResult<bool> result;

            if (VerifyFunc != null)
            {
                result = VerifyFunc.Invoke(_textBox.Text);
            }
            else
            {
                if (!string.IsNullOrEmpty(_textBox.Text))
                {
                    if (double.TryParse(_textBox.Text, out var value))
                    {
                        if (value < Minimum || value > Maximum)
                        {
                            result = OperationResult.Failed($"範圍{Minimum}-{Maximum}"
); } else { result = OperationResult.Success(); } } else { result = OperationResult.Failed(Properties.Langs.Lang.FormatError); } } else if (InfoElement.GetNecessary(this)) { result = OperationResult.Failed(Properties.Langs.Lang.IsNecessary); } else { result = OperationResult.Success(); } } SetCurrentValue(ErrorStrProperty, result.Message); SetCurrentValue(IsErrorProperty, ValueBoxes.BooleanBox(!result.Data)); return result.Data; }