RichTextBox.GetCharIndexFromPosition Method

Retrieves the index of the character nearest to the specified location.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public override int GetCharIndexFromPosition(
	Point pt
)

Parameters

pt
Type: System.Drawing.Point
The location to search.

Return Value

Type: System.Int32
The zero-based character index at the specified location.

This method returns the character index that is closest to the position specified in the pt parameter. The character index is a zero-based index of text in the control, including spaces. You can use this method to determine where in the text the user has the mouse over by passing the mouse coordinates to this method. This can be useful if you want to perform tasks when the user rests the mouse pointer over a word in the text of the control.

The following code example demonstrates how to use the GetCharIndexFromPosition method with the Find method to search for a specific string within a RichTextBox control and display the character index where the found string is located within the RichTextBox control. The example searches for the word "brown" within the contents of the control and returns the character index position where the search string is found. This example requires that you have a form that contains a RichTextBox control named richTextBox1 that contains text. It also requires that the code in the example is connected to the MouseDown event of the 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(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.");
			}
		}


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft