次の方法で共有


RichTextBox.GetCharFromPosition メソッド

コントロール内の指定した位置の一番近くにある文字を取得します。

Public Function GetCharFromPosition( _
   ByVal pt As Point _) As Char
[C#]
public char GetCharFromPosition(Pointpt);
[C++]
public: __wchar_t GetCharFromPosition(Pointpt);
[JScript]
public function GetCharFromPosition(
   pt : Point) : Char;

パラメータ

  • pt
    一番近くにある文字をシークする位置。

戻り値

指定した位置にある文字。

解説

pt パラメータに指定した位置がコントロールのクライアント領域の外側にある場合は、 pt で指定した位置の一番近くにある文字列の最初の文字が返されます。このメソッドを使用して、コントロール内の特定位置の近くにある文字を確認できます。その後、この値を使用して、その位置にあるテキストを操作できます。

メモ    pt パラメータに指定した位置がコントロールのクライアント領域の右側にある場合は、 pt で指定した位置の一番近くにある文字列の最後の文字が返されます。

使用例

[Visual Basic, C#, C++] GetCharFromPosition メソッドを使用して、 RichTextBox コントロールの内容から、指定した座標の文字を取得する例を次に示します。この例のコードでは、 MouseEventArgs オブジェクトに格納された座標情報を使用して、コントロール内の該当座標にある文字を取得しています。見つかった文字が空白文字ではなかった場合、その文字は MessageBox に表示されます。この例は、 richTextBox1 という名前の RichTextBox コントロールがフォームに配置され、この例のコードが RichTextBoxMouseDown イベントに関連付けられていることを前提にしています。

 
Private Sub richTextBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles richTextBox1.MouseDown
    ' Determine which mouse button is clicked.
    If e.Button = MouseButtons.Left Then
        ' Obtain the character at which the mouse cursor was clicked.
        Dim tempChar As Char = richTextBox1.GetCharFromPosition(New Point(e.X, e.Y))
        ' Determine whether the character is an empty space.
        If tempChar <> " " Then
            ' Display the character in a message box.
            MessageBox.Show(("The character at the specified position is " + tempChar + "."))
        End If
    End If
End Sub

[C#] 
private void richTextBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
    // Determine which mouse button is clicked.
    if(e.Button == MouseButtons.Left)
    {
        // Obtain the character at which the mouse cursor was clicked at.
        char tempChar = richTextBox1.GetCharFromPosition(new Point(e.X, e.Y));
        // Determine whether the character is an empty space.
        if (tempChar.ToString() != " ")
            // Display the character in a message box.
            MessageBox.Show("The character at the specified position is " + tempChar + ".");

    }
}

[C++] 
private:
    void richTextBox1_MouseDown(Object* /*sender*/, System::Windows::Forms::MouseEventArgs* e)
    {
        // Determine which mouse button is clicked.
        if(e->Button == MouseButtons::Left)
        {
            // Obtain the character at which the mouse cursor was clicked at.
            Char tempChar = richTextBox1->GetCharFromPosition(Point(e->X, e->Y));
            // Determine whether the character is an empty space.
            if (!tempChar.ToString()->Equals(S" "))
                // Display the character in a message box.
                MessageBox::Show(String::Format( S"The character at the specified position is {0}.", __box(tempChar)));

        }
    }

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

RichTextBox クラス | RichTextBox メンバ | System.Windows.Forms 名前空間 | GetCharIndexFromPosition | GetLineFromCharIndex | GetPositionFromCharIndex