RichTextBox.GetCharIndexFromPosition メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > RichTextBox.GetCharIndexFromPosition メソッドの意味・解説 

RichTextBox.GetCharIndexFromPosition メソッド

指定した位置の一番近くにある文字インデックス取得します

名前空間: System.Windows.Forms
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)
構文構文

Public Overrides Function
 GetCharIndexFromPosition ( _
    pt As Point _
) As Integer
Dim instance As RichTextBox
Dim pt As Point
Dim returnValue As Integer

returnValue = instance.GetCharIndexFromPosition(pt)
public override int GetCharIndexFromPosition
 (
    Point pt
)
public:
virtual int GetCharIndexFromPosition (
    Point pt
) override
public int GetCharIndexFromPosition (
    Point pt
)
public override function GetCharIndexFromPosition
 (
    pt : Point
) : int

パラメータ

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
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
RichTextBox クラス
RichTextBox メンバ
System.Windows.Forms 名前空間
GetLineFromCharIndex
GetPositionFromCharIndex


このページでは「.NET Framework クラス ライブラリ リファレンス」からRichTextBox.GetCharIndexFromPosition メソッドを検索した結果を表示しています。
Weblioに収録されているすべての辞書からRichTextBox.GetCharIndexFromPosition メソッドを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からRichTextBox.GetCharIndexFromPosition メソッド を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

RichTextBox.GetCharIndexFromPosition メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



RichTextBox.GetCharIndexFromPosition メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS