TextBoxBase.SelectionStart 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定文字方塊中選取文字的起點。
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
屬性值
文字方塊中選取的文字開始位置。
- 屬性
例外狀況
指派的值小於零。
範例
範例 1
下列程式碼範例使用 TextBox 衍生類別。 它會為 MenuItem 執行剪下、複製、貼上和復原作業的物件提供 Click 事件處理常式。 此範例會要求 TextBox 已建立名為 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
範例 2
下列範例會 SelectionStart 藉由提供 Focus 第一個 TextBoxReadOnly 屬性來設定 的 屬性。
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
備註
如果控制項中未選取任何文字,這個屬性會指出新文字的插入點或插入號。 如果您將此屬性設定為超出控制項中文字長度的位置,選取範圍開始位置將會放在最後一個字元之後。 在文字方塊控制項中選取文字時,變更此屬性可能會降低 屬性的值 SelectionLength 。 如果 屬性所指示 SelectionStart 的位置之後控制項中的剩餘文字小於 屬性的值 SelectionLength ,則屬性的值 SelectionLength 會自動減少。 屬性的值 SelectionStart 永遠不會造成 屬性增加 SelectionLength 。
您可以藉由設定 SelectionStart 和 屬性,以程式設計方式在文字方塊中移動選取範圍 SelectionLength 。
您可以透過程式設計方式移動文字方塊內的插入號,方法是將 設定 SelectionStart 為要移動插入號的文字方塊內的位置,並將 屬性設定 SelectionLength 為零 (0) 。
TextBox必須有焦點,才能移動選取範圍或插入號。 您可以藉由提供 Focus 第一個 TextBox 屬性來設定 SelectionStart 的 ReadOnly 屬性。