我需要双击datagridview内的Masked Textbox。
但是当我尝试在蒙版文本框获得焦点之后输入一些值时,我无法输入任何值。但是当我使用鼠标双击时我可以输入一个值。因此,我通过在DataGridView.KeyPress事件中编写SendKeys.Send(“ {F2}”)进行检查,该事件适用于所有要编辑的普通列(如文本框),但无法在MaskedTextBox中输入值。
请双击鼠标的SendKeys.Send()帮助我。
在DataGridView中,按键事件尝试调用sendKeys.send(“ {F2}”)。
sendKeys.send(“ {F2}”)
当datagridview内部的Masked文本框成为焦点时,用户应该能够输入一个值。
下面是我的示例代码:
private void Form7_Load(object sender, EventArgs e)
{
DataGridViewComboBoxColumn TpyeCol = new DataGridViewComboBoxColumn();
TpyeCol.Name = "Type";
TpyeCol.HeaderText = "Type";
TpyeCol.Items.AddRange(new string[] { "Home Phone", "Cell", "Work", "Email" });
this.dataGridView1.Columns.Add(TpyeCol);
this.dataGridView1.Columns.Add("Description", "Description");
this.dataGridView1.Rows.Add("Home Phone","");
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["Description"].Index &&
e.RowIndex < this.dataGridView1.NewRowIndex)
{
string type = this.dataGridView1["Type",e.RowIndex].Value.ToString();
if (type == "Home Phone" || type == "Cell" || type == "Work")
{
this.maskedTextBox.Mask = "00/00/0000";
Rectangle rect = this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex,e.RowIndex,true);
this.maskedTextBox.Location = rect.Location;
this.maskedTextBox.Size = rect.Size;
this.maskedTextBox.Text = "04/02/2019";
if (this.dataGridView1[e.ColumnIndex, e.RowIndex].Value != null)
{
this.maskedTextBox.Text = this.dataGridView1[e.ColumnIndex,
e.RowIndex].Value.ToString();
}
this.maskedTextBox.Visible = true;
}
}
}
void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (this.maskedTextBox.Visible)
{
this.dataGridView1.CurrentCell.Value = this.maskedTextBox.Text;
this.maskedTextBox.Visible = false;
}
}
void dataGridView1_KeyPress(object sender, DataGridKeyPressEventArgs e)
{
If(dataGridView1.ColumnIndex = 1)
{
SendKeys.Send("{F2}") //To Point cursor in position 1 of Masked TextBox but not able to edit the masked textbox in DGV
}
}
MaskedTextBox maskedTextBox;