C#仿QQ皮肤—修正ComBox控件OnDrawItem事件通知

C#仿QQ皮肤-实现原理系列文章导航
http://www.cnblogs.com/sufei/archive/2010/03/10/1682847.html

首先感谢大家对QQ皮肤的支持,有朋友告诉我在使用过程中ComBox会出现异常,提示类型转换失败,我看了一下之前写的Combox的代码发现,以前使用了一些不好的方法,之前是这样写的OnDrawItem事件

代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--> protected override void OnDrawItem(DrawItemEventArgse)
{
Graphicsg
= e.Graphics;
// 绘制区域
Rectangler = e.Bounds;

Fontfn
= null ;
if (e.Index >= 0 )
{
if (e.State == DrawItemState.None)
{
// 设置字体、字符串格式、对齐方式
fn = e.Font;
string s = ( string ) this .Items[e.Index];
StringFormatsf
= new StringFormat();
sf.Alignment
= StringAlignment.Near;
// 根据不同的状态用不同的颜色表示
if (e.State == (DrawItemState.NoAccelerator | DrawItemState.NoFocusRect))
{
e.Graphics.FillRectangle(
new SolidBrush(Color.Red),r);
e.Graphics.DrawString(s,fn,
new SolidBrush(Color.Black),r,sf);
e.DrawFocusRectangle();
}
else
{
e.Graphics.FillRectangle(
new SolidBrush(Color.White),r);
e.Graphics.DrawString(s,fn,
new SolidBrush(Shared.FontColor),r,sf);
e.DrawFocusRectangle();
}
}
else
{
fn
= e.Font;
StringFormatsf
= new StringFormat();
sf.Alignment
= StringAlignment.Near;
string s = ( string ) this .Items[e.Index];
e.Graphics.FillRectangle(
new SolidBrush(Shared.ControlBackColor),r);
e.Graphics.DrawString(s,fn,
new SolidBrush(Shared.FontColor),r,sf);

}
}
}

哎呀其实是一个低级的错误,大家看到经色部分的代码也许 就明白了

<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--> string s = ( string ) this .Items[e.Index];

正确 的写法是这样

<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--> string s = this .Items[e.Index].ToString();

如果你的是3.5的话也可以使用这个方法来解决另外的一个问题,就是解决不能同进显示Text和Value属性的类,

代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--> using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace BaseFunction
{
/// <summary>
/// 处理一些控件的方法
/// </summary>
public class UI_Misc_Helper
{
/// <summary>
/// 返回选中的Comobox中Text对应的value值或者是value对应的Text值
/// </summary>
/// <paramname="combobox"> combobox对象 </param>
/// <paramname="dt_tables"> 要查询的表 </param>
/// <paramname="Text"> 要查询的字段 </param>
/// <paramname="Value"> 要返回的字段 </param>
/// <returns> int </returns>
public static string GetComboBoxValueOrText(CRD.WinUI.Misc.ComboBoxcombobox,DataTabledt_tables, string Text, string Value)
{
// 从dt_tables中查询出来Text对应是Value值
varresult = dt_tables.AsEnumerable().Where(fi => fi[Text.ToString().Trim()].ToString().Trim()
== combobox.Text.ToString().Trim()).FirstOrDefault();

// 把Value值返回
return result[Value.ToString().Trim()].ToString().Trim();
}

}
}

更加具体的说明请参考 C#仿QQ皮肤-皮肤使用须知

如有转载请注明出处谢谢合作!!!

签名:做一番一生引以为豪的事业;找一个一生荣辱与共的妻子;在有生之年报答帮过我的人;并有能力帮助需要帮助的人;

博客园主页欢迎大家过来交流探讨:http://sufei.cnblogs.com/

QQ:361983679 Email:sufei.1013@163.com MSN:sufei.1013@163.com

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值