다음을 통해 공유


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부터 시작하는 문자 인덱스입니다.

설명

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

예제

다음 코드 예제에서는 GetCharIndexFromPosition 메서드를 Find 메서드와 함께 사용하여 RichTextBox 컨트롤 내의 특정 문자열을 검색하고 RichTextBox 컨트롤 내에서 찾은 문자열의 위치를 나타내는 문자 인덱스를 표시합니다. 이 예제에서는 컨트롤의 내용 내에서 단어 "brown"을 검색하고 해당 검색 문자열을 찾은 문자 인덱스 위치를 반환합니다. 이 예제를 실행하려면 텍스트가 포함된 richTextBox1이라는 RichTextBox 컨트롤이 폼에 있어야 합니다. 또한 예제의 코드가 RichTextBoxMouseDown 이벤트에 연결되어 있어야 합니다.

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에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

참고 항목

참조

RichTextBox 클래스
RichTextBox 멤버
System.Windows.Forms 네임스페이스
GetLineFromCharIndex
GetPositionFromCharIndex