highlight a seached text in a rtb while its not in focus

ethicalhacker

Well-known member
Joined
Apr 22, 2007
Messages
142
Location
Delhi,India
Programming Experience
5-10
im making a notepad type of application, im able to search for a text and highlight it, but the problem is that when the find button(its on another small popup form like in notepad) is clicked the term doesnt seem to be highlighted but when i click the form the term shows highlighted.
I want that like in notepad im able to highlight the term in the rich text box without the form being in focus
 
Last edited:
By default most controls don't show their selection while not in focus. That doesn't mean that nothing is selected though, you can still set SelectionStart and SelectionLength properties and the designated text will be selected. If you want it to show the selection when not in focus you need to set the HideSelection property to False.

I don't know if you are already but you should also make sure that you display your Find or Find & Replace dialogue as a modeless dialogue. That means that the dialogue will always be displayed over its caller, just like a modal dialogue, but it will prevent the user accessing the caller, unlike a modal dialogue. A modeless dialogue will also be minimised, restored or closed if its caller is. In .NET terms a modeless dialogue is an owned form. You can create an owned form in a number of ways:
VB.NET:
Dim f As New Form

f.Owner = Me
f.Show
VB.NET:
Dim f As New Form

Me.AddOwnedForm(f)
f.Show
VB.NET:
Dim f As New Form

f.Show(Me)
 
No man even after i set the hide selection to false but it dint work i even tried setting the show selection to true but even that dint work.
Note: Im using a form as the find form and not an inputbox because i need a check box and find next button on it.
And im making an mdi notepad application...
The term found doesnt show as selected while the find form is in focus :(
 
Well, you're obviously doing something wrong because I just tested it and it works exactly as it should. I tested it by displaying a modal and modeless dialogue and it worked properly both times.

Try this:

1. Create a new WinForms project.
2. Add a RichTextBox and a Button to Form1.
3. Set the HideSelection property of the RichTextBox to False.
4. Add a second form.
5. A Button to the second form.
6. Add this code to the first form:
VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim f2 As New Form2

    f2.Show(Me)
End Sub
7. Add this code to the second form:
VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim f1 As Form1 = DirectCast(Me.Owner, Form1)

    f1.RichTextBox1.SelectionStart = 10
    f1.RichTextBox1.SelectionLength = 13
End Sub
8. Run the project.
9. Add at least 23 characters to the RichTextBox.
10. Press the button on the main form.
11. Press the button on the second form.

Voila!
 
no this code doesn't work it gives me the error
VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Error 1 Handles clause requires a WithEvents variable defined in the containing type or one of its base types.
 
nope ive followed them all correctly but have a look at this http://www.vbdotnetforums.com/showthread.php?t=20431
it may be the reason im facing the problem?
Any changes I make at design time just dont show at runtime!
And yes ive saved the project and opened it again!
 
selected text doesnt highlight when not in focus

I have an mdi container form with a child form on it and the child form has a richtextbox on it. I also have another form for Find (searching text and selecting it like in notepad). But when I type in a search keyword and press find nothing appears to be highlighted while the find form is in focus but when I click the main form the text shows the highlighted keyword.
Any idea what is wrong?
The code I used works for an sdi notepad app but not here.
 
You are just plain doing it wrong because I just tested three forms: one an MDI parent, one an MDI child and one a modeless dialogue and it work perfectly. Form1 was the MDI parent, Form2 had a RichTextBox with the HideSelection property set to False, and Form3 had a TextBox and a Button. I added this code to Form1:
VB.NET:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim f2 As New Form2

    f2.MdiParent = Me
    f2.Show()

    Dim f3 As New Form3

    f3.Show(Me)
End Sub

Public Sub Find(ByVal str As String)
    DirectCast(Me.ActiveMdiChild, Form2).Find(str)
End Sub
this to Form2:
VB.NET:
Public Sub Find(ByVal str As String)
    Dim selectionStart As Integer = Me.RichTextBox1.Text.IndexOf(str)

    If selectionStart <> -1 Then
        Me.RichTextBox1.SelectionStart = selectionStart
        Me.RichTextBox1.SelectionLength = str.Length
    End If
End Sub
and this to Form3:
VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    DirectCast(Me.Owner, Form1).Find(Me.TextBox1.Text)
End Sub
I ran the project and added some text to the RTB. I then entered a substring in the TextBox that I knew would be found in the RTB and pressed the Button. Exactly as expected the substring was highlighted in the RTB, even though neither the parent or child forms containing it were active.
 
thanks i tried the code u gave me in the same project and it dint work and when i tried it by creating a new project it worked
So i ended p creating my project all over again form by form and I added the code and it worked!
I think it was because I had signed the other project and forgotten the password so I did cancel on runtime and deleted the pfx file and ran it again and now a temporary.pfx file was created maybe I dint have complete access to the project so the changes werent showing?!

If I sign the project will users have to type the password to run the app I guess not, I think its only for editing the project
And if the project is signed then on installation will it still show the warning 'this app is not trusted'?
 
Back
Top