Tag Archives: handycontrol

[Solved] handycontrol: numericupdown Fail to display custom error

The latest version seems to be 3.2

The errorstr property is set for the numericupdown control. The set string will not take effect regardless of whether it is with HC namespace or not. After looking at the source code, modify a sentence in numericupdown.cs as follows:

Official source code

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;
        }

After modification

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;
        }