Search code examples
c#.netwinformsnumericupdown

c# WinForms can you get the NumericUpDown text area


Is it possible to get the text area of a NumericUpDown control? I'm looking to get it's size so that I can mask it with a panel. I don't want the user to be able to edit AND select the text. Is this possible? Or is there another way of covering up the text in the text box?

Thanks.


Solution

  • You can get this by using a Label control instead of the baked-in TextBox control. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form.

    using System;
    using System.Windows.Forms;
    
    class UpDownLabel : NumericUpDown {
        private Label mLabel;
        private TextBox mBox;
    
        public UpDownLabel() {
            mBox = this.Controls[1] as TextBox;
            mBox.Enabled = false;
            mLabel = new Label();
            mLabel.Location = mBox.Location;
            mLabel.Size = mBox.Size;
            this.Controls.Add(mLabel);
            mLabel.BringToFront();
        }
    
        protected override void UpdateEditText() {
            base.UpdateEditText();
            if (mLabel != null) mLabel.Text = mBox.Text;
        }
    }