Share via


SetFocus 方法、LineCount、TextLength 屬性範例

下列範例會使用LineCountTextLength屬性,以及SetFocus方法,計算TextBox中的字元和文字行數。

在本範例中,使用者在 TextBox 中輸入值,然後取出 LineCountTextLength 屬性的目前值。

若要使用本範例,請將此範例程式碼複製到表單的宣告部分中。 請確定該表單包含:

  • 一個名為 TextBox1 的 TextBox
  • 一個名為 CommandButton1 的 CommandButton
  • 兩個分別名為 Label1 和 Label2 的 Label 控制項。
'Type SHIFT+ENTER to start a new line in the text box. 
 
Private Sub CommandButton1_Click() 
 'Must first give TextBox1 the focus to get line 
 'count 
 TextBox1.SetFocus 
 Label1.Caption = "LineCount = " _ 
 & TextBox1.LineCount 
 Label2.Caption = "TextLength = " _ 
 & TextBox1.TextLength 
End Sub 
 
Private Sub UserForm_Initialize() 
 CommandButton1.WordWrap = True 
 CommandButton1.AutoSize = True 
 CommandButton1.Caption = "Get Counts" 
 
 Label1.Caption = "LineCount = " 
 Label2.Caption = "TextLength = " 
 
 TextBox1.MultiLine = True 
 TextBox1.WordWrap = True 
 TextBox1.Text = "Enter your text here." 
End Sub

支援和意見反應

有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應