Background
Break News
How to add local font to Tailwind Css and NextJS? - Tutorial Design Pattern? - Blockchain Technology, How to create own Bitcoin virtual currency - Zustand mordern management state - Design Pattern - Flyweight Pattern? - Docker Full training Topic

[Tips] How to display images in ComboBox in C#

Monday, 18 January 2016
|
Read: Completed in minutes

[Tips] How to display images in ComboBox in C#

Welcome Back!, If you had working with combo box in C#, you can easy add items into it by command: cb.Items.add(), but if you want to every item add to combo box control
Today I will show you how to customize combo box Control, so you can add image to combo box in C#.

Here is my example:
Add 6 items into combo box with image on the first item.

How to display images in ComboBox in C#

How to work with image combobox control in CSharp




Oke, now we do it
First add my exe from here to your projects or you can create your form with ImageComboBox source code to your projects, same as below action

Create ImageComboBox

How to display images in ComboBox in C#







/*
 * Develper: Zidane (huuvi168@gmail.com)
 * Last modified: 2015-12-25
 */

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ImageComboBox
{
    public class ImageComboBox : ComboBox
    {
        private ImageList mImageList = null;

        public ImageComboBox()
        {
            this.DrawMode = DrawMode.OwnerDrawFixed;
        }

        public ImageList ImageList
        {
            get { return mImageList; }
            set { mImageList = value; }
        }

        protected override void OnDrawItem(
                     System.Windows.Forms.DrawItemEventArgs e)
        {
            base.OnDrawItem(e);

            e.DrawBackground();
            e.DrawFocusRectangle();

            if (e.Index >= 0)
            {

                if (this.Items[e.Index] is ImageComboItem)
                {
                    if (mImageList != null)
                    {
             ImageComboItem CurrItem = (ImageComboItem)this.Items[e.Index];

                        if (CurrItem.ImageIndex != -1)
                        {
             this.ImageList.Draw(e.Graphics, e.Bounds.Left, e.Bounds.Top, 
                                                          CurrItem.ImageIndex);
             e.Graphics.DrawString(CurrItem.Text, CurrItem.Font,
                   new SolidBrush(CurrItem.ForeColor),
                   e.Bounds.Left + mImageList.ImageSize.Width, e.Bounds.Top);
                        }
                        else
                        {
            e.Graphics.DrawString(CurrItem.Text, CurrItem.Font,
                   new SolidBrush(CurrItem.ForeColor),
                   e.Bounds.Left + mImageList.ImageSize.Width, e.Bounds.Top);
                        }
                    }
                }
                else
                {
            e.Graphics.DrawString(this.Items[e.Index].ToString(), e.Font, 
                   new SolidBrush(e.ForeColor), e.Bounds.Left, e.Bounds.Top);
                }
            }
        }
    }
    
    public class ImageComboItem
    {
        private Color mForeColor = Color.Black;
        private int mImageIndex = -1;
        private object mTag = null;
        private string mText = "";
        private Font mFont;

        public ImageComboItem(string Text, Font Font, Color ForeColor)
        {
            mText = Text;
            mFont = Font;
            mForeColor = ForeColor;
        }

        public ImageComboItem(string Text, Font Font, 
                               Color ForeColor, int ImageIndex)
        {
            mText = Text;
            mFont = Font;
            mForeColor = ForeColor;
            mImageIndex = ImageIndex;
        }

        public Color ForeColor
        {
            get { return mForeColor; }
            set { mForeColor = value; }
        }

        public int ImageIndex
        {
            get { return mImageIndex; }
            set { mImageIndex = value; }
        }

        public object Tag
        {
            get { return mTag; }
            set { mTag = value; }
        }

        public string Text
        {
            get { return mText; }
            set { mText = value; }
        }

        public Font Font
        {
            get { return mFont; }
            set { mFont = value; }
        }

        public override string ToString()
        {
            return mText;
        }
    }
}



And then use add References of this exe to your projects (here my auto Phong Than Projects) :d


How to display images in ComboBox in C#


After you can easy use it same as default combo box in CSharp. Set imagebox control name is cmbPKSkill. View the code


/*
 * Develper: Zidane (huuvi168@gmail.com)
 * Last modified: 2015-12-25
 */
private void demo()
{         
    this.cmbPKSkill.ImageList = this.imageListGS;
    string[] ArrSkillGS = clsCharacter.getSkillGS();
    for (int i = 0; i < ArrSkillGS.Length; i++)
    {
      this.cmbPKSkill.Items.Add(new ImageComboBox.ImageComboItem(ArrSkillGS[i],
          new Font("Verdana", 9), Color.Black, i));
    }
    cmbPKSkill.SelectedIndex = 0;
}



Thank you for reading this post. I hope you found it helpful and easy to follow. If you have any feedback or questions about How to Use Image ComboBox Control with C# , please share them in the comments below. I would love to hear from you and discuss this topic further
✋✋✋✋  Webzone Tech Tips, all things Tech Tips for web development  - I am Zidane, See you next time soon ✋✋✋✋

🙇🏼🙇🏼 We Appreciate Your Comments and Suggestions - Webzone, all things Tech Tips web development
Popular Webzone Tech Tips topic maybe you will be like it - by Webzone Tech Tips - Zidane
As a student, I found Blogspot very useful when I joined in 2014. I have been a developer for years . To give back and share what I learned, I started Webzone, a blog with tech tips. You can also search for tech tips zidane on Google and find my helpful posts. Love you all,

I am glad you visited my blog. I hope you find it useful for learning tech tips and webzone tricks. If you have any technical issues, feel free to browse my posts and see if they can help you solve them. You can also leave a comment or contact me if you need more assistance. Here is my blog address: https://learn-tech-tips.blogspot.com.

My blog where I share my passion for web development, webzone design, and tech tips. You will find tutorials on how to build websites from scratch, using hot trends frameworks like nestjs, nextjs, cakephp, devops, docker, and more. You will also learn how to fix common bugs on development, like a mini stackoverflow. Plus, you will discover how to easily learn programming languages such as PHP (CAKEPHP, LARAVEL), C#, C++, Web(HTML, CSS, javascript), and other useful things like Office (Excel, Photoshop). I hope you enjoy my blog and find it helpful for your projects. :)

Thanks and Best Regards!
Follow me on Tiktok @learntechtips and send me a direct message. I will be happy to chat with you.
Webzone - Zidane (huuvi168@gmail.com)
I'm developer, I like code, I like to learn new technology and want to be friend with people for learn each other
I'm a developer who loves coding, learning new technologies, and making friends with people who share the same passion. I have been a full stack developer since 2015, with more than years of experience in web development.
Copyright @2022(November) Version 1.0.0 - By Webzone, all things Tech Tips for Web Development Zidane
https://learn-tech-tips.blogspot.com