码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2020-05-24 11:47:10      阅读:85      评论:0      收藏:0      [点我收藏+]

标签:nim   led   control   val   dex   dbr   different   data   focus   

下拉框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的值和显示框内的值不同

标签:nim   led   control   val   dex   dbr   different   data   focus   

原文地址:https://www.cnblogs.com/yanweichen/p/12947888.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!