RichTextBox.GetCharIndexFromPosition(Point) 메서드

정의

지정된 위치에 가장 가까운 문자의 인덱스를 검색합니다.

public:
 int GetCharIndexFromPosition(System::Drawing::Point pt);
public:
 override int GetCharIndexFromPosition(System::Drawing::Point pt);
public int GetCharIndexFromPosition (System.Drawing.Point pt);
public override int GetCharIndexFromPosition (System.Drawing.Point pt);
member this.GetCharIndexFromPosition : System.Drawing.Point -> int
override this.GetCharIndexFromPosition : System.Drawing.Point -> int
Public Function GetCharIndexFromPosition (pt As Point) As Integer
Public Overrides Function GetCharIndexFromPosition (pt As Point) As Integer

매개 변수

pt
Point

검색할 위치입니다.

반환

지정된 위치의 0부터 시작하는 문자 인덱스입니다.

예제

다음 코드 예제를 사용 하는 방법에 설명 합니다 메서드를 Find 사용 하 여 GetCharIndexFromPosition 특정 문자열을 검색 하는 메서드 내에서 특정 문자열을 RichTextBox 검색 하 고 검색 된 문자열이 컨트롤 내에 있는 문자 인덱스를 RichTextBox 표시 합니다. 이 예제에서는 컨트롤의 내용 내에서 "brown"이라는 단어를 검색하고 검색 문자열이 있는 문자 인덱스 위치를 반환합니다. 이 예제에서는 텍스트가 포함된 라는 richTextBox1 컨트롤이 RichTextBox 포함된 폼이 있어야 합니다. 또한 예제의 코드가 의 RichTextBox이벤트에 연결되어야 합니다MouseDown.

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.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 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

설명

이 메서드는 매개 변수에 지정된 위치에 가장 가까운 문자 인덱스 를 pt 반환합니다. 문자 인덱스는 공백을 포함하여 컨트롤의 텍스트 인덱스(0부터 시작)입니다. 이 메서드를 사용하여 사용자가 이 메서드에 마우스 좌표를 전달하여 텍스트에서 마우스가 있는 위치를 확인할 수 있습니다. 사용자가 컨트롤의 텍스트에 있는 단어 위에 마우스 포인터를 놓을 때 작업을 수행하려는 경우에 유용할 수 있습니다.

적용 대상

추가 정보