zoukankan      html  css  js  c++  java
  • 定制C# combobox的下拉框

    今天做了个小东西,需要定制combobox的下拉框,打开下拉框时如下图。


    选择一个后,如下图。

    实现的方法是需要把combobox的DrawoMode设置成OwnerDrawVariable,然后处理DrawItem事件,详见ComboBox.DrawItem Event (System.Windows.Forms)代码如下:

        1         private void cb_Risk_DrawItem(object sender, DrawItemEventArgs e)

        2         {

        3             if (e.Index < 0) return;

        4 

        5             switch (e.Index)

        6             {

        7                 case 0:

        8                     e.Graphics.FillRectangle(Brushes.Red, e.Bounds);

        9                     break;

       10                 case 1:

       11                     e.Graphics.FillRectangle(Brushes.Yellow, e.Bounds);

       12                     break;

       13                 case 2:

       14                     e.Graphics.FillRectangle(Brushes.Blue, e.Bounds);

       15                     break;

       16                 default:

       17                     break;

       18             }

       19             e.Graphics.DrawString(cb_Risk.Items[e.Index].ToString(), cb_Risk.Font, Brushes.Black, (RectangleF)e.Bounds);

       20         }


    对了,我这里的代码都是用CopySourceAsHtml这个VS的addin粘进来的,对于VS2010,这篇文章CopyAsHtml in Visual Studio 2010 - AppliSec有一个workaround。



  • 相关阅读:
    暗影精灵3安装无线网卡驱动(ubuntu16.04)
    装饰器之基本
    pyhton代码规范
    2.线程
    文件拾遗
    闭包函数
    6.文件基本操作
    1.socket网络编程
    9.异常处理
    Python语言规范
  • 原文地址:https://www.cnblogs.com/fresky/p/1872166.html
Copyright © 2011-2022 走看看