Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Searching in labels
#1
Hi all

I have a question about a code on how to search in labels in my windows form and highlights the text then. It has to search in one windows form and only Labels not Textboxes, Richtextboxes...
I don't know if this possible but I hope so.

Thx
#2
Well as far as I know, it is not possible to highlight only a section of a label. MarMan from VBForums suggested that
Quote:You can use a textbox with no border, backcolor the same as the surroundings and readonly to true
My Blog | My Setup | My Videos | Have a wonderful day.
#3
I agree here is how to do it in a richtext box if you do decide to go ahead and do so

add 1 textbox
add 1 Button
add 1 Richtextbox

Code

Code:
Public Class Form1
    Sub findTextAndHighlight(ByVal searchtext As String, ByVal rtb As RichTextBox)
        Dim textEnd As Integer = rtb.TextLength
        Dim index As Integer = 0
        Dim fnt As Font = New Font(rtb.Font, FontStyle.Bold)
        Dim lastIndex As Integer = rtb.Text.LastIndexOf(searchtext)
        While (index < lastIndex)
            rtb.Find(searchtext, index, textEnd,
            RichTextBoxFinds.WholeWord)
            rtb.SelectionFont = fnt
            rtb.SelectionLength = searchtext.Length
            rtb.SelectionColor = Color.Black
            rtb.SelectionBackColor = Color.Yellow
            index = rtb.Text.IndexOf(searchtext, index) + 1
        End While
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        findTextAndHighlight(TextBox1.Text, RichTextBox1)
    End Sub
End Class

I could not find a way either hope this helps in some way

Kind Regards

Bradley
#4
Sorry that I'm so late... Thanks for the help, I will adjust the option in my program a little bit so I can use your code!

Thanks!


Forum Jump:


Users browsing this thread: 1 Guest(s)