Gaddigesh
Gaddigesh

Reputation: 1953

List Box Update Issue

I have a ListBox of constant size 4

I can Add n number of ListBoxItems,Once size exceeds 4 I have enabled scroll bar,

Problem:when scroll is enabled(more than 4 items), whenever i delete last item, there is a white patch in place of deleted Item.

Patch goes off only when I touch the scroll bar.

I tried ListBox.Invalidate(), But no use

Upvotes: 1

Views: 202

Answers (2)

Henk Holterman
Henk Holterman

Reputation: 273179

Additional: This only happens when the last element is selected when it is deleted.

Solution: Explicitly set the new selection, and for the last element make the list scroll first:

        int selected = listBox1.SelectedIndex;        
        if (selected >= 0)
        {
            listBox1.Items.RemoveAt(selected);
            if (selected == listBox1.Items.Count)
                listBox1.SelectedIndex = 0;

            listBox1.SelectedIndex = selected - 1;
        }

Upvotes: 2

Neil Barnwell
Neil Barnwell

Reputation: 42105

What is your "delete" code to remove from the listbox? If you are using something like this:

listBox.Items[3] = null;

...then there are still 4 items in the listbox, just that the 4th one is null. You actually need to remove the item:

listBox.Items.Remove(3);

Upvotes: 0

Related Questions