6

我正在尝试将“%”符号添加到我的 numericUpDown 中,即 readonly=false。因此,例如,它就像 5%。

这可能吗?

谢谢

4

4 回答 4

3

如果我们想要不同的“输出”,我们需要编写自己的控件并使用它。

public class MyNumericUpDown : NumericUpDown
{
    protected override void UpdateEditText()
    {
        base.UpdateEditText();

        ChangingText = true;
        Text += "%";
    }
}

@Kenji如果我们只设置Text属性,我们会失去一些功能(例如十六进制或十进制)

于 2013-09-11T22:20:40.490 回答
3

您可以创建自己的自定义 NumericUpDown 类并覆盖 UpdateEditText 方法,如下所示:

使用名称创建一个新类CustomNumericUpDown并将此代码放入该类中。

public class CustomNumericUpDown : NumericUpDown
{
  protected override void UpdateEditText()
  {
    this.Text = this.Value.ToString() + "%";
  }
}

记得添加using System.Windows.Forms;。并且无论何时您想将其添加到您的表单中。你用

CustomNumericUpDown mynum = new CustomNumericUpDown();
于 2013-09-11T21:57:16.100 回答
3

如果有人正在寻找一个完整的解决方案,这里有一个:

using System;
using System.Windows.Forms;
using System.Globalization;
using System.Diagnostics;
using System.ComponentModel;


/// <summary>
/// Implements a <see cref="NumericUpDown"/> with leading and trailing symbols.
/// </summary>
public class NumericUpDownEx : NumericUpDown
{

    /// <summary>
    /// Initializes a new instance of <see cref="NumericUpDownEx"/>.
    /// </summary>
    public NumericUpDownEx()
    { }

    private string _leadingSign = "";
    private string _trailingSign = "";

    /// <summary>
    /// Gets or sets a leading symbol that is concatenate with the text.
    /// </summary>
    [Description("Gets or sets a leading symbol that is concatenated with the text.")]
    [Browsable(true)]
    [DefaultValue("")]
    public string LeadingSign
    {
        get { return _leadingSign; }
        set { _leadingSign = value; this.UpdateEditText(); }
    }

    /// <summary>
    /// Gets or sets a trailing symbol that is concatenated with the text.
    /// </summary>
    [Description("Gets or sets a trailing symbol that is concatenated with the text.")]
    [Browsable(true)]
    [DefaultValue("")]
    public string TrailingSign
    {
        get { return _trailingSign; }
        set { _trailingSign = value; this.UpdateEditText(); }
    }

    protected override void UpdateEditText()
    {
        if (UserEdit)
        {
            ParseEditText();
        }

        ChangingText = true;
        base.Text = _leadingSign + GetNumberText(this.Value) + _trailingSign;
        Debug.Assert(ChangingText == false, "ChangingText should have been set to false");
    }

    private string GetNumberText(decimal num)
    {
        string text;

        if (Hexadecimal)
        {
            text = ((Int64)num).ToString("X", CultureInfo.InvariantCulture);
            Debug.Assert(text == text.ToUpper(CultureInfo.InvariantCulture), "GetPreferredSize assumes hex digits to be uppercase.");
        }
        else
        {
            text = num.ToString((ThousandsSeparator ? "N" : "F") + DecimalPlaces.ToString(CultureInfo.CurrentCulture), CultureInfo.CurrentCulture);
        }
        return text;
    }

    protected override void ValidateEditText()
    {
        ParseEditText();
        UpdateEditText();
    }

    protected new void ParseEditText()
    {
        Debug.Assert(UserEdit == true, "ParseEditText() - UserEdit == false");

        try
        {
            string text = base.Text;
            if (!string.IsNullOrEmpty(_leadingSign))
            {
                if (text.StartsWith(_leadingSign))
                    text = text.Substring(_leadingSign.Length);
            }
            if (!string.IsNullOrEmpty(_trailingSign))
            {
                if (text.EndsWith(_trailingSign))
                    text = text.Substring(0, text.Length - _trailingSign.Length);
            }

            if (!string.IsNullOrEmpty(text) &&
                !(text.Length == 1 && text == "-"))
            {
                if (Hexadecimal)
                {
                    base.Value = Constrain(Convert.ToDecimal(Convert.ToInt32(text, 16)));
                }
                else
                {
                    base.Value = Constrain(decimal.Parse(text, CultureInfo.CurrentCulture));
                }
            }
        }
        catch
        {

        }
        finally
        {
            UserEdit = false;
        }
    }

    private decimal Constrain(decimal value)
    {
        if (value < base.Minimum)
            value = base.Minimum;

        if (value > base.Maximum)
            value = base.Maximum;

        return value;
    }

}
于 2016-07-25T02:23:00.990 回答
1

该解决方案对我不起作用,因为我在编辑 numericupdown 时出现意外行为。例如,我无法更改一个数字并保存结果,因为文本包含尾随字符(问题中的“%”,在我的情况下为“€”)。

所以我用一些代码更新了类来解析小数并存储值。注意使用,ChangingText否则它会在循环中触发修改事件

class EuroUpDown : NumericUpDown
{
    protected override void UpdateEditText()
    {
        ChangingText = true;
        Regex decimalRegex = new Regex(@"(\d+([.,]\d{1,2})?)");
        Match m = decimalRegex.Match(this.Text);
        if (m.Success)
        {
            Text = m.Value;

        }
        ChangingText = false;

        base.UpdateEditText();
        ChangingText = true;

        Text = this.Value.ToString("C", CultureInfo.CurrentCulture);
    }
}
于 2015-09-17T08:58:12.830 回答