共用方式為


如何:控制 Windows Form TextBox 控制項的插入點

當 Windows Forms TextBox 控制項第一次收到焦點時,文字輸入框內的預設插入位置是任何現有文字的左邊。 使用者可以使用鍵盤或滑鼠移動插入點。 如果文字輸入框遺失,然後重新取得焦點,則插入點會是使用者最後放置的位置。

在某些情況下,此行為可能會讓使用者感到困惑。 在文書處理應用程式中,使用者可能會預期新字元出現在任何現有文字之後。 在資料輸入應用程式中,使用者可能預期新字元會取代任何現有的輸入。 SelectionStartSelectionLength 屬性可讓您修改此行為,以符合您的目的。

若要控制 TextBox 控制項的插入點

  1. WordWrap 屬性設定為適當值。 設為零則會將插入點放在緊鄰第一個字元的左邊。

  2. (選擇性) 將 SelectionLength 屬性設定為您想要選取之文字的長度。

    下列程式碼一律會使插入點返回至 0。 TextBox1_Enter 事件處理常式必須繫結至控制項;如需詳細資訊,請參閱在 Windows Forms 中建立事件處理常式

    Private Sub TextBox1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Enter
       TextBox1.SelectionStart = 0
       TextBox1.SelectionLength = 0
    End Sub
    
    private void textBox1_Enter(Object sender, System.EventArgs e) {
       textBox1.SelectionStart = 0;
       textBox1.SelectionLength = 0;
    }
    
    private:
       void textBox1_Enter(System::Object ^  sender,
          System::EventArgs ^  e)
       {
          textBox1->SelectionStart = 0;
          textBox1->SelectionLength = 0;
       }
    

將插入點預設為顯示

只有在 TextBox 控制項第一次處於定位順序時,新表單中才會預設為顯示 TextBox 插入點。 否則,只有在您使用鍵盤或滑鼠給予 TextBox 焦點時,才會顯示插入點。

若要讓文字輸入框的插入點預設顯示在新表單上

另請參閱