Selected text not highlighted until parent form clicked

robertb_NZ

Well-known member
Joined
May 11, 2010
Messages
146
Location
Auckland, New Zealand
Programming Experience
10+
I am trying to add Find and Replace functions to a RichTextBox editor (Jazz Workbench). I found an example that did what I wanted, downloaded the sample application, and copied frmFind into my project. It works perfectly except that: Find doesn't highlight the found text until you click the parent form. When you do frmFind remains visible (correct), but is no longer the active screen.

The exact same code works perfectly when I run the sample application in its original form; frmFind remains the active form, but the found text is highlighted, just as in Notepad and Visual Studio.

To simplify this problem I created frmFind2 with nothing but a textbox and a button, and this code: -
Public Class frmFind2

Private Sub btnFind_Click(sender As System.Object, e As System.EventArgs) Handles btnFind.Click
Dim iOptions As Integer = 0
Dim lStartPos, lEndPos As Long

Dim WKB As JazzWorkbench = Startup.JazzWorkbench
If WKB IsNot Nothing Then
lStartPos = WKB.ProgramArea.SelectionStart + WKB.ProgramArea.SelectionLength
lEndPos = WKB.ProgramArea.TextLength

If WKB.ProgramArea.Find(txtFind.Text, lStartPos, lEndPos, iOptions) = -1 Then
MessageBox.Show("No more occurences of '" & txtFind.Text & "' have been found.", "Find result", MessageBoxButtons.OK, MessageBoxIcon.Information)​
End If​
End If​
End Sub​
End Class

frmFind2 shows the same incorrect behavior as frmFind, requiring the parent form to be clicked to show the highlighting. Otherwise the search behaves perfectly.

I've spend several hours experimenting and reading MSDN, but nothing I've tried helps. I hope somebody can help me.
Thank you, Robert.
 
Eureka, thank you John! This worked perfectly.

As for why the sample code worked without this - I'll leave the investigation of this for a day when I've run out of things to do.

Thanks again, Robert.
 

Latest posts

Back
Top