RichTextBox.GetCharIndexFromPosition(Point) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
检索距离指定位置最近的字符索引。
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
参数
- pt
- Point
要搜索的位置。
返回
位于指定位置的从零开始的字符索引。
示例
下面的代码示例演示如何将 方法与 方法结合使用GetCharIndexFromPositionFind,以搜索 控件中的RichTextBox特定字符串,并显示找到的字符串在 控件中的位置的RichTextBox字符索引。 该示例在控件的内容中搜索单词“brown”,并返回在其中找到搜索字符串的字符索引位置。 此示例要求具有一个窗体,其中包含 RichTextBox 名为 richTextBox1
的控件,该控件包含文本。 它还要求示例中的代码连接到 MouseDown 的 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
注解
此方法返回最接近参数中指定的位置的 pt
字符索引。 字符索引是控件中文本(包括空格)的从零开始的索引。 可以通过将鼠标坐标传递给此方法,使用此方法来确定用户在文本中鼠标悬停的位置。 当用户将鼠标指针悬停在控件文本中的某个单词上时,如果要执行任务,这非常有用。