Stone
Stone

Reputation: 15

List-box Search function

Please forgive me in advance for such messy code, as I'm still learning. However from the below screen shot I've got a search field, list-box and some pseudo data (Not actual data as that is sensitive)

Screen Shot of Controls

The way I handle the search currently is, if the value is an exact match, within a sub string, then the search field transitions from the colour white to the colour green. What I've been trying to do is when this happens the lstBox control will then select the line, which it does, but it will not scroll to the point in which the line is located.

My scruffy code is below:

Sub searchRecord()
    If txtSearch.Text <> "" And txtSearch.Text.Length = 10 Then
        For i As Integer = 0 To lstLine.Items.Count - 1
            If lstLine.Items(i).ToUpper.Substring(0, 10).Contains(txtSearch.Text.ToUpper) Then
                lstLine.SelectedIndex = i
                setSearch("#FF33BD48")
                Exit For
            Else
                setSearch("#d31d1d")
            End If
        Next
    Else
        setSearch("#ffffff")
    End If
End Sub

And please any criticism is welcome either about this post or my code, it's the only way I'm going to learn.

Upvotes: 0

Views: 421

Answers (1)

Gabriel
Gabriel

Reputation: 411

I think you should use BringIntoView() method. In C# I would do it like this:

1) Get ListBoxItem from your SelectedItem

ListBoxItem selectedListBoxItem = lstLine.ItemContainerGenerator.ContainerFromItem(lstLine.SelectedItem) as ListBoxItem;

2) Then bring selected item into view:

selectedListBoxItem.BringIntoView();

Upvotes: 1

Related Questions