解决方案 »
- Mysql+.NET C# 插入和修改报错,异常详细信息: System.Data.Odbc.OdbcException:
- 泛型的向上转型出错.net3.5
- 如何格式化字符串
- 一个不算复杂的问题 可是我调试了许久都不知道哪里错了 C#操作ACCESS的
- ######################小问题!帮忙看一下!!!!!谢谢啦!!在线等!!
- 百分求教--用正则表达式进行文字列替换的问题.
- 使用OracleDataReader得到多条记录,怎么才能逐条访问
- 使用WF流程设计器时,如何自定义FlowSwitch(即修改外观大小,图片,文字等)
- 同志们:一个public类型的成员变量和属性有什么区别啊?
- 怎么才能在拔掉网线的瞬间就知道网络断了
- 鼠标中键滚轮改变图像大小
- 请教各位大虾类似课程表这样的二维表应该怎么在c#winform中设计啊
类名字:ComboBoxEx.cs代码如下:using System;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Drawing;namespace Mengxianhui.ComboBoxEx
{
class ComboBoxEx : ComboBox
{
private ImageList imageList;
public ImageList ImageList
{
get {return imageList;}
set {imageList = value;}
} public ComboBoxEx()
{
DrawMode = DrawMode.OwnerDrawFixed;
} protected override void OnDrawItem(DrawItemEventArgs ea)
{
ea.DrawBackground();
ea.DrawFocusRectangle(); ComboBoxExItem item;
Size imageSize = imageList.ImageSize;
Rectangle bounds = ea.Bounds; try
{
item = (ComboBoxExItem)Items[ea.Index]; if (item.ImageIndex != -1)
{
imageList.Draw(ea.Graphics, bounds.Left, bounds.Top,
item.ImageIndex);
ea.Graphics.DrawString(item.Text, ea.Font, new
SolidBrush(ea.ForeColor), bounds.Left+imageSize.Width, bounds.Top);
}
else
{
ea.Graphics.DrawString(item.Text, ea.Font, new
SolidBrush(ea.ForeColor), bounds.Left, bounds.Top);
}
}
catch
{
if (ea.Index != -1)
{
ea.Graphics.DrawString(Items[ea.Index].ToString(), ea.Font, new
SolidBrush(ea.ForeColor), bounds.Left, bounds.Top);
}
else
{
ea.Graphics.DrawString(Text, ea.Font, new
SolidBrush(ea.ForeColor), bounds.Left, bounds.Top);
}
} base.OnDrawItem(ea);
}
} class ComboBoxExItem
{
private string _text;
public string Text
{
get {return _text;}
set {_text = value;}
} private int _imageIndex;
public int ImageIndex
{
get {return _imageIndex;}
set {_imageIndex = value;}
} public ComboBoxExItem()
: this("")
{
} public ComboBoxExItem(string text)
: this(text, -1)
{
} public ComboBoxExItem(string text, int imageIndex)
{
_text = text;
_imageIndex = imageIndex;
} public override string ToString()
{
return _text;
}
}}
使用方法using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace Mengxianhui.ComboBoxEx
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private ComboBoxEx comboBox1;
private System.ComponentModel.IContainer components; public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent(); //
// TODO: Add any constructor code after InitializeComponent call
//
} /// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
ComboBoxEx comboBox1 = new ComboBoxEx();
ImageList imageList1 = new ImageList();
imageList1.Images.Add(Image.FromFile(@"D:\favicon.ico"));
imageList1.Images.Add(Image.FromFile(@"D:\favicon4.ico"));
imageList1.Images.Add(Image.FromFile(@"D:\favicon5.ico"));
comboBox1.ImageList = imageList1; comboBox1.Width = 200; comboBox1.Items.Add(new ComboBoxExItem("【孟宪会之精彩世界】", 0));
comboBox1.Items.Add(new ComboBoxExItem("【孟宪会之精彩世界】", 1));
comboBox1.Items.Add(new ComboBoxExItem("【孟宪会之精彩世界】", 2));
this.Controls.AddRange(new System.Windows.Forms.Control[] {comboBox1}); //
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {comboBox1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion /// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
代码已经打包了,你可以到这个地址下下来,可以直接使用,不需要注册直接下载即可。
http://kkxxdown.com/rGPPFSAK