[英]c# WinForms can you get the NumericUpDown text area
是否可以獲取NumericUpDown控件的文本區域? 我正在尋找它的尺寸,以便可以用面板遮蓋它。 我不希望用戶能夠編輯和選擇文本。 這可能嗎? 還是有另一種方法來掩蓋文本框中的文本?
謝謝。
如果要禁止手動編輯,則可以將ReadOnly
屬性設置為true
。
updown.ReadOnly = true;
如果您也不想選擇(我想知道為什么需要這樣做),則可以使用反射。 我認為沒有更好的方法,因為upDownEdit
字段是UpDownBase
內部字段。
FieldInfo editProp = updown.GetType().GetField("upDownEdit", BindingFlags.Instance | BindingFlags.NonPublic);
TextBox edit = (TextBox)editProp.GetValue(updown);
edit.Enabled = false;
您可以使用Label控件而不是內置的TextBox控件來獲取此信息。 將新類添加到您的項目中,然后粘貼以下代碼。 編譯。 將新控件從工具箱的頂部拖放到窗體上。
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;
}
}
將ReadOnly屬性設置為true,僅此而已。
實現此目的的“正確”方法是創建一個上下控件和一個標簽(不能選擇或編輯標簽)。 但是,Windows Forms的作者以其無窮的智慧決定了我們不需要Up-Down控件,因此他們沒有為它提供.NET包裝器。 他們認為,我們想要Up-Down控件的唯一原因是與TextBox控件配對時。
如果您想使用此路線,則Up-Down控件非常簡單,可以創建一個光包裝器: http : //msdn.microsoft.com/zh-cn/library/bb759880.aspx
編輯1
[片段]
編輯2
我在這里寫過有關它的文章: http : //tergiver.wordpress.com/2010/11/05/using-the-up-down-control-in-windows-forms/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.