开发者

C# combobox color [duplicate]

开发者 https://www.devze.com 2023-02-14 22:08 出处:网络
This question already has answers here: 开发者_运维技巧 Closed 11 years ago. Possible Duplicate: Colour Individual Items in a winforms ComboBox?
This question already has answers here: 开发者_运维技巧 Closed 11 years ago.

Possible Duplicate:

Colour Individual Items in a winforms ComboBox?

is any simple example to set custom background color of each item?


You can achieve your objective by calling DrawItem of the ComboBox, For reference see the following method.

private void comboBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
    {
        // Override this function to draw items in the Color comboBox

        // Get the Graphics Object (aka. CDC or Device Context Object )
        // passed via the DrawItemEventArgs parameter
        Graphics g = e.Graphics ;

        // Get the bounding rectangle of the item currently being painted
        Rectangle r = e.Bounds ;

        if ( e.Index >= 0 ) 
        {
            Rectangle rd = r; 
            r.X = r.Right ; 

            // Get the brush object, at the specifid index in the colorArray
            SolidBrush b = (SolidBrush)colorArray[e.Index];
            // Fill a portion of the rectangle with the selected brush
            g.FillRectangle(b, rd);


            // Draw the rectangle
            e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.Black), 2 ), r );

            if ( e.State == ( DrawItemState.NoAccelerator | DrawItemState.NoFocusRect))
            {
                // if the item is not selected draw it with a different color
                e.Graphics.FillRectangle(new SolidBrush(Color.White) , r);
                e.DrawFocusRectangle();
            }
            else
            {
                // if the item is selected draw it with a different color
                e.Graphics.FillRectangle(new SolidBrush(Color.LightBlue) , r);
                e.DrawFocusRectangle();
            }
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号