zoukankan      html  css  js  c++  java
  • C#仿QQ皮肤—修正ComBox控件OnDrawItem事件通知

    阅读全文 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皮肤-皮肤使用须知

  • 相关阅读:
    新單詞
    custom preview link
    注冊碼
    准备用VB.Net 写一个律师管理的系统
    Windows服務
    下一步
    失败
    如何在篩選聯絡人時控制只能篩選上層客戶的聯絡人.
    如何取Lookup欄位的值
    Dynamic Picklist Sample
  • 原文地址:https://www.cnblogs.com/sufei/p/1731178.html
Copyright © 2011-2022 走看看