我在表单中有两个richtextbox。我想通过单击按钮添加文本。我如何知道光标当前是哪个richtextbox?
我的代码是这样的:
If rtbContent1.Focused = True Then
rtbContent1.SelectedText = "New Text"
ElseIf rtbContent2.Focused = True Then
rtbContent2.SelectedText = "New Text"
End If
但它不起作用。
答案 0 :(得分:1)
当您单击按钮时,cursot不会出现在文本框中,焦点将转移到按钮。但是你可以通过保持对最后一个聚焦的RichTextBox的引用来实现这一点。
在两个RichTextBox上附加Enter-event并将最后一个重点CheckBox存储在一个私有变量中:
Private lastFocusedTextBox As RichTextBox = Nothing
Private Sub RichTextBoxes_Enter(sender As Object, e As EventArgs) Handles RichTextBox1.Enter, RichTextBox2.Enter
lastFocusedTextBox = CType(sender, RichTextBox)
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If lastFocusedTextBox IsNot Nothing Then
lastFocusedTextBox.SelectedText = "My Text"
End If
End Sub