views:

77

answers:

4

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.

A: 

Set the ReadOnly property to true, that's all.

A_Nablsi
This will allow the user to highlite the number. I want that disabled.
Then you should consider what Tergiver tells you, you have to create a custom control, or you know you can create a UserControl and put the NumericUpDown control put a label over the textbox area and whenever the ValueChanged of the NumericUpDownControl fired you change the label text.
A_Nablsi
A: 

If you want do disallow manual editing, you can just set ReadOnly property to true.

updown.ReadOnly = true;

If you want to disallow selecting too (I wonder why you need this), you can use reflection. I don't think there's better way, because the field upDownEdit is internal field of UpDownBase.

FieldInfo editProp = updown.GetType().GetField("upDownEdit", BindingFlags.Instance | BindingFlags.NonPublic);
TextBox edit = (TextBox)editProp.GetValue(updown);
edit.Enabled = false;
Athari
A: 

The 'proper' way to do this is to create an Up-Down control and a Label (the label can't be selected or edited). However, the authors of Windows Forms, in their infinite wisdom, have decided that we don't need the Up-Down control and so they didn't provide a .NET wrapper for one. They decided that the only reason we could ever want an Up-Down control is when paired with a TextBox control.

The Up-Down control is simple enough to create a light wrapper if you want to go this route: http://msdn.microsoft.com/en-us/library/bb759880.aspx

Edit 1

[snip]

Edit 2

I blogged about it here: http://tergiver.wordpress.com/2010/11/05/using-the-up-down-control-in-windows-forms/

Tergiver
I did some digging and found that my idea wasn't going to pan out. I also discovered that Windows.Forms does indeed contain an `UpDownButtons` control, it's used by Numeric/DomainUpDown. It's not a wrapper of the system Up-Down control, it's a "from scratch" implementation of it. It's also internal so you cannot make use of it. I think that Hans' solution is fine. It's inefficient (using 3 controls where only two are necessary), but very simple to implement. The 'proper' solution, building an UpDownLabel control modeled after the `NumericUpDown` is a bit more involved.
Tergiver
More info (in case anyone other than myself is interested): We cannot use the system Up-Down control in conjunction with a WinForms Label control with the built-in increment/decrement feature (UDS_SETBUDDYINT). The reason is that the WinForm Label control is not an instance of the system label control, it is another "from scratch" implementation of it and it does not respond to the WM_SETTEXT message which is used by the Up-Down control.
Tergiver
+1  A: 

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