Adding a character to a numericUpDown value (e.g.%)

I am trying to add the "%" sign to my numericUpDown, which is readonly = false. So, for example, 5%.

Is it possible?

thanks

+4
source share
4 answers

You can create your own NumericUpDown class and override the UpdateEditText method as follows:

Create a new class called CustomNumericUpDown and put this code in the class.

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

Remember to add using System.Windows.Forms; . And whenever you want to add it to your form. You are using

 CustomNumericUpDown mynum = new CustomNumericUpDown(); 
+3
source

We need to write our own control and use it if we need another "output".

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

@Kenji If we set only the Text property, we will lose some functionality (for example, hexadecimal or decimal)

+2
source

If someone is looking for a complete solution, here is one of them:

 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; } } 
+2
source

The solution did not work for me, as I was getting unpredictable behavior when editing numericupdown. For example, I could not change one number and save the result, because the text contained the final character ("%" in the question "in my case").

So, I am updating the class with some code to parse the decimal and store the value. Pay attention to using ChangingText , otherwise it would trigger a modification event in loops

 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); } } 
+1
source

Source: https://habr.com/ru/post/1501740/


All Articles