make substring bold if any listbox item contains it

ridhwaans

Active member
Joined
Jun 1, 2012
Messages
34
Programming Experience
3-5
suppose I've got a listbox with a list of names as items
I do a listbox search to see if any of its items contains a particular substring
If the substring exists in an item, then make only the substring bold, and the rest of the string remains a normal font

i.e. listbox items:
...
Mark Smith
John Doe
Smith John
Bob John
...

this might be possible in a rich textbox, but I wish to do it in a listbox
 
I don't think it could be done as standard, but you could build a custom control inheriting from list box and extend its abilities

Sent from my XT910 using Tapatalk 2
 
Yup you can do it with a bit of tinkering. First you need to set the ListBox.DrawMode property to OwnerDraw at design time. I'd also recommend that you set the ListBox.Font to a fixed width font like Courier New.

Then add the following sub (suitably amended for your purposes) and a trigger
VB.NET:
Private Sub ListBox1_DrawItem(sender As System.Object, e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem

[I]'this routine sets everything from the 3rd character to bold in the selected item only
'you'll have to adapt the  item and substring selection to your own needs
[/I]

       Dim normal As String[I] 'substring to be drawn normally[/I]
        Dim bold As String    [I]'substring to be bolded[/I]
        Dim NewX = e.Bounds[I] 'needed to ensure that the bold section doesn't overwrite the normal section[/I]
        Dim f = New Font(e.Font, FontStyle.Bold)[I] 'sets font to bold[/I]


        Try 
            If e.Index = ListBox1.SelectedIndex Then
                normal = ListBox1.Items(e.Index).ToString
                bold = normal.Substring(2)
                normal = normal.Substring(0, 2)
                NewX.X = e.Bounds.X + ((normal.Length + 3) * e.Font.Unit) [I]'this shifts the drawing point past the first two characters
                                                                                                 'it will need tinkering with for each different font[/I]
                                                                                                [I] 'there's almost certainly a better way to do it but I haven't discovered it yet![/I]

                e.Graphics.DrawString(normal, e.Font, Brushes.Black, e.Bounds, StringFormat.GenericDefault)

                   [I]'bold's not necessarily easily distinguished so you can change text colour as well [/I]                                   
                e.Graphics.DrawString(bold, f, Brushes.Red, NewX, StringFormat.GenericDefault)


            Else[I] 'this provides the default drawing for all other items[/I]
                e.Graphics.DrawString(ListBox1.Items(e.Index).ToString, e.Font, Brushes.Black, e.Bounds, StringFormat.GenericDefault)


            End If
        Catch


        End Try


    End Sub


    Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        ListBox1.Refresh() [I]' this must be done to ensure repaint, add it to a suitable event or at the end of your selection process[/I]
    End Sub
 
Back
Top