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

下拉框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();
        }
    }
}

 

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

上一篇:Win10家庭版升级到企业版


下一篇:c# 爬虫