Click here to Skip to main content
16,015,900 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
below my code listbox draw item but not worked,please check and correct it.
C#
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            bool selected = ((e.State & DrawItemState.Selected) == DrawItemState.Selected);
            int index = e.Index;
            Graphics g = e.Graphics;
            Color color;
            if (selected == true)
            {
                color = Color.Red;
            }
            else
            {
                color = Color.Pink;
            }
            /* Draw Background */
            Color borderColor = Color.Black;
            g.DrawRectangle(new Pen(borderColor), e.Bounds);
            g.FillRectangle(new SolidBrush(color), e.Bounds);

            /* Draw Item Text */
            g.DrawString(listBox1.Items[e.Index].ToString(), e.Font, new SolidBrush(Color.Black), e.Bounds, StringFormat.GenericDefault);

            e.DrawFocusRectangle();
        }
Posted
Updated 31-Oct-14 2:56am
v2

1 solution

Maybe you didn't set in your listbox the DrawMode property to OwnerDrawFixed or OwnerDrawVariable.
 
Share this answer
 
Comments
cadsolution 1-Nov-14 12:26pm    
thankyou Pikoh

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900