BP Forums
Searching in labels - Printable Version

+- BP Forums (https://bpforums.info)
+-- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=55)
+--- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=56)
+---- Forum: VB.NET (Visual Basic 2010/2008) (https://bpforums.info/forumdisplay.php?fid=8)
+----- Forum: Programming Help (https://bpforums.info/forumdisplay.php?fid=9)
+----- Thread: Searching in labels (/showthread.php?tid=537)



Searching in labels - brco900033 - 05-25-2012

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


Re: Searching in labels - brandonio21 - 05-25-2012

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



Re: Searching in labels - Bradley - 05-25-2012

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


Re: Searching in labels - brco900033 - 06-06-2012

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!