RichTextBox.MaxLength 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置用户可键入或粘贴到富文本框控件中的最大字符数。
public:
virtual property int MaxLength { int get(); void set(int value); };
public override int MaxLength { get; set; }
member this.MaxLength : int with get, set
Public Overrides Property MaxLength As Integer
属性值
可以输入到控件中的字符数。 默认值为 Int32.MaxValue。
例外
分配给该属性的值小于 0。
示例
下面的代码示例演示如何使用 MaxLength 属性来确定分配给 RichTextBox 控件的文本是否大于分配给 MaxLength 该属性的值。 如果文本不较大,则本示例使用 SelectedText 属性将文本分配给控件。 本示例要求已将名为
private:
void AddMyText( String^ textToAdd )
{
// Determine if the text to add is larger than the max length property.
if ( textToAdd->Length > richTextBox1->MaxLength )
// Alert user text is too large.
MessageBox::Show( "The text is too large to add to the RichTextBox" ); // Add the text to be added to the control.
else
richTextBox1->SelectedText = textToAdd;
}
private void AddMyText(string textToAdd)
{
// Determine if the text to add is larger than the max length property.
if (textToAdd.Length > richTextBox1.MaxLength)
// Alert user text is too large.
MessageBox.Show("The text is too large to addo to the RichTextBox");
else
// Add the text to be added to the control.
richTextBox1.SelectedText = textToAdd;
}
Private Sub AddMyText(ByVal textToAdd As String)
' Determine if the text to add is larger than the max length property.
If textToAdd.Length > richTextBox1.MaxLength Then
' Alert user text is too large.
MessageBox.Show("The text is too large to addo to the RichTextBox")
' Add the text to be added to the control.
Else
richTextBox1.SelectedText = textToAdd
End If
End Sub
注解
当此属性设置为 0 时,可在控件中输入的文本的最大长度为 64 KB。 此属性通常用于 RichTextBox 显示单行格式格式(RTF)文本。 可以使用此属性限制在控件中输入的值(如邮政编码和电话号码)中的文本长度,或限制在数据库中输入数据时输入的文本长度。 可以将输入到控件中的文本限制为数据库中相应字段的最大长度。