RichTextBox.GetCharIndexFromPosition(Point) Metod
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Hämtar indexet för tecknet närmast den angivna platsen.
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
Parametrar
- pt
- Point
Sökvägen.
Returer
Det nollbaserade teckenindexet på den angivna platsen.
Exempel
Följande kodexempel visar hur du använder GetCharIndexFromPosition metoden med Find metoden för att söka efter en specifik sträng i en RichTextBox kontroll och visa teckenindexet där den hittade strängen RichTextBox finns i kontrollen. Exemplet söker efter ordet "brun" i innehållet i kontrollen och returnerar teckenindexpositionen där söksträngen hittas. Det här exemplet kräver att du har ett formulär som innehåller en RichTextBox kontroll med namnet richTextBox1 som innehåller text. Det kräver också att koden i exemplet är ansluten till MouseDown händelsen för RichTextBox.
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
Kommentarer
Den här metoden returnerar det teckenindex som är närmast den position som anges i parametern pt . Teckenindexet är ett nollbaserat textindex i kontrollen, inklusive blanksteg. Du kan använda den här metoden för att avgöra var i texten användaren har musen över genom att skicka muskoordinaterna till den här metoden. Detta kan vara användbart om du vill utföra uppgifter när användaren vilar muspekaren över ett ord i kontrollens text.