combox 添加图片

分为下面几步:

1.添加一个类;

2.给类添加一个继承ComboBox;

3.从工具箱中拖出一个ComboBox添加的面板,如附件中的图片所示;

3.添加需要的代码。

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

namespace CreateGDB.ThemeMap
{
    // <summary>
    /// 重写了Combobox类,使其在下拉框中显示Symbol颜色带的图片
    /// </summary>
    /// <remarks>
    /// </remarks>

    public partial class PictureComboBox : ComboBox
    {
        private ComboBox comboBox1;

        public PictureComboBox()
        {
            //以下两句是关键的;
            DrawMode = DrawMode.OwnerDrawFixed;
            DropDownStyle. = ComboBoxStyle.DropDownList;
        }

        //重写函数
        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            e.DrawBackground();
            e.DrawFocusRectangle();
            try
            {
                //显示图片
                Image image = (Image)Items[e.Index];
                System.Drawing.Rectangle rect = e.Bounds;
                e.Graphics.DrawImage(image, rect);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                base.OnDrawItem(e);
            }
        }

        private void InitializeComponent()
        {
            this.comboBox1 = new System.Windows.Forms.ComboBox();
            this.SuspendLayout();
            //
            // comboBox1
            //
            this.comboBox1.FormattingEnabled = true;
            this.comboBox1.Location = new System.Drawing.Point(0, 0);
            this.comboBox1.Name = "comboBox1";
            this.comboBox1.Size = new System.Drawing.Size(121, 20);
            this.comboBox1.TabIndex = 0;
            this.ResumeLayout(false);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值