TextBoxBase.SelectionStart Vlastnost
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Získá nebo nastaví výchozí bod textu vybraného v textovém poli.
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
Hodnota vlastnosti
Počáteční pozice textu vybraného v textovém poli
- Atributy
Výjimky
Přiřazená hodnota je menší než nula.
Příklady
Příklad 1
Následující příklad kódu používá TextBoxodvozenou třídu. Poskytuje Click obslužné rutiny událostí pro MenuItem objekty, které provádějí operace vyjmutí, kopírování, vložení a vrácení zpět. Tento příklad vyžaduje vytvoření TextBox ovládacího prvku s názvem 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 ))
{
// 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
Příklad 2
Následující příklad nastaví SelectionStart vlastnost TextBox , která je ReadOnly tím, že jí Focus dává první.
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
Poznámky
Pokud není v ovládacím prvku vybrán žádný text, tato vlastnost označuje kurzor nebo kurzor pro nový text. Pokud tuto vlastnost nastavíte na místo nad délkou textu v ovládacím prvku, umístění začátku výběru se umístí za poslední znak. Pokud je text vybrán v ovládacím prvku textového pole, může změna této vlastnosti snížit hodnotu SelectionLength vlastnosti. Pokud zbývající text v ovládacím prvku za pozicí označenou SelectionStart vlastností je menší než hodnota SelectionLength vlastnosti, hodnota SelectionLength vlastnosti se automaticky sníží. Hodnota SelectionStart vlastnosti nikdy nezpůsobí zvýšení SelectionLength vlastnosti.
Výběr v textovém poli můžete přesunout prostřednictvím kódu programu nastavením SelectionStart a SelectionLength vlastností.
Kurzor v textovém poli můžete přesunout prostřednictvím kódu programu tak, že nastavíte SelectionStart pozici v textovém poli, do kterého se má kurzor přesunout, a vlastnost nastavíte SelectionLength na hodnotu nuly (0).
Aby TextBox se výběr nebo kurzor přesunul, musí mít fokus. Vlastnost tohoto objektu SelectionStartTextBox můžete nastavit tak, že ReadOnly ji Focus dáte jako první.