PowerTools InputMan for Windows Forms 8.0J
SelectionLength プロパティ (EditBase)
使用例 

選択するテキストの文字数を取得または設定します。
構文
Public Overridable Property SelectionLength As Integer
public virtual int SelectionLength {get; set;}

プロパティ値

選択するテキストの文字数を表すInt32値。 既定値は0です。
例外
例外解説
System.ArgumentOutOfRangeException 指定された値が0未満です。
解説

SelectionStart 、 SelectionLength、SelectedTextの 各プロパティを.NET FrameworkのClipboardオブジェクトと 組み合わせて使うと、切り取り、コピー、貼り付けの各操作を実行できます。

Enter イベントの発生時に、ユーザーはフォーカスを受け取った コントロール上でSelectionStart プロパティとSelectionLength プロパティを設定し、カーソル位置と選択範囲を設定することができます。

使用例
派生クラス GcTextBox を使用するコード例を次に示します。切り取り、コピー、貼り付け、元に戻す、テキストの選択開始点や選択文字数の設定などの各操作を実行する System.Windows.Forms.MenuItem.Click オブジェクトの System.Windows.Forms.MenuItem イベント ハンドラを用意します。
//  Please use the following namespace
//  using System.Windows.Forms;
//  using GrapeCity.Win.Editors;

private GcTextBox gcTextBox1 = new GcTextBox();
private void Menu_Copy(System.Object sender, System.EventArgs e)
{
    // Ensure that text is selected in the text box.   
    if (gcTextBox1.SelectionLength > 0)
        // Copy the selected text to the Clipboard.
        gcTextBox1.Copy();
}

private void Menu_Cut(System.Object sender, System.EventArgs e)
{
    // Ensure that text is currently selected in the text box.   
    if (gcTextBox1.SelectedText != "")
        // Cut the selected text in the control and paste it into the Clipboard.
        gcTextBox1.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 (gcTextBox1.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.
                gcTextBox1.SelectionStart = gcTextBox1.SelectionStart + gcTextBox1.SelectionLength;
        }
        // Paste current text in Clipboard into text box.
        gcTextBox1.Paste();
    }
}

private void Menu_Undo(System.Object sender, System.EventArgs e)
{
    // Determine if last operation can be undone in text box.   
    if (gcTextBox1.CanUndo == true)
    {
        // Undo the last operation.
        gcTextBox1.Undo();
        // Clear the undo buffer to prevent last action from being redone.
        gcTextBox1.ClearUndo();
    }
}
'  Please use the following namespace
'  Imports System.Windows.Forms;
'  Imports GrapeCity.Win.Editors;

Private gcTextBox1 As New GcTextBox()
Private Sub Menu_Copy(ByVal sender As System.Object, ByVal e As System.EventArgs)
    ' Ensure that text is selected in the text box.   
    If gcTextBox1.SelectionLength > 0 Then
        ' Copy the selected text to the Clipboard.
        gcTextBox1.Copy()
    End If
End Sub

Private Sub Menu_Cut(ByVal sender As System.Object, ByVal e As System.EventArgs)
    ' Ensure that text is currently selected in the text box.   
    If gcTextBox1.SelectedText <> "" Then
        ' Cut the selected text in the control and paste it into the Clipboard.
        gcTextBox1.Cut()
    End If
End Sub

Private Sub Menu_Paste(ByVal sender As System.Object, ByVal 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 gcTextBox1.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.
                gcTextBox1.SelectionStart = gcTextBox1.SelectionStart + gcTextBox1.SelectionLength
            End If
        End If
        ' Paste current text in Clipboard into text box.
        gcTextBox1.Paste()
    End If
End Sub

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

EditBase クラス
EditBase メンバ
SelectionStart プロパティ
SelectedText プロパティ
Select メソッド

 

 


© 2004-2015 GrapeCity inc. All rights reserved.