TextBoxBase.SelectionStart Propriedade
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Obtém ou define o ponto de partida do texto selecionado na caixa de texto.
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
Valor de Propriedade
A posição inicial do texto selecionada na caixa de texto.
- Atributos
Exceções
O valor atribuído é inferior a zero.
Exemplos
Exemplo 1
O seguinte exemplo de código utiliza TextBox, uma classe derivada. Fornece Click gestores de eventos para MenuItem objetos que realizam operações de Cortar, Copiar, Colar e Desfazer. Este exemplo exige que um TextBox controlo nomeado textBox1 tenha sido criado.
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 ))
{
// 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)
{
// 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))
{
// 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)
{
// 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
Exemplo 2
O exemplo seguinte define a SelectionStart propriedade de TextBox um que é ReadOnly atribuindo-lhe a Focus primeira.
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
Public Shared Sub Main()
End Sub
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
Observações
Se não for selecionado texto no controlo, esta propriedade indica o ponto de inserção, ou cardete, para novo texto. Se definir esta propriedade para uma localização além do comprimento do texto no controlo, a posição inicial de seleção será colocada após o último caractere. Quando o texto é selecionado no controlo da caixa de texto, alterar esta propriedade pode diminuir o valor da SelectionLength propriedade. Se o texto restante no controlo após a posição indicada pela SelectionStart propriedade for inferior ao valor da SelectionLength propriedade, o valor da SelectionLength propriedade diminui automaticamente. O valor da SelectionStart propriedade nunca causa um aumento da propriedade SelectionLength .
Podes mover programaticamente a seleção dentro da caixa de texto definindo as SelectionStart propriedades e SelectionLength .
Podes mover programaticamente o caret dentro da caixa de texto definindo o SelectionStart para a posição dentro da caixa de texto para onde queres que o caret se mova e definir a SelectionLength propriedade para um valor zero (0).
O TextBox foco é necessário para que a seleção ou o caret seja movido. Pode definir a SelectionStart propriedade de TextBox um que é ReadOnly atribuindo-lhe a Focus primeira.