0

I am developing software in which I needed to do more elaborate visual styles to a listbox than usual and I used some sample code online. As a good learning programmer should I tried to understand the code when I edited it and for the most part I did, but the index property was returning -1 when I deleted an Item. On MSDN it says the Index property may return -1 when deleting an item, yet how can I stop it?

Line 12 is the troubled one
"Browsers" is the name of my listbox

Code:

private void Browsers_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
        {
            times++;
            // Get the Bounding rectangle
            Rectangle rc = new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height);

            // Setup the stringformatting object
            StringFormat sf = new StringFormat();
            sf.Alignment = StringAlignment.Near;

            // Get the item text
            String str = (String)Browsers.Items[e.Index];

            // Draw the rectangle
            e.Graphics.FillRectangle(new SolidBrush(Color.LightSlateGray), rc);

            // Check if the item is selected
            if (e.State == (DrawItemState.NoAccelerator | DrawItemState.NoFocusRect))
            {
                // Paint the item Accordingly if not selected
                if (e.Index % 2 == 0)
                {
                    Color col = ColorTranslator.FromHtml("#b5bbc4");
                    e.Graphics.FillRectangle(new SolidBrush(col), rc);
                }
                else
                {
                    Color col = ColorTranslator.FromHtml("#c9cfd6");
                    e.Graphics.FillRectangle(new SolidBrush(col), rc);
                }
                e.Graphics.DrawString(str, new Font("Arial", 9), new SolidBrush(Color.Black), rc, sf);
                e.DrawFocusRectangle();
            }
            else
            {
                // Paint the item accordingly if it is selected
                e.DrawFocusRectangle();
                if (e.Index % 2 == 0)
                {
                    Color col = ColorTranslator.FromHtml("#61c9e8");
                    e.Graphics.FillRectangle(new SolidBrush(col), rc);
                }
                else
                {
                    Color col = ColorTranslator.FromHtml("#5dcef0");
                    e.Graphics.FillRectangle(new SolidBrush(col), rc);
                }
                e.Graphics.DrawString(str, new Font("Arial", 9), new SolidBrush(Color.White), rc, sf);
            }
        }

Edited by jackabascal: n/a

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Geekitygeek
0

Have you tried checking the index before attempting to access it? In fact, if index is -1 then you can skip the whole method since you are trying to draw an item that isnt in the list so:

private void Browsers_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
    if(e.Index<0)
        return;
    
    //rest of method runs here if index is not out of bounds...
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.