在numericUpDown值中添加一个符号(如%)

我正在尝试将“%”符号添加到我的numericUpDown,即readonly = false。 所以就像5%一样。

这可能吗?

谢谢

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

创建一个名为CustomNumericUpDown的新类,并将此代码放在类中。

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

记得using System.Windows.Forms;添加using System.Windows.Forms; 。 并且只要您想将其添加到表单中。 你用

 CustomNumericUpDown mynum = new CustomNumericUpDown(); 

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

 public class MyNumericUpDown : NumericUpDown { protected override void UpdateEditText() { base.UpdateEditText(); ChangingText = true; Text += "%"; } } 

@Kenji如果我们只设置Text属性,我们会失去一些function(例如。hex或十进制数)

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

 using System; using System.Windows.Forms; using System.Globalization; using System.Diagnostics; using System.ComponentModel; ///  /// Implements a  with leading and trailing symbols. ///  public class NumericUpDownEx : NumericUpDown { ///  /// Initializes a new instance of . ///  public NumericUpDownEx() { } private string _leadingSign = ""; private string _trailingSign = ""; ///  /// Gets or sets a leading symbol that is concatenate with the text. ///  [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(); } } ///  /// Gets or sets a trailing symbol that is concatenated with the text. ///  [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; } } 

解决方案对我来说不起作用,因为当我编辑数字化注册时,我遇到了意想不到的行为。 例如,我无法更改一个数字并保存结果,因为文本包含尾随字符(问题中为’%’,在我的情况下为’€’)。

所以我用一些代码更新类来解析小数并存储值。 注意使用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); } }