在插入位置下查找文本

时间:2013-05-02 18:37:56

标签: c# vb.net

我可以在任何应用程序中找到插入位置,但我需要知道当前插入位置有哪些文本(字)。

我如何获取文字?

2 个答案:

答案 0 :(得分:2)

很难理解你的问题,这个问题似乎主要是作为一种陈述。

假设我理解你的问题,试试这样的方法......

Private Sub CheckPosition()
Dim char_pos As Long
Dim row As Long
Dim col As Long

char_pos = SendMessage(Text1.hwnd, EM_GETSEL, 0, 0)
char_pos = char_pos \ &H10000

row = SendMessage(Text1.hwnd, EM_LINEFROMCHAR, _
char_pos, 0) + 1
col = char_pos - SendMessage(Text1.hwnd, EM_LINEINDEX, _
-1, 0) + 1

lblPosition.Caption = "(" & Format$(row) & ", " & _
Format$(col) & ")"
End Sub

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As _
Integer)
CheckPosition
End Sub

Private Sub Text1_KeyUp(KeyCode As Integer, Shift As _
Integer)
CheckPosition
End Sub

Private Sub Text1_MouseDown(Button As Integer, Shift As _
Integer, X As Single, Y As Single)
CheckPosition
End Sub

Private Sub Text1_MouseUp(Button As Integer, Shift As _
Integer, X As Single, Y As Single)
CheckPosition
End Sub 

答案 1 :(得分:2)

如果您使用的是WinForms应用程序,并且根据插入位置,您的意思是在文本框中使用Caret Position。然后你可以做这样的事情。

  • 1.将事件处理程序附加到KeyUp和MouseUp事件
  • 2.获取当前文本框文本和插入位置
  • 3.将此传递给返回该位置下的单词的函数
  •     private void textBox1_KeyUp(object sender, EventArgs e)
        {
            GetWordFromCaretPosition(textBox1.Text, textBox1.SelectionStart);
        }
    
        private void textBox1_MouseUp(object sender, EventArgs e)
        {
            GetWordFromCaretPosition(textBox1.Text, textBox1.SelectionStart);
        }
    
        private string GetWordFromCaretPosition(string input, int position)
        {
            string word = string.Empty;
            //Yet to be implemented.
            return word;
        }
    

  • 对于WPF文本框,插入位置由textBox1.CaretIndex
  • 表示
  • 对于WPF RichTextBox,请参阅此主题:WPF RichTextBox - get whole word at current caret position
  • 对于Windows Phone 7,插入位置由textBox1.SelectionStart表示。如果您的应用是Windows Phone应用,请参阅此主题:Selecting the tapped-on word on a single click in textbox