Images In ComboBox in Win Forms
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
//draw back groud of the item
e.DrawBackground();
//Create a Image from a file
Image img = Image.FromFile("C:\\1.jpg");
//Draw the image in combo box using its bound, here size of image is
// 10, 10 you can increase the size if you want
e.Graphics.DrawImage(img, e.Bounds.X, e.Bounds.Y, 35, 35);
//we need to draw the item as string because we made drawmode to ownervariable
e.Graphics.DrawString(comboBox1.Items[e.Index].ToString(), comboBox1.Font,System.Drawing.Brushes.Black,new RectangleF(e.Bounds.X + 15, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));
//draw rectangle over the item selected
e.DrawFocusRectangle();
}
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.DrawMode = DrawMode.OwnerDrawFixed;
comboBox1.Items.Add("rajesh");
comboBox1.Items.Add("raj");
comboBox1.Items.Add("ramesh");
comboBox1.Items.Add("ram");
}
}
}
No comments:
Post a Comment