• 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();
            }
        }
    }
  • 相关阅读:
    R语言编程艺术(2)R中的数据结构
    R语言编程艺术(1)快速入门
    R语言实战(十)处理缺失数据的高级方法
    R语言实战(九)主成分和因子分析
    R语言实战(八)广义线性模型
    R语言实战(七)图形进阶
    R语言实战(六)重抽样与自助法
    R语言实战(五)方差分析与功效分析
    R语言实战(四)回归
    R语言实战(三)基本图形与基本统计分析
  • 原文地址:https://www.cnblogs.com/yanweichen/p/12947888.html
Copyright © 2020-2023  润新知