- 积分
 - 40186
 
- 好友
  
- 记录
  
- 主题
  
- 帖子
  
- 听众
  
- 收听
  
 
 
 
 
 
 | 
 
            C#皮肤—修正ComBox控件OnDrawItem事件通知 
导读部分 
------------------------------------------------------------------------------------------------------------- 
C#皮肤-实现原理系列文章导航  
http://www.sufeinet.com/thread-2-1-1.html 
 
首先感谢大家对C#皮肤的支持,有朋友告诉我在使用过程中ComBox会出现异常,提示类型转换失败,我看了一下之前写的Combox的代码发现,以前使用了一些不好的方法,之前是这样写的OnDrawItem事件 
[code=csharp] protected override void OnDrawItem(DrawItemEventArgs e) 
        { 
            Graphics g = e.Graphics; 
            //绘制区域 
            Rectangle r = e.Bounds; 
 
            Font fn = null; 
            if (e.Index >= 0) 
            { 
                if (e.State == DrawItemState.None) 
                { 
                    //设置字体、字符串格式、对齐方式 
                    fn = e.Font; 
                    string s = (string)this.Items[e.Index]; 
                    StringFormat sf = 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; 
                    StringFormat sf = 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); 
 
                } 
            } 
      }[/code] 
哎呀其实是一个低级的错误,大家看到经色部分的代码也许 就明白了 
[code=csharp]string s = (string)this.Items[e.Index];[/code] 
正确 的写法是这样 
[code=csharp]string s = this.Items[e.Index].ToString ();[/code] 
如果你的是3.5的话也可以使用这个方法来解决另外的一个问题,就是解决不能同进显示Text和Value属性的类, 
[code=csharp]代码  
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> 
        /// <param name="combobox">combobox对象</param> 
        /// <param name="dt_tables">要查询的表</param> 
        /// <param name="Text">要查询的字段</param> 
        /// <param name="Value">要返回的字段</param> 
        /// <returns>int</returns> 
        public static string GetComboBoxValueOrText(CRD.WinUI.Misc.ComboBox combobox, DataTable dt_tables, string Text, string Value) 
        { 
            //从dt_tables中查询出来Text对应是Value值 
            var result = dt_tables.AsEnumerable().Where(fi => fi[Text.ToString().Trim()].ToString().Trim()  
                == combobox.Text.ToString().Trim()).FirstOrDefault(); 
 
            //把Value值返回 
            return result[Value.ToString().Trim()].ToString().Trim(); 
        } 
 
    } 
}[/code] 
更加具体的说明请参考 C#仿QQ皮肤-皮肤使用须知 
 |   
 
 
 
 |