Windows Develop Bookmark and Share   
 index > Windows Forms General > add units of measure to numericupdown control
 

add units of measure to numericupdown control

I'm looking to add units of measure to a numericupdown control. For instance, if a user types in "3000", the textbox should display "3 k". Or, for another use, if the user types "3mm", maybe the textbox shows "0.003 m". Shawn Oster's blog describes some handy functions built into the Silverlight numericupdown control that allow this: http://shawnoster.com/Blog/Creating-a-LengthUpDown-from-a-NumericUpDown

However, these functions don't seem to be available in the numericupdown control from the System.Windows.Forms namespace. Is there a way to do this?

Here's what I've found so far:

There is a VBproject on the CodeProject website that creates a control by placing a textbox control on top of a NumericUpDown control and adding the functions to display the reformatted text. This works, but it seems like an non-elegant solution because the NumericUpDown already has a textbox. This means we've got events firing to display text in a textbox that's never seen.

I tried to inherit UpDownBase in a new control and override the necessary functions. This seems to be the proper way to go about it, but the designer gives me the following error:
"The designer must create an instance of type 'System.Windows.Forms.UpDownBase' but it cannot because the type is declared as abstract."
Now, I'm not dragging 'UpDownBase' onto the form--I'm dragging my extended control (with the functions overridden). I've seen some posts that say the designer won't add abstract controls, and that makes sense, of course. But I must be missing something because the NumericUpDown inherits UpDownBase, and the designer has no problem adding that control. I've posted my code below.

Should I just try to use the Silverlight control? (Is that possible?--I'm still kind ofa newb) Or, is there a way to override the formatting functions in the numericupdown control?

Thanks for your help!

public class ExtUpDown : UpDownBase
    {
        private double m_dIncrement = 1;
        /// <summary>
        /// The value will increase or decrease by this amount when the arrow buttons are pressed.
        /// </summary>
        public double Increment
        {
            get { return m_dIncrement; }
            set { m_dIncrement = value; }
        }

        private double m_dMaximum = 20;
        /// <summary>
        /// The maximum allowed value
        /// </summary>
        public double Maximum
        {
            get { return m_dMaximum; }
            set { m_dMaximum = value; }
        }

        private double m_dMinimum = 0;
        /// <summary>
        /// The minimum allowed value
        /// </summary>
        public double Minimum
        {
            get { return m_dMinimum; }
            set { m_dMinimum = value; }
        }

        private double m_dNumericValue = 0;
        /// <summary>
        /// The numeric value of the control
        /// </summary>
        public double Value
        {
            get { return m_dNumericValue; }
            set { m_dNumericValue = value; }
        }

        protected override void UpdateEditText()
        {
            this.Text = FormatText(m_dNumericValue);
        }

        protected override void ValidateEditText()
        {
            m_dNumericValue = ParseFieldText(this.Text);
            if (m_dNumericValue > m_dMaximum)
                m_dNumericValue = m_dMaximum;
            else if (m_dNumericValue < m_dMinimum)
                m_dNumericValue = m_dMinimum;
            
            this.UpdateEditText();
            
            base.ValidateEditText();
        }

        /// <summary>
        /// Adds 'Increment' to the current value
        /// </summary>
        public override void UpButton()
        {
            m_dNumericValue += m_dIncrement;
            this.ValidateEditText();
        }

        /// <summary>
        /// Subtracts 'Increment' from the current value
        /// </summary>
        public override void DownButton()
        {
            m_dNumericValue -= m_dIncrement;
            this.ValidateEditText();
        }

        protected virtual double ParseFieldText(string FieldText)
        {
            // add text parsing...
        }

        protected virtual string FormatText(double Value)
        {
            // add text formatting
        }
    }



  • Moved byTaylorMichaelLMVPThursday, October 01, 2009 6:09 PMWinForms related (From:Visual C# IDE)
  •  
satrimb  Thursday, October 01, 2009 4:04 PM
Use the DomainUpDown.
JohnWein  Thursday, October 01, 2009 10:12 PM
Use the DomainUpDown.
JohnWein  Thursday, October 01, 2009 10:12 PM

You can use google to search for other answers

Custom Search

More Threads

• only allow user to select value 1 through 4 from listbox,textbox....?
• File help!
• Give me some feedback from this "rude" Drag and Drop
• problem: Drawing disappears!!
• Updating MS Access Table using DataTable.Merge
• Text with wrong format for custom TextBox (with UserPaint)
• convert pixel to point
• Floating license
• HTML Help Viewer
• retrieving a value from dynamically created checkbox c#