Click here to Skip to main content
15,885,278 members
Articles / Multimedia / GDI+

Image ComboBox Control

Rate me:
Please Sign up or sign in to vote.
4.84/5 (40 votes)
7 Jul 2005CDDL4 min read 365.4K   17.5K   117  
This is an extended, owner drawn ComboBox which has an added support to display images in the combobox dropdown as well as the edit (text) box.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Test
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.ImageList imageList1;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.RadioButton simpleRadioButton;
		private System.Windows.Forms.RadioButton dropDownRadioButton;
		private System.Windows.Forms.RadioButton DropDownListRadioButton;
		private System.Windows.Forms.CheckBox checkBox1;
		private ImageComboBox.ImageComboBox imageComboBox1;
		private System.ComponentModel.IContainer components;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
			ImageComboBox.ImageComboBoxItem imageComboBoxItem1 = new ImageComboBox.ImageComboBoxItem();
			ImageComboBox.ImageComboBoxItem imageComboBoxItem2 = new ImageComboBox.ImageComboBoxItem();
			ImageComboBox.ImageComboBoxItem imageComboBoxItem3 = new ImageComboBox.ImageComboBoxItem();
			ImageComboBox.ImageComboBoxItem imageComboBoxItem4 = new ImageComboBox.ImageComboBoxItem();
			ImageComboBox.ImageComboBoxItem imageComboBoxItem5 = new ImageComboBox.ImageComboBoxItem();
			ImageComboBox.ImageComboBoxItem imageComboBoxItem6 = new ImageComboBox.ImageComboBoxItem();
			ImageComboBox.ImageComboBoxItem imageComboBoxItem7 = new ImageComboBox.ImageComboBoxItem();
			ImageComboBox.ImageComboBoxItem imageComboBoxItem8 = new ImageComboBox.ImageComboBoxItem();
			ImageComboBox.ImageComboBoxItem imageComboBoxItem9 = new ImageComboBox.ImageComboBoxItem();
			ImageComboBox.ImageComboBoxItem imageComboBoxItem10 = new ImageComboBox.ImageComboBoxItem();
			ImageComboBox.ImageComboBoxItem imageComboBoxItem11 = new ImageComboBox.ImageComboBoxItem();
			ImageComboBox.ImageComboBoxItem imageComboBoxItem12 = new ImageComboBox.ImageComboBoxItem();
			ImageComboBox.ImageComboBoxItem imageComboBoxItem13 = new ImageComboBox.ImageComboBoxItem();
			this.imageList1 = new System.Windows.Forms.ImageList(this.components);
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.DropDownListRadioButton = new System.Windows.Forms.RadioButton();
			this.dropDownRadioButton = new System.Windows.Forms.RadioButton();
			this.simpleRadioButton = new System.Windows.Forms.RadioButton();
			this.checkBox1 = new System.Windows.Forms.CheckBox();
			this.imageComboBox1 = new ImageComboBox.ImageComboBox();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// imageList1
			// 
			this.imageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth16Bit;
			this.imageList1.ImageSize = new System.Drawing.Size(48, 48);
			this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
			this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.DropDownListRadioButton);
			this.groupBox1.Controls.Add(this.dropDownRadioButton);
			this.groupBox1.Controls.Add(this.simpleRadioButton);
			this.groupBox1.Location = new System.Drawing.Point(264, 48);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(120, 112);
			this.groupBox1.TabIndex = 1;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Styles";
			// 
			// DropDownListRadioButton
			// 
			this.DropDownListRadioButton.Location = new System.Drawing.Point(8, 72);
			this.DropDownListRadioButton.Name = "DropDownListRadioButton";
			this.DropDownListRadioButton.TabIndex = 2;
			this.DropDownListRadioButton.Text = "DropDownList";
			this.DropDownListRadioButton.CheckedChanged += new System.EventHandler(this.DropDownListRadioButton_CheckedChanged);
			// 
			// dropDownRadioButton
			// 
			this.dropDownRadioButton.Location = new System.Drawing.Point(8, 48);
			this.dropDownRadioButton.Name = "dropDownRadioButton";
			this.dropDownRadioButton.TabIndex = 1;
			this.dropDownRadioButton.Text = "DropDown";
			this.dropDownRadioButton.CheckedChanged += new System.EventHandler(this.dropDownRadioButton_CheckedChanged);
			// 
			// simpleRadioButton
			// 
			this.simpleRadioButton.Checked = true;
			this.simpleRadioButton.Location = new System.Drawing.Point(8, 24);
			this.simpleRadioButton.Name = "simpleRadioButton";
			this.simpleRadioButton.TabIndex = 0;
			this.simpleRadioButton.TabStop = true;
			this.simpleRadioButton.Text = "Simple";
			this.simpleRadioButton.CheckedChanged += new System.EventHandler(this.simpleRadioButton_CheckedChanged);
			// 
			// checkBox1
			// 
			this.checkBox1.Location = new System.Drawing.Point(264, 200);
			this.checkBox1.Name = "checkBox1";
			this.checkBox1.TabIndex = 2;
			this.checkBox1.Text = "Toggle Margin";
			this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);
			// 
			// imageComboBox1
			// 
			this.imageComboBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;
			this.imageComboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.Simple;
			this.imageComboBox1.ImageList = this.imageList1;
			this.imageComboBox1.Indent = 20;
			this.imageComboBox1.ItemHeight = 15;
			imageComboBoxItem1.Font = new System.Drawing.Font("Monotype Corsiva", 14.25F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic))), System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			imageComboBoxItem1.Image = "1";
			imageComboBoxItem1.ImageIndex = 1;
			imageComboBoxItem1.IndentLevel = 0;
			imageComboBoxItem1.Item = null;
			imageComboBoxItem1.Text = "Desktop";
			imageComboBoxItem2.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			imageComboBoxItem2.Image = "5";
			imageComboBoxItem2.ImageIndex = 5;
			imageComboBoxItem2.IndentLevel = 1;
			imageComboBoxItem2.Item = null;
			imageComboBoxItem2.Text = "My Computer";
			imageComboBoxItem3.Font = new System.Drawing.Font("Lucida Sans", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			imageComboBoxItem3.Image = "9";
			imageComboBoxItem3.ImageIndex = 9;
			imageComboBoxItem3.IndentLevel = 1;
			imageComboBoxItem3.Item = null;
			imageComboBoxItem3.Text = "My Documents";
			imageComboBoxItem4.Font = new System.Drawing.Font("Arial", 11.25F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			imageComboBoxItem4.Image = "2";
			imageComboBoxItem4.ImageIndex = 2;
			imageComboBoxItem4.IndentLevel = 2;
			imageComboBoxItem4.Item = null;
			imageComboBoxItem4.Text = "Floppy (A)";
			imageComboBoxItem5.Font = new System.Drawing.Font("Bookman Old Style", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			imageComboBoxItem5.Image = "4";
			imageComboBoxItem5.ImageIndex = 4;
			imageComboBoxItem5.IndentLevel = 2;
			imageComboBoxItem5.Item = null;
			imageComboBoxItem5.Text = "Local Disk (C)";
			imageComboBoxItem6.Font = new System.Drawing.Font("French Script MT", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			imageComboBoxItem6.Image = "3";
			imageComboBoxItem6.ImageIndex = 3;
			imageComboBoxItem6.IndentLevel = 3;
			imageComboBoxItem6.Item = null;
			imageComboBoxItem6.Text = "Folder One";
			imageComboBoxItem7.Font = new System.Drawing.Font("Impact", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			imageComboBoxItem7.Image = "3";
			imageComboBoxItem7.ImageIndex = 3;
			imageComboBoxItem7.IndentLevel = 3;
			imageComboBoxItem7.Item = null;
			imageComboBoxItem7.Text = "Folder Two";
			imageComboBoxItem8.Font = new System.Drawing.Font("Times New Roman", 15.75F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic))), System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			imageComboBoxItem8.Image = "4";
			imageComboBoxItem8.ImageIndex = 4;
			imageComboBoxItem8.IndentLevel = 2;
			imageComboBoxItem8.Item = null;
			imageComboBoxItem8.Text = "Local Disk (D)";
			imageComboBoxItem9.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			imageComboBoxItem9.Image = "0";
			imageComboBoxItem9.ImageIndex = 0;
			imageComboBoxItem9.IndentLevel = 2;
			imageComboBoxItem9.Item = null;
			imageComboBoxItem9.Text = "CD Drive (E)";
			imageComboBoxItem10.Font = new System.Drawing.Font("Monotype Corsiva", 9.75F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			imageComboBoxItem10.Image = "0";
			imageComboBoxItem10.ImageIndex = 0;
			imageComboBoxItem10.IndentLevel = 2;
			imageComboBoxItem10.Item = null;
			imageComboBoxItem10.Text = "DVD Drive (F)";
			imageComboBoxItem11.Font = new System.Drawing.Font("Lucida Handwriting", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			imageComboBoxItem11.Image = "6";
			imageComboBoxItem11.ImageIndex = 6;
			imageComboBoxItem11.IndentLevel = 1;
			imageComboBoxItem11.Item = null;
			imageComboBoxItem11.Text = "My Network";
			imageComboBoxItem12.Font = new System.Drawing.Font("Lucida Sans", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			imageComboBoxItem12.Image = "7";
			imageComboBoxItem12.ImageIndex = 7;
			imageComboBoxItem12.IndentLevel = 1;
			imageComboBoxItem12.Item = null;
			imageComboBoxItem12.Text = "Recycle Bin";
			imageComboBoxItem13.Font = new System.Drawing.Font("Georgia", 20.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			imageComboBoxItem13.Image = "8";
			imageComboBoxItem13.ImageIndex = 8;
			imageComboBoxItem13.IndentLevel = 1;
			imageComboBoxItem13.Item = null;
			imageComboBoxItem13.Text = "Control Panel";
			this.imageComboBox1.Items.AddRange(new ImageComboBox.ImageComboBoxItem[] {
																						 imageComboBoxItem1,
																						 imageComboBoxItem2,
																						 imageComboBoxItem3,
																						 imageComboBoxItem4,
																						 imageComboBoxItem5,
																						 imageComboBoxItem6,
																						 imageComboBoxItem7,
																						 imageComboBoxItem8,
																						 imageComboBoxItem9,
																						 imageComboBoxItem10,
																						 imageComboBoxItem11,
																						 imageComboBoxItem12,
																						 imageComboBoxItem13});
			this.imageComboBox1.Location = new System.Drawing.Point(16, 48);
			this.imageComboBox1.Name = "imageComboBox1";
			this.imageComboBox1.Size = new System.Drawing.Size(224, 208);
			this.imageComboBox1.TabIndex = 3;
			this.imageComboBox1.Text = "imageComboBox1";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(408, 294);
			this.Controls.Add(this.imageComboBox1);
			this.Controls.Add(this.checkBox1);
			this.Controls.Add(this.groupBox1);
			this.Name = "Form1";
			this.Text = "ImageComboBox Demo";
			this.Load += new System.EventHandler(this.Form1_Load);
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void simpleRadioButton_CheckedChanged(object sender, System.EventArgs e)
		{
			if(simpleRadioButton.Checked)
				imageComboBox1.DropDownStyle = ComboBoxStyle.Simple;
		}

		private void dropDownRadioButton_CheckedChanged(object sender, System.EventArgs e)
		{
			if(dropDownRadioButton.Checked)
				imageComboBox1.DropDownStyle = ComboBoxStyle.DropDown;
		}

		private void DropDownListRadioButton_CheckedChanged(object sender, System.EventArgs e)
		{
			if(DropDownListRadioButton.Checked)
				imageComboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{
		
		}

		private void checkBox1_CheckedChanged(object sender, System.EventArgs e)
		{
			if(imageComboBox1.RightToLeft == RightToLeft.Yes)
				imageComboBox1.RightToLeft = RightToLeft.No;
			else
				if(imageComboBox1.RightToLeft == RightToLeft.No)
				imageComboBox1.RightToLeft = RightToLeft.Yes;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Common Development and Distribution License (CDDL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions