winform控件之ComboBox

ComboBox也算是常用的控件之一了,它提供了一个下拉框供可以让我们自己选择内容,这里我们通过自定义下拉框的形式来学习ComboBox的使用

 

1 界面布局

界面布局如下

winform控件之ComboBox_第1张图片

布局界面很简单,三个label和三个ComboBox即可,另外由于第三个需要一张图片,所以我们这里还需要添加一个ImageList

2 用法示例

如果我们想自定义下来菜单的形式,需要在DrawItem事件中进行重绘,为了能够在DrawItem事件中进行重绘,还需要设置DrawMode属性,该属性定义如下

Normal 0

控件中的所有元素都由操作系统绘制,并且元素大小都相等。

OwnerDrawFixed 1

控件中的所有元素都是手动绘制的,并且元素大小都相等。

OwnerDrawVariable 2

控件中的所有元素都由手动绘制,元素大小可能不相等。

 

2.1数据绑定

再重绘之前,首先我们需要知道数据如何添加到ComboBox里面的,这里大家可以参考

WinForm 中 comboBox控件之数据绑定

这里我分别使用了三种方法,对三个ComboBox的数据进行绑定,代码如下

private void BindCombox1()
        {
            //第一种方法
            //IList list = new List();
            //list.Add("111111");
            //list.Add("222222");
            //list.Add("333333");
            //list.Add("444444");
            //comboBox1.DataSource = list;

            //第二种方法
            IList infoList = new List();
            Info info1 = new Info() { Id = "1", Name = "张三" };
            Info info2 = new Info() { Id = "2", Name = "李四" };
            Info info3 = new Info() { Id = "3", Name = "王五" };
            infoList.Add(info1);
            infoList.Add(info2);
            infoList.Add(info3);
            comboBox1.DataSource = infoList;
            comboBox1.ValueMember = "Id";
            comboBox1.DisplayMember = "Name";
        }

        private void BindCombox2()
        {
            Dictionary kvDictonary = new Dictionary();
            kvDictonary.Add(1, "篮球");
            kvDictonary.Add(2, "足球");
            kvDictonary.Add(3, "排球");

            BindingSource bs = new BindingSource();
            bs.DataSource = kvDictonary;
            comboBox2.DataSource = bs;
            comboBox2.ValueMember = "Key";
            comboBox2.DisplayMember = "Value";        
        }

        private void BindCombox3()
        {
            DataTable dt = new DataTable();
            DataColumn dc1 = new DataColumn("id");
            DataColumn dc2 = new DataColumn("name");
            dt.Columns.Add(dc1);
            dt.Columns.Add(dc2);

            DataRow dr1 = dt.NewRow();
            dr1["id"] = "1";
            dr1["name"] = "金庸";

            DataRow dr2 = dt.NewRow();
            dr2["id"] = "2";
            dr2["name"] = "古龙";

            DataRow dr3 = dt.NewRow();
            dr3["id"] = "3";
            dr3["name"] = "梁羽生";

            dt.Rows.Add(dr1);
            dt.Rows.Add(dr2);
            dt.Rows.Add(dr3);

            comboBox3.DataSource = dt;
            comboBox3.ValueMember = "id";
            comboBox3.DisplayMember = "name";
        }

 

 

2.2代码示例

数据添加好之后我们就可以进行重绘了(不要忘记设置DrawMode属性,这里我设置的是OwnerDrawFixed)

我是参考这两篇文章来进行重绘的

WinForm窗体中ComboBox控件自定义高度和选项文本居中、选项高亮

commonbox之美化界面

我是对上面两篇文章进行了汇总,更方便的演示了重绘过程,其实过程不复杂,如果看过我之前几篇文章的话,这里应该很简单,整体代码如下

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

namespace WindowsFormsApplication1
{
    public partial class FormMain : Form
    {
        public FormMain(ArrayList arrip_list)
        {
            InitializeComponent();

            BindCombox1();
            BindCombox2();
            BindCombox3();

            //MessageBox.Show(comboBox1.GetItemText(comboBox1.Items[comboBox1.SelectedIndex]));
        }

        public class Info
        {
            public string Id { get; set; }
            public string Name { get; set; }

        }

        private void BindCombox1()
        {
            //第一种方法
            //IList list = new List();
            //list.Add("111111");
            //list.Add("222222");
            //list.Add("333333");
            //list.Add("444444");
            //comboBox1.DataSource = list;

            //第二种方法
            IList infoList = new List();
            Info info1 = new Info() { Id = "1", Name = "张三" };
            Info info2 = new Info() { Id = "2", Name = "李四" };
            Info info3 = new Info() { Id = "3", Name = "王五" };
            infoList.Add(info1);
            infoList.Add(info2);
            infoList.Add(info3);
            comboBox1.DataSource = infoList;
            comboBox1.ValueMember = "Id";
            comboBox1.DisplayMember = "Name";
        }

        private void BindCombox2()
        {
            Dictionary kvDictonary = new Dictionary();
            kvDictonary.Add(1, "篮球");
            kvDictonary.Add(2, "足球");
            kvDictonary.Add(3, "排球");

            BindingSource bs = new BindingSource();
            bs.DataSource = kvDictonary;
            comboBox2.DataSource = bs;
            comboBox2.ValueMember = "Key";
            comboBox2.DisplayMember = "Value";        
        }

