Проблема фокуса и двойного щелчка в ячейке DataGridView, содержащей MaskedTextBox - PullRequest
1 голос
/ 01 апреля 2020

У меня проблема при попытке отредактировать ячейку, содержащую текстовое поле maskedtext:

Если я пытаюсь отредактировать поле, мне нужно:

  1. двойной щелчок по ячейке ( на этом шаге маска-текстовое поле становится видимым)
  2. снова нажмите на поле, чтобы начать редактирование
  3. установить курсор «вручную» на первую позицию маскированного текста

(4. начало ввод значения)

Можно ли как-нибудь избежать "ручной" фокусировки на первой позиции маскированного текста? (например, при одиночном или двойном щелчке: установить видимое текстовое поле в маске и в то же время установить фокус / курсор в первой позиции для текстового окна с маской)

Я пробовал с помощью: 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;
            }
        }

1 Ответ

1 голос
/ 01 апреля 2020

Элемент управления, вероятно, должен получить фокус после метод BeginEdit завершен, поэтому попробуйте его следующим образом:

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