http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 9624|回复: 0

[C#皮肤] C#皮肤—修正ComBox控件OnDrawItem事件通知

[复制链接]
发表于 2012-12-22 11:19:40 | 显示全部楼层 |阅读模式
            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皮肤-皮肤使用须知


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-4-20 23:21

© 2014-2021

快速回复 返回顶部 返回列表