指定した位置の一番近くにある文字のインデックスを取得します。
Public Function GetCharIndexFromPosition( _
ByVal pt As Point _) As Integer
[C#]
public int GetCharIndexFromPosition(Pointpt);
[C++]
public: int GetCharIndexFromPosition(Pointpt);
[JScript]
public function GetCharIndexFromPosition(
pt : Point) : int;
パラメータ
- pt
検索する位置。
戻り値
指定した位置の 0 から始まる文字インデックス。
解説
このメソッドは、 pt パラメータに指定した位置の一番近くにある文字のインデックスを返します。文字のインデックスは、コントロール内のテキスト (空白を含む) の 0 から始まるインデックスです。このメソッドを使用し、マウスの位置座標をこのメソッドに渡すことによって、ユーザーがテキスト内でマウスを配置している位置を判断できます。これは、ユーザーがコントロールのテキスト内の単語にマウス ポインタを配置したときにタスクを実行する場合などに役立ちます。
使用例
[Visual Basic, C#, C++] GetCharIndexFromPosition メソッドと Find メソッドを使用して、 RichTextBox コントロール内で特定の文字列を検索し、見つかった文字列の RichTextBox コントロール内での位置の文字インデックスを表示する方法を次の例に示します。この例では、コントロール内の内容を対象に "brown" という単語を検索し、その検索文字列が見つかった場所の文字インデックスの位置を返しています。この例は、テキストが設定された richTextBox1
という名前の RichTextBox コントロールがフォームに配置されていることを前提にしています。またこの例のコードが、 RichTextBox の MouseDown イベントに関連付けられていることも前提にしています。
Private Sub richTextBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles richTextBox1.MouseDown
' Declare the string to search for in the control.
Dim searchString As String = "brown"
' Determine whether the user clicks the left mouse button and whether it is a double click.
If e.Clicks = 1 And e.Button = MouseButtons.Left Then
' Obtain the character index where the user clicks on the control.
Dim positionToSearch As Integer = richTextBox1.GetCharIndexFromPosition(New Point(e.X, e.Y))
' Search for the search string text within the control from the point the user clicked.
Dim textLocation As Integer = richTextBox1.Find(searchString, positionToSearch, RichTextBoxFinds.None)
' If the search string is found (value greater than -1), display the index the string was found at.
If textLocation >= 0 Then
MessageBox.Show(("The search string was found at character index " + textLocation.ToString() + "."))
' Display a message box alerting the user that the text was not found.
Else
MessageBox.Show("The search string was not found within the text of the control.")
End If
End If
End Sub
[C#]
private void richTextBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
// Declare the string to search for in the control.
string searchString = "brown";
// Determine whether the user clicks the left mouse button and whether it is a double click.
if (e.Clicks == 1 && e.Button == MouseButtons.Left)
{
// Obtain the character index where the user clicks on the control.
int positionToSearch = richTextBox1.GetCharIndexFromPosition(new Point(e.X, e.Y));
// Search for the search string text within the control from the point the user clicked.
int textLocation = richTextBox1.Find(searchString, positionToSearch, RichTextBoxFinds.None);
// If the search string is found (value greater than -1), display the index the string was found at.
if (textLocation >= 0)
MessageBox.Show("The search string was found at character index " + textLocation.ToString() + ".");
else
// Display a message box alerting the user that the text was not found.
MessageBox.Show("The search string was not found within the text of the control.");
}
}
[C++]
private:
void richTextBox1_MouseDown(Object* /*sender*/, System::Windows::Forms::MouseEventArgs* e)
{
// Declare the string to search for in the control.
String* searchString = S"brown";
// Determine whether the user clicks the left mouse button and whether it is a double click.
if (e->Clicks == 1 && e->Button == MouseButtons::Left)
{
// Obtain the character index where the user clicks on the control.
int positionToSearch = richTextBox1->GetCharIndexFromPosition(Point(e->X, e->Y));
// Search for the search string text within the control from the point the user clicked.
int textLocation = richTextBox1->Find(searchString, positionToSearch, RichTextBoxFinds::None);
// If the search string is found (value greater than -1), display the index the string was found at.
if (textLocation >= 0)
MessageBox::Show(String::Format( S"The search string was found at character index {0}.", __box(textLocation)));
else
// Display a message box alerting the user that the text was not found.
MessageBox::Show(S"The search string was not found within the text of the control.");
}
}
[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 名前空間 | GetCharFromPosition | GetLineFromCharIndex | GetPositionFromCharIndex