日期:2014-05-18  浏览次数:21186 次

C# ComboBoxToolTip 控制的问题 在DropDownStyle 为 DropDown时没问题 设置为DropDownList就运行不了
附上源码:
C# code


public class ComboBoxToolTip : ComboBox
    {
        private ToolTip itemToolTip;
        /// <summary>
        /// ComboBox 控件选择项带ToolTip提示的
        /// 当 选择项 长度大于本身 ComboBox 控件时 生成ToolTip提示
        /// </summary>
        public ComboBoxToolTip()
        {
            this.DrawMode = DrawMode.OwnerDrawFixed;
            this.DrawItem += new DrawItemEventHandler(DrawItemInList);
            this.DropDownClosed += new EventHandler(DropDownListIsClosed);
            this.itemToolTip = new ToolTip();
        }

        void DropDownListIsClosed(object sender, EventArgs e)
        {
            this.itemToolTip.Hide(this);
        }

        private bool IsTruncated(string text, Graphics graphics, Font font, Rectangle destinationBounds)
        {
            SizeF size = graphics.MeasureString(text, font);
            Rectangle offsetToZeroRect = new Rectangle(0, 0, destinationBounds.Width, destinationBounds.Height);

            return !offsetToZeroRect.Contains((int)size.Width, (int)size.Height);
        }

        void DrawItemInList(object sender, DrawItemEventArgs e)
        {
            e.DrawBackground();

           string text = this.Items[e.Index].ToString();
            bool truncated;

            using (Graphics graphics = e.Graphics)
            {
                graphics.DrawString(text, e.Font, Brushes.Black, e.Bounds, StringFormat.GenericDefault);
                truncated = IsTruncated(text, graphics, e.Font, e.Bounds);
            }

            if ((e.State == DrawItemState.Selected) && (truncated))
            {
                this.itemToolTip.Show(text, this, e.Bounds.X + e.Bounds.Width, e.Bounds.Y + e.Bounds.Height);
            }
            else
            {
                this.itemToolTip.Hide(this);
            }

            e.DrawFocusRectangle();
        }
    }




------解决方案--------------------
运行不了是什么意思,什么提示
------解决方案--------------------
探讨

就是我新建一个 窗体Form1 把1楼源码中的自定义控件 ComboBoxToolTip 拖到 窗体上
然后把控件 ComboBoxToolTip 属性项中的 DropDownStyle 修改为 DropDownList 原始初始属性值 DropDown 就提示InvalidArgument="-1"的值对于 "index" 无效
怎么修改1楼的源码?