TextBoxBase.TextLength 属性

获取控件中文本的长度。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public Overridable ReadOnly Property TextLength As Integer
用法
Dim instance As TextBoxBase
Dim value As Integer

value = instance.TextLength
public virtual int TextLength { get; }
public:
virtual property int TextLength {
    int get ();
}
/** @property */
public int get_TextLength ()
public function get TextLength () : int

属性值

控件的文本中包含的字符数。

备注

可以使用此属性来为一些任务(例如,在搜索控件文本中的特定文本字符串时,需要知道总字符数)确定字符串内的字符数。

示例

下面的代码示例演示如何使用 AppendText 方法和 TextLength 属性将文本从一个 TextBox 复制到另一个文本框。本示例要求将两个名为 textBox1textBox2TextBox 控件添加到窗体,而且 textBox1 已将文本分配给它的 Text 属性。

Private Sub AppendTextBox1Text()
   ' Determine if text is selected in textBox1.
   If textBox1.SelectionLength = 0 Then
      ' No selection made, return.
      Return
   End If
   ' Determine if the text being appended to textBox2 exceeds the MaxLength property.
   If textBox1.SelectedText.Length + textBox2.TextLength > textBox2.MaxLength Then
      MessageBox.Show("The text to paste in is larger than the maximum number of characters allowed")
      ' Append the text from textBox1 into textBox2.
   Else
      textBox2.AppendText(textBox1.SelectedText)
   End If
End Sub
private void AppendTextBox1Text()
{
   // Determine if text is selected in textBox1.
   if(textBox1.SelectionLength == 0)
      // No selection made, return.
      return;
   
   // Determine if the text being appended to textBox2 exceeds the MaxLength property.
   if((textBox1.SelectedText.Length + textBox2.TextLength) > textBox2.MaxLength)
      MessageBox.Show("The text to paste in is larger than the maximum number of characters allowed");
   else
      // Append the text from textBox1 into textBox2.
      textBox2.AppendText(textBox1.SelectedText);
}
void AppendTextBox1Text()
{
   // Determine if text is selected in textBox1.
   if ( textBox1->SelectionLength == 0 )

   // No selection made, return.
   return;

   // Determine if the text being appended to textBox2 exceeds the MaxLength property.
   if ( (textBox1->SelectedText->Length + textBox2->TextLength) > textBox2->MaxLength )
         MessageBox::Show( "The text to paste in is larger than the maximum number of characters allowed" ); // Append the text from textBox1 into textBox2.
   else
         textBox2->AppendText( textBox1->SelectedText );
}
private void AppendTextBox1Text()
{
    // Determine if text is selected in textBox1.
    if (textBox1.get_SelectionLength() == 0) {
        // No selection made, return.
        return;
    }
    // Determine if the text being appended to textBox2 exceeds the MaxLength
    // property.
    if (textBox1.get_SelectedText().get_Length()
        + textBox2.get_TextLength() > textBox2.get_MaxLength()) {
        MessageBox.Show("The text to paste in is larger than the maximum"
            + " number of characters allowed");
    }        
    else {
        // Append the text from textBox1 into textBox2.
        textBox2.AppendText(textBox1.get_SelectedText());
    }        
} //AppendTextBox1Text

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、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

请参见

参考

TextBoxBase 类
TextBoxBase 成员
System.Windows.Forms 命名空间
TextBoxBase.Text 属性