【问题标题】:Focus and Double Click problem in a DataGridView cell that contains a MaskedTextBox包含 MaskedTextBox 的 DataGridView 单元格中的焦点和双击问题
【发布时间】:2020-04-01 12:46:40
【问题描述】:

我在尝试编辑包含蒙版文本框的单元格时遇到问题:

如果我尝试编辑我需要的字段:

  1. 双击单元格(在此步骤中,蒙版文本框变为可见)
  2. 再次单击该字段开始编辑
  3. 将光标“手动”设置为蒙版文本框的第一个位置

(4.开始输入值)

有可能以某种方式避免“手动”聚焦到 maskedtextbox 的第一个位置吗? (例如单击或双击:设置可见的蒙版文本框,同时将焦点/光标设置在蒙版文本框的第一个位置)

我尝试过:focus()、select()、SelectionStart、CurrentCell,但没有运气。

我通过以下方式将 MaskedTextbox 添加到 DataGridView 单元格:

public Insert()
    {
        InitializeComponent();

        this.maskedTextBox = new MaskedTextBox();

        this.maskedTextBox.Visible = false;

        this.dataGridView1.Controls.Add(this.maskedTextBox);

        this.dataGridView1.CellBeginEdit += new DataGridViewCellCancelEventHandler(dataGridView1_CellBeginEdit);

        this.dataGridView1.CellEndEdit += new DataGridViewCellEventHandler(dataGridView1_CellEndEdit);

        this.dataGridView1.Scroll += new ScrollEventHandler(dataGridView1_Scroll);
    }

    void dataGridView1_Scroll(object sender, ScrollEventArgs e)
    {
        if (this.maskedTextBox.Visible)
        {
            Rectangle rect = this.dataGridView1.GetCellDisplayRectangle(
                this.dataGridView1.CurrentCell.ColumnIndex,
                this.dataGridView1.CurrentCell.RowIndex, true);
            this.maskedTextBox.Location = rect.Location;
        }
    }
    void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
    {
        if (e.ColumnIndex == this.dataGridView1.Columns[4].Index || e.ColumnIndex == this.dataGridView1.Columns[5].Index && e.RowIndex > -1)
        {
            string type = "";
            if (this.dataGridView1[e.ColumnIndex, e.RowIndex].Value != null)
                type = this.dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();

            this.maskedTextBox.Mask = "0000.00.00";
            Rectangle rect =
               this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);

            this.maskedTextBox.Location = rect.Location;
            this.maskedTextBox.Size = rect.Size;
            this.maskedTextBox.Text = "";

            if (this.dataGridView1[e.ColumnIndex, e.RowIndex].Value != null)
            {
                this.maskedTextBox.Text = this.dataGridView1[e.ColumnIndex,
                    e.RowIndex].Value.ToString();
            }
            this.maskedTextBox.Visible = true;
            this.maskedTextBox.Focus(); //tried
            this.maskedTextBox.Select(0, 0);
            this.maskedTextBox.SelectionStart =0 ;
            dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
        }
    }
void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
if (this.maskedTextBox.Visible && (e.ColumnIndex == this.dataGridView1.Columns["TEST"].Index && e.RowIndex > -1))
            {
                this.dataGridView1.CurrentCell.Value = maskedTextBox.Text;
                this.maskedTextBox.Visible = false;
            }
        }

【问题讨论】:

  • 尝试使用自定义 DataGridView 列版本。见DataGridView Masked TextBox Column
  • 问题是:这个代码被用在很多表单上,并且datagridview也有其他控件(比如组合框控件),它们的实现方式与上面的maskedtextbox相同

标签: c# .net winforms datagridview maskedtextbox


【解决方案1】:

控件可能需要在BeginEdit方法完成后获得焦点,所以试试这个方法:

this.BeginInvoke(new Action(() => {
  this.maskedTextBox.Visible = true;
  this.maskedTextBox.Focus();
  this.maskedTextBox.Select(0, 0);
}));

【讨论】:

  • 完美答案,有效!我重写了一点代码,因为我在 VS 2005 中使用 .net 2.0:this.BeginInvoke((MethodInvoker)delegate() { this.maskedTextBox.Visible = true; this.maskedTextBox.Focus(); this.maskedTextBox.Select(0, 0); });,非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-06
  • 2012-09-27
  • 2015-09-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多