UpDownBase.Text 属性
获取或设置数字显示框(也称为 up-down 控件)中显示的文本。
**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)
语法
声明
<LocalizableAttribute(True)> _
Public Overrides Property Text As String
用法
Dim instance As UpDownBase
Dim value As String
value = instance.Text
instance.Text = value
[LocalizableAttribute(true)]
public override string Text { get; set; }
[LocalizableAttribute(true)]
public:
virtual property String^ Text {
String^ get () override;
void set (String^ value) override;
}
/** @property */
public String get_Text ()
/** @property */
public void set_Text (String value)
public override function get Text () : String
public override function set Text (value : String)
属性值
数字显示框中显示的字符串值。
备注
如果在 UserEdit 属性设置为 true 时设置 Text 属性,将调用 UpdateEditText 方法。如果在 UserEdit 属性设置为 false 时设置 Text 属性,将调用 ValidateEditText 方法。
示例
下面的代码示例使用派生类 NumericUpDown。此代码要求您在窗体上创建了一个 NumericUpDown 控件和一个 Button,并且已经将 System.Drawing 命名空间作为引用添加。当该按钮的 Click 事件发生时,NumericUpDown 控件中文本的磅值将增加。这将提示控件调整它的 PreferredHeight 属性,使所有文本都在该控件中可见。当用户输入新值并离开 NumericUpDown 控件之后,该文本将从字符串值转换为数值,然后验证该值是否在 Minimum 和 Maximum 值之间。如果该值无效,则会显示包含错误的 MessageBox,并且 Select 方法将选择文本,使用户可以输入新值。
Private Sub numericUpDown1_Leave(sender As Object, e As EventArgs)
' If the entered value is greater than Minimum or Maximum,
' select the text and open a message box.
If (System.Convert.ToInt32(numericUpDown1.Text) > numericUpDown1.Maximum) Or _
(System.Convert.ToInt32(numericUpDown1.Text) < numericUpDown1.Minimum) Then
MessageBox.Show("The value entered was not between the Minimum and " & _
"Maximum allowable values." & Microsoft.VisualBasic.ControlChars.Cr & _
"Please re-enter.")
numericUpDown1.Focus()
numericUpDown1.Select(0, numericUpDown1.Text.Length)
End If
End Sub
Private Sub button1_Click(sender As Object, e As EventArgs)
Dim varPrefHeight1 As Integer
' Capture the PreferredHeight before and after the Font
' is changed, and display the results in a message box.
varPrefHeight1 = numericUpDown1.PreferredHeight
numericUpDown1.Font = New System.Drawing.Font("Microsoft Sans Serif", _
12F, System.Drawing.FontStyle.Bold)
MessageBox.Show("Before Font Change: " & varPrefHeight1.ToString() & _
Microsoft.VisualBasic.ControlChars.Cr & "After Font Change: " & _
numericUpDown1.PreferredHeight.ToString())
End Sub
private void numericUpDown1_Leave(Object sender,
EventArgs e)
{
/* If the entered value is greater than Minimum or Maximum,
select the text and open a message box. */
if((System.Convert.ToInt32(numericUpDown1.Text) > numericUpDown1.Maximum) ||
(System.Convert.ToInt32(numericUpDown1.Text) < numericUpDown1.Minimum))
{
MessageBox.Show("The value entered was not between the Minimum and" +
"Maximum allowable values." + "\n" + "Please re-enter.");
numericUpDown1.Focus();
numericUpDown1.Select(0, numericUpDown1.Text.Length);
}
}
private void button1_Click(Object sender,
EventArgs e)
{
int varPrefHeight1;
/* Capture the PreferredHeight before and after the Font
is changed, and display the results in a message box. */
varPrefHeight1 = numericUpDown1.PreferredHeight;
numericUpDown1.Font = new System.Drawing.Font("Microsoft Sans Serif",
12F, System.Drawing.FontStyle.Bold);
MessageBox.Show("Before Font Change: " + varPrefHeight1.ToString() +
"\n" + "After Font Change: " + numericUpDown1.PreferredHeight.ToString());
}
void numericUpDown1_Leave( Object^ /*sender*/, EventArgs^ /*e*/ )
{
/* If the entered value is greater than Minimum or Maximum,
select the text and open a message box. */
if ( (System::Convert::ToInt32( numericUpDown1->Text ) > numericUpDown1->Maximum) || (System::Convert::ToInt32( numericUpDown1->Text ) < numericUpDown1->Minimum) )
{
MessageBox::Show( "The value entered was not between the Minimum andMaximum allowable values.\nPlease re-enter." );
numericUpDown1->Focus();
numericUpDown1->Select(0,numericUpDown1->Text->Length);
}
}
void button1_Click( Object^ /*sender*/, EventArgs^ /*e*/ )
{
int varPrefHeight1;
/* Capture the PreferredHeight before and after the Font
is changed, and display the results in a message box. */
varPrefHeight1 = numericUpDown1->PreferredHeight;
numericUpDown1->Font = gcnew System::Drawing::Font( "Microsoft Sans Serif",12.0,System::Drawing::FontStyle::Bold );
MessageBox::Show( String::Format( "Before Font Change: {0}\nAfter Font Change: {1}", varPrefHeight1, numericUpDown1->PreferredHeight ) );
}
private void numericUpDown1_Leave(Object sender, EventArgs e)
{
/* If the entered value is greater than Minimum or Maximum,
select the text and open a message box.
*/
if (System.Convert.ToInt32(numericUpDown1.get_Text()) >
System.Convert.ToInt32(numericUpDown1.get_Maximum())
|| System.Convert.ToInt32(numericUpDown1.get_Text()) <
System.Convert.ToInt32(numericUpDown1.get_Minimum())) {
MessageBox.Show(("The value entered was not between the Minimum and"
+ "Maximum allowable values." + "\n" + "Please re-enter."));
numericUpDown1.Focus();
numericUpDown1.Select(0, numericUpDown1.get_Text().length());
}
} //NumericUpDown1_Leave
private void button1_Click(Object sender, EventArgs e)
{
int varPrefHeight1;
/* Capture the PreferredHeight before and after the Font
is changed, and display the results in a message box.
*/
varPrefHeight1 = numericUpDown1.get_PreferredHeight();
numericUpDown1.set_Font(
new System.Drawing.Font("Microsoft Sans Serif", 12,
System.Drawing.FontStyle.Bold));
MessageBox.Show(("Before Font Change: "
+ System.Convert.ToString(varPrefHeight1)
+ "\n" + "After Font Change: "
+ System.Convert.ToString(
numericUpDown1.get_PreferredHeight())));
} //button1_Click
平台
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
.NET Compact Framework
受以下版本支持:2.0、1.0
请参见
参考
UpDownBase 类
UpDownBase 成员
System.Windows.Forms 命名空间
UserEdit
UpdateEditText
ValidateEditText