代码改变世界

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

  苏飞  阅读(4190)  评论(2编辑  收藏  举报

阅读全文 http://www.cckan.net/thread-1817-1-1.html

 

 

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

 

复制代码
代码
   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);

                }
            }
      }
复制代码

 

 

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

 

    string s = (string)this.Items[e.Index];

 

  

 正确 的写法是这样

  string s = this.Items[e.Index].ToString ();

 

 

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

 

复制代码
代码
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();
        }

    }
}
复制代码

 

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

 

(评论功能已被禁用)
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示