• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#.NET自定义下拉框实现选中下拉list的值和显示框内的值不同

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

下拉框list的值为:

key1-value1

key2-value2

key3-value3

选中后显示:

value1

value2

value3

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            InitializeComboBox();
        }

        internal System.Windows.Forms.ComboBox ComboBox1;
        private string[] dropDawnList;


        private void InitializeComboBox()
        {
            this.ComboBox1 = new ComboBox();
            this.ComboBox1.DrawMode =
                System.Windows.Forms.DrawMode.OwnerDrawVariable;
            this.ComboBox1.Location = new System.Drawing.Point(10, 20);
            this.ComboBox1.Name = "ComboBox1";
            this.ComboBox1.Size = new System.Drawing.Size(250, 120);
            this.ComboBox1.DropDownWidth = 250;
            this.ComboBox1.TabIndex = 0;
            this.ComboBox1.DropDownStyle = ComboBoxStyle.DropDown;
            dropDawnList = new string[] { "key1-value1", "key2-value2", "key3-value3" };
            foreach (string ddl in dropDawnList) 
            {
                ComboBox1.Items.Add(ddl.Split('-')[1]);
            }
            this.Controls.Add(this.ComboBox1);

            // Hook up the MeasureItem and DrawItem events
            this.ComboBox1.DrawItem +=
                new DrawItemEventHandler(ComboBox1_DrawItem);
          
        }

       
      
        private void ComboBox1_DrawItem(object sender,
            System.Windows.Forms.DrawItemEventArgs e)
        {

            float size = 0;
            System.Drawing.Font myFont;
            FontFamily family = null;

            System.Drawing.Color animalColor = new System.Drawing.Color();
            size = 10;
            animalColor = System.Drawing.Color.Gray;
            family = FontFamily.GenericSansSerif;
          

            // Draw the background of the item.
            e.DrawBackground();

            // Create a square filled with the animals color. Vary the size
            // of the rectangle based on the length of the animals name.
            Rectangle rectangle = new Rectangle(2, e.Bounds.Top + 2,
                    e.Bounds.Height, e.Bounds.Height - 4);
            e.Graphics.FillRectangle(new SolidBrush(animalColor), rectangle);

            // Draw each string in the array, using a different size, color,
            // and font for each item.
            myFont = new Font(family, size, FontStyle.Bold);
            e.Graphics.DrawString(dropDawnList[e.Index], myFont, System.Drawing.Brushes.Black, new RectangleF(e.Bounds.X + rectangle.Width, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));
            
            // Draw the focus rectangle if the mouse hovers over an item.
            e.DrawFocusRectangle();
        }
    }
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
支付宝C#接口发布时间:2022-07-13
下一篇:
C#调用C++DLL传递结构体数组的终极解决方案发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap