c# WinForms can you get the NumericUpDown text area

后端 未结 4 1347
半阙折子戏
半阙折子戏 2021-01-16 16:09

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

4条回答
  •  不知归路
    2021-01-16 17:11

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

提交回复
热议问题