Freigeben über


RichTextBox.GetCharIndexFromPosition-Methode

Ruft den Index des Zeichens ab, das der angegebenen Position am nächsten liegt.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

Syntax

'Declaration
Public Overrides Function GetCharIndexFromPosition ( _
    pt As Point _
) As Integer
'Usage
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

Parameter

  • pt
    Die zu suchende Position.

Rückgabewert

Der nullbasierte Zeichenindex an der angegebenen Position.

Hinweise

Diese Methode gibt den Zeichenindex zurück, der der im pt-Parameter angegebenen Position am nächsten liegt. Der Zeichenindex ist ein nullbasierter Index von Text im Steuerelement, einschließlich Leerzeichen. Mit dieser Methode können Sie feststellen, auf welche Position im Text der Mauszeiger des Benutzers zeigt, indem Sie die Mauskoordinaten an diese Methode übergeben. Dies kann sinnvoll sein, wenn Sie Tasks ausführen möchten, während der Benutzer mit dem Mauszeiger auf ein Wort im Text des Steuerelements zeigt.

Beispiel

Im folgenden Codebeispiel wird veranschaulicht, wie die GetCharIndexFromPosition-Methode mit der Find-Methode verwendet wird, um in einem RichTextBox-Steuerelement nach einer bestimmten Zeichenfolge zu suchen und den Zeichenindex anzuzeigen, an dem sich die gefundene Zeichenfolge im RichTextBox-Steuerelement befindet. Im Beispiel wird der Inhalt des Steuerelements nach dem Wort "brown" durchsucht und die Zeichenindexposition zurückgegeben, an der die Suchzeichenfolge gefunden wurde. Für dieses Beispiel ist es erforderlich, dass ein Formular mit einem RichTextBox-Steuerelement mit dem Namen richTextBox1 vorhanden ist und dieses Text enthält. Des Weiteren ist es erforderlich, dass der Code im Beispiel mit dem MouseDown-Ereignis der RichTextBox verbunden ist.

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

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

Siehe auch

Referenz

RichTextBox-Klasse
RichTextBox-Member
System.Windows.Forms-Namespace
GetLineFromCharIndex
GetPositionFromCharIndex