Skip to content

Display percentage in textbox

New Discussion
Mark
Mark asked on Jul 7, 2009 6:37 PM

Is there anyway to display a numeric value (such as .14) as a percentage (%14) in a windowsform  control?  There appears to be a web control to do this, but I can’t find a forms control.

Mark <><

Sign In to post a reply

Replies

  • 0
    Mark
    Mark answered on Sep 5, 2008 4:11 PM

    Have a look at the UltraMaskedEdit control, with a mask something like ""999,999,999,999.###%"

  • 0
    Mark
    Mark answered on Sep 5, 2008 4:17 PM

    Actually, cancel the above. Use the same control but leave the mask empty and set your FormatString property to "P". That seems to work ok.

    • 0
      Mark
      Mark answered on Sep 5, 2008 4:45 PM

       That didn't work for me.  Regardless of what I set the value property to, I get the decimal value displayed.  Just to be sure, is the "P" with or without quotes?

       What I really want is for the user to enter 25 and have it stored as .25 but displayed at 25%.

      Mark <><

      • 0
        Mark
        Mark answered on Sep 8, 2008 10:31 AM

        Hi Mark,

        The only other change I think you need to make is to change the EditAs property to Double. The format doesn't use quotes; think of it as whatever would be on the other side of a String.Format  {0:P} format string. And, the way it's currently set up, to get "25%" you'd have to enter "0.25", rather than "25".

        Otherwise, try the code below. It's basic but it's a start.

        using System;

        using

        System.Collections.Generic;

        using

        System.Text;

        using

        Infragistics.Win.UltraWinEditors;namespace BarraCoder

        {

        public class PercentageTextBox : UltraTextEditor

        {

        protected override void OnBeforeExitEditMode(Infragistics.Win.BeforeExitEditModeEventArgs args)

        {

        if (!string.IsNullOrEmpty(base.textValue))

        {

        double candidate;if (!double.TryParse(string.Format("{0}", base.Value), out candidate))

        {

        args.Cancel =

        true;throw new ApplicationException("Not a valid number.");

        }

        base.Value = candidate / 100;

        string newText = string.Format("{0:P}", candidate/100);base.SetText(newText);

        }

        base.OnBeforeExitEditMode(args);

        }

        protected override void OnValueChanged(EventArgs args)

        {

        System.Diagnostics.Debug.WriteLine(string.Format("New Value: {0}", base.Value));base.OnValueChanged(args);

        }

        }

        }

      • 0
        namrata
        namrata answered on Jul 7, 2009 6:37 PM

        Hi,

         

        I am looking for something similar. However, my requirement is that the user should be able to enter both number 25 or "25%". I want it to behave exactly like the UltraWinGrid percentage works, because at some places I use a grid for percentages and at some places a textbox.

         

        Please let me know how I can let the user enter 25%. I tried setting the EditType as String instead of double but that behaves wierd. Kindly help.

        Thanks

        Namrata Ghadi

  • You must be logged in to reply to this topic.
Discussion created by
Favorites
Replies
Created On
Last Post
Discussion created by
Mark
Favorites
0
Replies
5
Created On
Jul 07, 2009
Last Post
17 years, 3 months ago

Suggested Discussions

Tags

No tags

Created by

Created on

Jul 7, 2009 6:37 PM

Last activity on

Feb 11, 2026 5:30 PM