DataGridView设置ToolTip效果

2015-05-03 10:59:17  访问(2566) 赞(0) 踩(0)


        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolTip1_Draw(object sender, DrawToolTipEventArgs e)
        {
            e.Graphics.FillRectangle(Brushes.AliceBlue, e.Bounds);
            e.Graphics.DrawRectangle(Pens.Chocolate, new Rectangle(0, 0, e.Bounds.Width - 1, e.Bounds.Height - 1));
            e.Graphics.DrawString
                (
                    this.toolTip1.ToolTipTitle + e.ToolTipText, 
                    e.Font, 
                    Brushes.Red, 
                    e.Bounds
                );
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView_Main_MouseLeave(object sender, EventArgs e)
        {
            this.toolTip1.Hide(this.dataGridView_Main);
        }

        /// <summary>
        /// 
        /// </summary>
        private int cellColumnIndex = -1, cellRowIndex = -1;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView_Main_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex < 0 || e.ColumnIndex < 0)
            {
                return;
            }

            if (this.cellColumnIndex == e.ColumnIndex
                && this.cellRowIndex == e.RowIndex)
            {
                return;
            }

            this.toolTip1.Hide(this.dataGridView_Main);
            this.cellColumnIndex = e.ColumnIndex;
            this.cellRowIndex = e.RowIndex;

            if (this.cellColumnIndex >= 0 && this.cellRowIndex >= 0)
            {
                Point mousePos = PointToClient(MousePosition);
                string strValue = "";

                if(this.dataGridView_Main[this.cellColumnIndex, this.cellRowIndex].Value!=null)
                    strValue = this.dataGridView_Main[this.cellColumnIndex, this.cellRowIndex].Value.ToString();

                string tip = "Tip is " + strValue;
                this.toolTip1.Show(tip, this.dataGridView_Main, mousePos);
            }
        }

        


标签:DataGridView设置ToolTip效果 

上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)
 
  ┈全部┈  
 
(显示默认分类)