        private void BindCombox3()
        {
            DataTable dt = new DataTable();
            DataColumn dc1 = new DataColumn("id");
            DataColumn dc2 = new DataColumn("name");
            dt.Columns.Add(dc1);
            dt.Columns.Add(dc2);

            DataRow dr1 = dt.NewRow();
            dr1["id"] = "1";
            dr1["name"] = "金庸";

            DataRow dr2 = dt.NewRow();
            dr2["id"] = "2";
            dr2["name"] = "古龙";

            DataRow dr3 = dt.NewRow();
            dr3["id"] = "3";
            dr3["name"] = "梁羽生";

            dt.Rows.Add(dr1);
            dt.Rows.Add(dr2);
            dt.Rows.Add(dr3);

            comboBox3.DataSource = dt;
            comboBox3.ValueMember = "id";
            comboBox3.DisplayMember = "name";
        }

       

        private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            
            ComboBox cmb = sender as ComboBox;  //当前的ComboBox控件
            SolidBrush myBrush = new SolidBrush(cmb.ForeColor);  //字体颜色
            Font ft = cmb.Font;    //获取在属性中设置的字体
            Font newft = new Font("宋体", 9, FontStyle.Bold);
            
            //选项的文本
            string itemText = cmb.GetItemText(cmb.Items[e.Index]);
            // 计算字符串尺寸(以像素为单位)
            SizeF ss = e.Graphics.MeasureString(itemText, cmb.Font);
 
            // 水平居中
            float left = 0;
            //left = (float)(e.Bounds.Width - ss.Width) / 2;  //如果需要水平居中取消注释
            if (left < 0) left = 0f;
 
            // 垂直居中
            float top = (float)(e.Bounds.Height - ss.Height) / 2;
            if (top <= 0) top = 0f;
 
            // 重新绘制背景
            e.DrawBackground();

            //这里的状态需要使用或以后进行判断
            if (e.State == (e.State | DrawItemState.Selected))
            {
                //如果被选中则垂直居中且水平居中,并且以newft格式显示
                e.Graphics.DrawString(itemText, newft, myBrush, new RectangleF(
                    e.Bounds.X + (float)(e.Bounds.Width - ss.Width) / 2,    //设置X坐标偏移量
                    e.Bounds.Y + top,     //设置Y坐标偏移量
                    e.Bounds.Width, e.Bounds.Height), StringFormat.GenericDefault);
            }
            else
            {
                //如果未被选中则垂直居中,并且以默认格式显示
                e.Graphics.DrawString(itemText, ft, myBrush, new RectangleF(
                    e.Bounds.X + left,    //设置X坐标偏移量
                    e.Bounds.Y + top,     //设置Y坐标偏移量
                    e.Bounds.Width, e.Bounds.Height), StringFormat.GenericDefault);            
            }

            e.DrawFocusRectangle();
             
        }

        private void comboBox2_DrawItem(object sender, DrawItemEventArgs e)
        {
            ComboBox cmb = sender as ComboBox;  //当前的ComboBox控件
            SolidBrush myBrush = new SolidBrush(cmb.ForeColor);  //字体颜色
            Font ft = cmb.Font;    //获取在属性中设置的字体
            Font newft = new Font("宋体", 9, FontStyle.Bold);

            //选项的文本
            string itemtext = cmb.GetItemText(cmb.Items[e.Index]);

            // 重新绘制背景
            e.DrawBackground();

            //如果该项被选中
            if (e.State == (e.State | DrawItemState.Selected))
            {
                //设置字符串前矩形块rd的大小
                Rectangle rd = e.Bounds;

                //矩形框宽度为20
                rd.Width = rd.Left + 20;

                //绘制矩形
                SolidBrush b = new SolidBrush(Color.Red);
                e.Graphics.FillRectangle(b, rd);

                //绘制字符串
                e.Graphics.DrawString(itemtext, newft, myBrush, new RectangleF(
                    e.Bounds.X + 20,    //设置X坐标偏移量
                    e.Bounds.Y,     //设置Y坐标偏移量
                    e.Bounds.Width, e.Bounds.Height), StringFormat.GenericDefault);
            }
            else
            {
                //如果未被选中,按照默认格式绘制即可
                e.Graphics.DrawString(itemtext, ft, myBrush, e.Bounds, StringFormat.GenericDefault);
            }

            e.DrawFocusRectangle();
        }

        private void comboBox3_DrawItem(object sender, DrawItemEventArgs e)
        {
            ComboBox cmb = sender as ComboBox;  //当前的ComboBox控件
            SolidBrush myBrush = new SolidBrush(cmb.ForeColor);  //字体颜色
            Font ft = cmb.Font;    //获取在属性中设置的字体
            Font newft = new Font("宋体", 9, FontStyle.Bold);

            //选项的文本
            string itemtext = cmb.GetItemText(cmb.Items[e.Index]);

            // 重新绘制背景
            e.DrawBackground();


            //如果该项被选中
            if (e.State == (e.State | DrawItemState.Selected))
            {
                //绘制图片
                imageList1.Draw(e.Graphics, e.Bounds.Left, e.Bounds.Top, 0);

                //绘制字符串
                e.Graphics.DrawString(itemtext, newft, myBrush, new RectangleF(
                    e.Bounds.X + imageList1.ImageSize.Width,    //设置X坐标偏移量
                    e.Bounds.Y,     //设置Y坐标偏移量
                    e.Bounds.Width, e.Bounds.Height), StringFormat.GenericDefault);
            }
            else
            {
                //如果未被选中,按照默认格式绘制即可
                e.Graphics.DrawString(itemtext, ft, myBrush, e.Bounds, StringFormat.GenericDefault);
            }
        }
    }
}


 

不妥之处望多多批评指正,谢谢

 

 

 

你可能感兴趣的:(winform,winform,C#)