Exemplo n.º 1
0
        /// <summary>
        /// 文本框失去焦点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void rtbTitle_Leave(object sender, EventArgs e)
        {
            DataGridViewColumn myColumn = this.m_SortedColumnList[this.m_SelectedColumnIndex];
            //定义事件参数
            ColumnHeaderEditEventArgs myArgs = new ColumnHeaderEditEventArgs(myColumn, this.rtbTitle.Text.Trim());

            if (this.EndingEdit != null)
            {
                this.EndingEdit(this, myArgs); //引发事件
                if (myArgs.Cancel)             //如果取消标志为true
                {
                    this.rtbTitle.Focus();     //保持编辑状态
                    return;
                }
            }

            this.rtbTitle.Visible = false;
            if (myArgs.NewHeaderText.Length > 0)//不允许用空字符串作为标题
            {
                if (myColumn.HeaderText != myArgs.NewHeaderText)
                {
                    //用事件参数里面的新标题,因为在事件处理程序里面可能修改新标题
                    myColumn.HeaderText = myArgs.NewHeaderText;
                }
            }

            if (this.EndEdit != null)
            {
                this.EndEdit(this, myArgs);//引发事件
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 显示标题编辑效果
        /// </summary>
        private void ShowHeaderEdit()
        {
            if (this.BeginEdit != null)
            {
                ColumnHeaderEditEventArgs myArgs = new ColumnHeaderEditEventArgs(this.m_SortedColumnList[this.m_SelectedColumnIndex], "");
                BeginEdit(this, myArgs);
                if (myArgs.Cancel)
                {
                    return;
                }
            }

            int intColumnRelativeLeft = 0;
            //第一列左边距,需要判断是否显示行标题
            int intFirstColumnLeft = (this.m_TargetControl.RowHeadersVisible ? this.m_TargetControl.RowHeadersWidth + 1 : 1);
            int intTargetX = this.m_TargetControl.Location.X, intTargetY = this.m_TargetControl.Location.Y, intTargetWidth = this.m_TargetControl.Width;

            intColumnRelativeLeft = GetColumnRelativeLeft(this.m_SelectedColumnIndex);

            if (intColumnRelativeLeft < this.m_ScrollValue)
            {
                this.rtbTitle.Location = new Point(intTargetX + intFirstColumnLeft, intTargetY + 1);
                if (intColumnRelativeLeft + this.m_SortedColumnList[this.m_SelectedColumnIndex].Width > this.m_ScrollValue)
                {
                    this.rtbTitle.Width = intColumnRelativeLeft + this.m_SortedColumnList[this.m_SelectedColumnIndex].Width - this.m_ScrollValue;
                }
                else
                {
                    this.rtbTitle.Width = 0;
                }
            }
            else
            {
                this.rtbTitle.Location = new Point(intColumnRelativeLeft + intTargetX - this.m_ScrollValue + intFirstColumnLeft, intTargetY + 1);

                if (this.rtbTitle.Location.X + this.rtbTitle.Width > intTargetX + intTargetWidth)
                {
                    int intWidth = intTargetX + intTargetWidth - this.rtbTitle.Location.X;
                    this.rtbTitle.Width = (intWidth >= 0 ? intWidth : 0);
                }
                else
                {
                    this.rtbTitle.Width = this.m_SortedColumnList[this.m_SelectedColumnIndex].Width;
                }
            }

            this.rtbTitle.Height = this.m_TargetControl.ColumnHeadersHeight - 1;
            this.rtbTitle.Text   = this.m_SortedColumnList[this.m_SelectedColumnIndex].HeaderText;
            this.rtbTitle.SelectAll();
            this.rtbTitle.Visible = true;
            this.rtbTitle.Focus();
        }