TextBoxBase.SelectionStart Proprietà

Definizione

Ottiene o imposta il punto di inizio del testo selezionato nella casella di testo.

public:
 property int SelectionStart { int get(); void set(int value); };
[System.ComponentModel.Browsable(false)]
public int SelectionStart { get; set; }
[<System.ComponentModel.Browsable(false)>]
member this.SelectionStart : int with get, set
Public Property SelectionStart As Integer

Valore della proprietà

La posizione iniziale del testo selezionato nella casella di testo.

Attributi

Eccezioni

Il valore assegnato è minore di zero.

Esempio

Esempio 1

Nell'esempio di codice seguente viene TextBoxusata una classe derivata. Fornisce Click gestori eventi per MenuItem gli oggetti che eseguono operazioni Taglia, Copia, Incolla e Annulla. Questo esempio richiede che sia stato creato un TextBox controllo denominato textBox1 .

private:
   void Menu_Copy( System::Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      // Ensure that text is selected in the text box.   
      if ( textBox1->SelectionLength > 0 )
      {
         // Copy the selected text to the Clipboard.
         textBox1->Copy();
      }
   }

   void Menu_Cut( System::Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      // Ensure that text is currently selected in the text box.   
      if (  !textBox1->SelectedText->Equals( "" ) )
      {
         // Cut the selected text in the control and paste it into the Clipboard.
         textBox1->Cut();
      }
   }

   void Menu_Paste( System::Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      // Determine if there is any text in the Clipboard to paste into the text box.
      if ( Clipboard::GetDataObject()->GetDataPresent( DataFormats::Text ) == true )
      {
         // Determine if any text is selected in the text box.
         if ( textBox1->SelectionLength > 0 )
         {
            // Ask user if they want to paste over currently selected text.
            if ( MessageBox::Show( "Do you want to paste over current selection?",
               "Cut Example", MessageBoxButtons::YesNo ) == ::DialogResult::No )
            {
               // Move selection to the point after the current selection and paste.
               textBox1->SelectionStart = textBox1->SelectionStart + textBox1->SelectionLength;
            }
         }
         // Paste current text in Clipboard into text box.
         textBox1->Paste();
      }
   }

   void Menu_Undo( System::Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      // Determine if last operation can be undone in text box.   
      if ( textBox1->CanUndo == true )
      {
         // Undo the last operation.
         textBox1->Undo();
         // Clear the undo buffer to prevent last action from being redone.
         textBox1->ClearUndo();
      }
   }
private void Menu_Copy(System.Object sender, System.EventArgs e)
 {
    // Ensure that text is selected in the text box.   
    if(textBox1.SelectionLength > 0)
        // Copy the selected text to the Clipboard.
        textBox1.Copy();
 }
 
 private void Menu_Cut(System.Object sender, System.EventArgs e)
 {   
     // Ensure that text is currently selected in the text box.   
     if(textBox1.SelectedText != "")
        // Cut the selected text in the control and paste it into the Clipboard.
        textBox1.Cut();
 }
 
 private void Menu_Paste(System.Object sender, System.EventArgs e)
 {
    // Determine if there is any text in the Clipboard to paste into the text box.
    if(Clipboard.GetDataObject().GetDataPresent(DataFormats.Text) == true)
    {
        // Determine if any text is selected in the text box.
        if(textBox1.SelectionLength > 0)
        {
          // Ask user if they want to paste over currently selected text.
          if(MessageBox.Show("Do you want to paste over current selection?", "Cut Example", MessageBoxButtons.YesNo) == DialogResult.No)
             // Move selection to the point after the current selection and paste.
             textBox1.SelectionStart = textBox1.SelectionStart + textBox1.SelectionLength;
        }
        // Paste current text in Clipboard into text box.
        textBox1.Paste();
    }
 }

 private void Menu_Undo(System.Object sender, System.EventArgs e)
 {
    // Determine if last operation can be undone in text box.   
    if(textBox1.CanUndo == true)
    {
       // Undo the last operation.
       textBox1.Undo();
       // Clear the undo buffer to prevent last action from being redone.
       textBox1.ClearUndo();
    }
 }
