RichTextBox.GetCharIndexFromPosition メソッド
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)

Dim instance As RichTextBox Dim pt As Point Dim returnValue As Integer returnValue = instance.GetCharIndexFromPosition(pt)
戻り値
指定した位置の 0 から始まる文字インデックス。

このメソッドは、pt パラメータに指定した位置の一番近くにある文字のインデックスを返します。文字のインデックスは、コントロール内のテキスト (空白を含む) の 0 から始まるインデックスです。このメソッドを使用し、マウスの位置座標をこのメソッドに渡すことによって、ユーザーがテキスト内でマウスを配置している位置を判断できます。これは、ユーザーがコントロールのテキスト内の単語にマウス ポインタを配置したときにタスクを実行する場合などに役立ちます。

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
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."); } }
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( 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( "The search string was found at character index {0}.", textLocation ) ); // 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." ); } }
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.get_Clicks() == 1 && e.get_Button().Equals(get_MouseButtons(). Left)) { // Obtain the character index where the user clicks on the control. int positionToSearch = richTextBox1.GetCharIndexFromPosition( new Point(e.get_X(), e.get_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 " + ((System.Int32)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."); } } } //richTextBox1_MouseDown

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


RichTextBox クラス
RichTextBox メンバ
System.Windows.Forms 名前空間
GetLineFromCharIndex
GetPositionFromCharIndex
Weblioに収録されているすべての辞書からRichTextBox.GetCharIndexFromPosition メソッドを検索する場合は、下記のリンクをクリックしてください。

- RichTextBox.GetCharIndexFromPosition メソッドのページへのリンク