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);
            }
        }

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...
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.