Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding a sign to numericUpDown value (like %)

I am trying to add "%" sign to my numericUpDown that is readonly=false. So it's like 5% for example.

Is this possible?

Thanks

like image 418
Razor Avatar asked Sep 11 '13 21:09

Razor


3 Answers

You can create your own custom NumericUpDown class and override the UpdateEditText method like so:

Create a new class with the name 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 use

CustomNumericUpDown mynum = new CustomNumericUpDown();
like image 108
Kenji Avatar answered Sep 24 '22 01:09

Kenji


We need to write own control and use it if we want different "output".

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

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

@Kenji If we only set Text property we lose some functionality (eg. Hexadecimal or DecimalPlaces)

like image 35
Carnifex Avatar answered Sep 27 '22 01:09

Carnifex


If someone is searching for a complete solution, here is one:

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

}
like image 26
doomgg Avatar answered Sep 26 '22 01:09

doomgg