Private Sub Menu_Copy(sender As System.Object, e As System.EventArgs)
    ' Ensure that text is selected in the text box.   
    If textBox1.SelectionLength > 0 Then
        ' Copy the selected text to the Clipboard.
        textBox1.Copy()
    End If
End Sub
 
Private Sub Menu_Cut(sender As System.Object, e As System.EventArgs)
    ' Ensure that text is currently selected in the text box.   
    If textBox1.SelectedText <> "" Then
        ' Cut the selected text in the control and paste it into the Clipboard.
        textBox1.Cut()
    End If
End Sub
 
Private Sub Menu_Paste(sender As System.Object, e As System.EventArgs)
    ' Determine if there is any text in the Clipboard to paste into the text box.
    If Clipboard.GetDataObject().GetDataPresent(DataFormats.Text) = True Then
        ' Determine if any text is selected in the text box.
        If textBox1.SelectionLength > 0 Then
            ' Ask user if they want to paste over currently selected text.
            If MessageBox.Show("Do you want to paste over current selection?", _
                "Cut Example", MessageBoxButtons.YesNo) = DialogResult.No Then
                ' Move selection to the point after the current selection and paste.
                textBox1.SelectionStart = textBox1.SelectionStart + _
                    textBox1.SelectionLength
            End If
        End If 
        ' Paste current text in Clipboard into text box.
        textBox1.Paste()
    End If
End Sub

Private Sub Menu_Undo(sender As System.Object, e As System.EventArgs)
    ' Determine if last operation can be undone in text box.   
    If textBox1.CanUndo = True Then
        ' Undo the last operation.
        textBox1.Undo()
        ' Clear the undo buffer to prevent last action from being redone.
        textBox1.ClearUndo()
    End If
End Sub

Esempio 2

Nell'esempio seguente viene impostata la SelectionStart proprietà di un TextBox oggetto che lo ReadOnly assegna al Focus primo.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.button1.Click += new System.EventHandler(this.ButtonClickWork);
    }

    private void ButtonClickWork(object sender, EventArgs e)
    {
        this.textBox1.Text = "Hello world!";
        this.textBox1.ReadOnly = true;

        this.textBox1.Focus();
        this.textBox1.SelectionStart = this.textBox1.SelectionStart + 1;
        this.textBox1.SelectionLength = 1;
    }
}
Public Class Form1

    Private Sub ButtonClickWork(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles Button1.Click

        Me.TextBox1.Text = "Hello world!"
        Me.TextBox1.ReadOnly = True

        Me.TextBox1.Focus()
        Me.TextBox1.SelectionStart = Me.TextBox1.SelectionStart + 1
        Me.TextBox1.SelectionLength = 1
    End Sub
End Class

Commenti

Se nel controllo non è selezionato alcun testo, questa proprietà indica il punto di inserimento o il punto di inserimento per il nuovo testo. Se si imposta questa proprietà su una posizione oltre la lunghezza del testo nel controllo, la posizione iniziale della selezione verrà posizionata dopo l'ultimo carattere. Quando il testo è selezionato nel controllo casella di testo, la modifica di questa proprietà potrebbe ridurre il valore della SelectionLength proprietà. Se il testo rimanente nel controllo dopo la posizione indicata dalla SelectionStart proprietà è minore del valore della proprietà, il valore della SelectionLengthSelectionLength proprietà viene automaticamente ridotto. Il valore della SelectionStart proprietà non causa mai un aumento della SelectionLength proprietà.

È possibile spostare a livello di codice la selezione all'interno della casella di testo impostando le SelectionStartSelectionLength proprietà e .

È possibile spostare a livello di codice il cursore all'interno della casella di testo impostando la SelectionStart posizione all'interno della casella di testo in cui si vuole spostare il cursore e impostare la SelectionLength proprietà su un valore pari a zero (0).

L'oggetto TextBox deve avere lo stato attivo per la selezione o il cursore da spostare. È possibile impostare la SelectionStart proprietà di un TextBox oggetto che lo ReadOnly assegna al Focus primo.

Si applica a