views:

54

answers:

2

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

A: 

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);
Neil Barnwell
I am doing listBox.Items.Remove(3) only, Anyhow above answer solved my problem
Gaddigesh
+2  A: 

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;
        }
Henk Holterman
I am setting new selection(Even afeter Problem persists),I did'not understand the statement "for the last element make the list scroll first" .Can you Please elaborate
Gaddigesh
It's this statement: `listBox1.SelectedIndex = 0;`
Henk Holterman
Thanks a lot, That helped
Gaddigesh