RichTextBox.GetCharIndexFromPosition(Point) Methode
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
Haalt de index op van het teken dat zich het dichtst bij de opgegeven locatie bevindt.
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
Parameters
- pt
- Point
De locatie om te zoeken.
Retouren
De op nul gebaseerde tekenindex op de opgegeven locatie.
Voorbeelden
In het volgende codevoorbeeld ziet u hoe u de GetCharIndexFromPosition methode gebruikt met de Find methode om te zoeken naar een specifieke tekenreeks binnen een RichTextBox besturingselement en de tekenindex weer te geven waarin de gevonden tekenreeks zich in het RichTextBox besturingselement bevindt. In het voorbeeld wordt gezocht naar het woord 'bruin' in de inhoud van het besturingselement en wordt de positie van de tekenindex geretourneerd waar de zoekreeks wordt gevonden. Voor dit voorbeeld moet u een formulier hebben met een besturingselement met de RichTextBox naam richTextBox1 tekst. Het vereist ook dat de code in het voorbeeld is verbonden met de gebeurtenis van de MouseDownRichTextBox.
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
Opmerkingen
Deze methode retourneert de tekenindex die zich het dichtst bij de positie bevindt die is opgegeven in de pt parameter. De tekenindex is een op nul gebaseerde index van tekst in het besturingselement, inclusief spaties. U kunt deze methode gebruiken om te bepalen waar in de tekst de gebruiker de muisaanwijzer heeft door de muiscoƶrdinaten door te geven aan deze methode. Dit kan handig zijn als u taken wilt uitvoeren wanneer de gebruiker de muisaanwijzer op een woord in de tekst van het besturingselement plaatst.