Наличие текста внутри элемента управления NumericUpDown после числа - PullRequest
15 голосов
/ 07 мая 2011

Возможно ли в WinForms показывать текст внутри элемента управления NumericUpDown? Например, я хочу показать значение в моем числовом элементе управления вверх вниз - микроамперы, поэтому оно должно быть равно «1 мкА».

Спасибо.

Ответы [ 4 ]

27 голосов
/ 07 мая 2011

Нет такой функциональности, встроенной в стандартный элемент управления.Однако это довольно легко добавить, создав пользовательский элемент управления, который наследуется от класса NumericUpDown и переопределяет метод UpdateEditText для соответствующего форматирования числа.

Например, вы можете иметьследующее определение класса:

public class NumericUpDownEx : NumericUpDown
{
    public NumericUpDownEx()
    {
    }

    protected override void UpdateEditText()
    {
        // Append the units to the end of the numeric value
        this.Text = this.Value + " uA";
    }
}

Или, для более полной реализации, см. этот пример проекта: NumericUpDown с единицей измерения

1 голос
/ 28 марта 2019

Вот что я использовал для показа как минимум 2 цифр для шестнадцатеричного NumericUpDown с префиксом 0x. Он помещает текст в элемент управления и избегает использования «debounce» с помощью предоставленной .Net поле ChangingText

    class HexNumericUpDown2Digits : NumericUpDown
    {
        protected override void UpdateEditText()
        {
            if (Hexadecimal)
            {
                ChangingText = true;
                Text = $"0x{(int)Value:X2}";
            }
            else
            {
                base.UpdateEditText();
            }
        }
    }
1 голос
/ 07 ноября 2018

Использование ответа CodeGray , комментария Фабио об ошибке ValidateEditText и документации NumericUpDown У меня есть простой компонент NumericUpDownWithUnit.Вы можете скопировать / вставить как есть:

using System;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Windows.Forms;

public class NumericUpDownWithUnit : NumericUpDown
{
    #region| Fields |

    private string unit = null;
    private bool unitFirst = true;

    #endregion

    #region| Properties |

    public string Unit
    {
        get => unit;
        set
        {
            unit = value;

            UpdateEditText();
        }
    }

    public bool UnitFirst
    {
        get => unitFirst;
        set
        {
            unitFirst = value;

            UpdateEditText();
        }
    }

    #endregion

    #region| Methods |

    /// <summary>
    /// Method called when updating the numeric updown text.
    /// </summary>
    protected override void UpdateEditText()
    {
        // If there is a unit we handle it ourselfs, if there is not we leave it to the base class.
        if (Unit != null && Unit != string.Empty)
        {
            if (UnitFirst)
            {
                Text = $"({Unit}) {Value}";
            }
            else
            {
                Text = $"{Value} ({Unit})";
            }
        }
        else
        {
            base.UpdateEditText();
        }
    }

    /// <summary>
    /// Validate method called before actually updating the text.
    /// This is exactly the same as the base class but it will use the new ParseEditText from this class instead.
    /// </summary>
    protected override void ValidateEditText()
    {
        // See if the edit text parses to a valid decimal considering the label unit
        ParseEditText();
        UpdateEditText();
    }

    /// <summary>
    /// Converts the text displayed in the up-down control to a numeric value and evaluates it.
    /// </summary>
    protected new void ParseEditText()
    {
        try
        {
            // The only difference of this methods to the base one is that text is replaced directly
            // with the property Text instead of using the regex.
            // We now that the only characters that may be on the textbox are from the unit we provide.
            // because the NumericUpDown handles invalid input from user for us.
            // This is where the magic happens. This regex will match all characters from the unit
            // (so your unit cannot have numbers). You can change this regex to fill your needs
            var regex = new Regex($@"[^(?!{Unit} )]+");
            var match = regex.Match(Text);

            if (match.Success)
            {
                var text = match.Value;

                // VSWhidbey 173332: Verify that the user is not starting the string with a "-"
                // before attempting to set the Value property since a "-" is a valid character with
                // which to start a string representing a negative number.
                if (!string.IsNullOrEmpty(text) && !(text.Length == 1 && text == "-"))
                {
                    if (Hexadecimal)
                    {
                        Value = Constrain(Convert.ToDecimal(Convert.ToInt32(Text, 16)));
                    }
                    else
                    {
                        Value = Constrain(Decimal.Parse(text, CultureInfo.CurrentCulture));
                    }
                }
            }
        }
        catch
        {
            // Leave value as it is
        }
        finally
        {
            UserEdit = false;
        }
    }

    /// </summary>
    /// Returns the provided value constrained to be within the min and max.
    /// This is exactly the same as the one in base class (which is private so we can't directly use it).
    /// </summary>
    private decimal Constrain(decimal value)
    {
        if (value < Minimum)
        {
            value = Minimum;
        }

        if (value > Maximum)
        {
            value = Maximum;
        }

        return value;
    }

    #endregion
}
1 голос
/ 01 июля 2017

Я недавно наткнулся на эту проблему и нашел удивительный ответ Коди Грей. Я использовал это в своих интересах, но недавно резонировал с одним из комментариев к его ответу, в котором говорилось о том, что текст не пройдет проверку, если суффикс все еще там. Я создал, вероятно, не очень профессиональное быстрое решение для этого.

В основном поле this.Text считывается для чисел.

После того, как числа найдены, они помещаются в this.Text, но для того, чтобы мы не создали переполнение стека 1008 *.

, требуется отладка или все, что вам нужно, чтобы вызвать его.

После ввода нового текста с единственным номером для вызова процесса вызываются обычные ParseEditText(); и UpdateEditText();.

Это не самое дружественное к ресурсам или эффективное решение, но большинство современных компьютеров сегодня должно прекрасно с этим справиться.

Также вы заметите, что я создал свойство для изменения суффикса просто для более удобного использования в редакторе.

public class NumericUpDownUnit : System.Windows.Forms.NumericUpDown
    {

        public string Suffix{ get; set; }

        private bool Debounce = false;

        public NumericUpDownUnit()
        {

        }

        protected override void ValidateEditText()
        {
            if (!Debounce) //I had to use a debouncer because any time you update the 'this.Text' field it calls this method.
            {
                Debounce = true; //Make sure we don't create a stack overflow.

                string tempText = this.Text; //Get the text that was put into the box.
                string numbers = ""; //For holding the numbers we find.

                foreach (char item in tempText) //Implement whatever check wizardry you like here using 'tempText' string.
                {
                    if (Char.IsDigit(item))
                    {
                        numbers += item;
                    }
                    else
                    {
                        break;
                    }
                }

                decimal actualNum = Decimal.Parse(numbers, System.Globalization.NumberStyles.AllowLeadingSign);
                if (actualNum > this.Maximum) //Make sure our number is within min/max
                    this.Value = this.Maximum;
                else if (actualNum < this.Minimum)
                    this.Value = this.Minimum;
                else
                    this.Value = actualNum; 

                ParseEditText(); //Carry on with the normal checks.
                UpdateEditText();

                Debounce = false;
            }

        }

        protected override void UpdateEditText()
        {
            // Append the units to the end of the numeric value
            this.Text = this.Value + Suffix;
        }
    }

Пожалуйста, не стесняйтесь улучшить мой ответ или исправить меня, если что-то не так, я программист-самоучка, все еще учусь.

...