[C #] Simple Self-painted ComboBox with images

Source: Internet
Author: User

First look at the effect:

 

 

 


 

First construct a subitem class:

 

[Csharp]
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Drawing ;//
 
Namespace ComboBox_Draw
{
// Customize the combo box item
Class MyItem
{
// Item text content
Private String Text;

// Item Image
Public Image Img;
 
// Constructor
Public MyItem (String text, Image img)
{
Text = text;
Img = img;
}
 
// Rewrite the ToString function to return the item text
Public override string ToString ()
{
Return Text;
} Www.2cto.com
}
}
 

Then let's see the rewrite DrawItem event:

 

[Csharp]
Private void comboboxincludrawitem (object sender, DrawItemEventArgs e)
{
// Select this item with the mouse
If (e. State & DrawItemState. Selected )! = 0)
{
// Gradient painter
LinearGradientBrush brush = new LinearGradientBrush (e. Bounds, Color. FromArgb (255,251,237 ),
Color. FromArgb (255,236,181), LinearGradientMode. Vertical );
// Fill area
Rectangle borderRect = new Rectangle (3, e. Bounds. Y, e. Bounds. Width-5, e. Bounds. Height-2 );
 
E. Graphics. FillRectangle (brush, borderRect );
 
// Draw a border
Pen pen = new Pen (Color. FromArgb (229,195,101 ));
E. Graphics. DrawRectangle (pen, borderRect );
}
Else
{
SolidBrush brush = new SolidBrush (Color. FromArgb (255,255,255 ));
E. Graphics. FillRectangle (brush, e. Bounds );
}
 
// Obtain the item image and draw the image
MyItem item = (MyItem) comboBox1.Items [e. Index];
Image img = item. Img;

// The area of the image.
Rectangle imgRect = new Rectangle (6, e. Bounds. Y + 3, 45, 45 );
E. Graphics. DrawImage (img, imgRect );

// Text content display area
Rectangle textRect =
New Rectangle (imgRect. Right + 2, imgRect. Y, e. Bounds. Width-imgRect. Width, e. Bounds. Height-2 );

// Obtain the item text and draw the text
String itemText = comboBox1.Items [e. Index]. ToString ();
 
// Vertical center of text format
StringFormat strFormat = new StringFormat ();
StrFormat. LineAlignment = StringAlignment. Center;
E. Graphics. DrawString (itemText, new Font ("", 12), Brushes. Black, textRect, strFormat );
}
 


The last is the form Load event: the last is the form Load event:

 

[Csharp]
Private void Form1_Load (object sender, EventArgs e)
{
// Add an item
ComboBox1.Items. Add (new MyItem ("000000", Image. FromFile (Application. StartupPath + "\ 0.gif ")));
ComboBox1.Items. Add (new MyItem ("111111", Image. FromFile (Application. StartupPath + "\ 1.gif ")));
ComboBox1.Items. Add (new MyItem ("222222", Image. FromFile (Application. StartupPath + "\ 2.gif ")));
ComboBox1.Items. Add (new MyItem ("333333", Image. FromFile (Application. StartupPath + "\ 3.gif ")));
 
// Index selected by default
ComboBox1.SelectedIndex = 0;
 
// Attributes to be set for the Self-painted combo box
ComboBox1.DrawMode = DrawMode. OwnerDrawFixed;
ComboBox1.DropDownStyle = ComboBoxStyle. DropDownList;
ComboBox1.ItemHeight = 50;
ComboBox1.Width = 200;
 
// Add the DrawItem event processing function
ComboBox1.DrawItem + = combobox#drawitem;
 
}
Author: isaced

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.