【发布时间】:2020-02-25 15:12:59
【问题描述】:
我希望验证具有 DataGridViewTextBoxCells 的 DataGridView 的输入。文本框单元格可以是有符号或无符号的,也可以是 int 或 double,具体取决于焦点列。我遇到的问题是确定按下键时的插入符号位置。
例如,如果单元格允许有符号双打(插入符号 '^'):
- 有效的按键事件:^12.3456 按键为“-”将给出 -12.3456
- 无效的按键事件:12.34-56 按键为“-”将给出 12.34-56
我找不到任何可以让我在按键时访问插入符号位置的东西。
private void SomeGridView_KeyPress(object sender, KeyPressEventArgs e)
{
DataGridView DGV = SomeGridView;
string curStr;
bool isFirst = DGV.CurrentCell.EditedFormattedValue == null;
curStr = isFirst ? "" : DGV.CurrentCell.EditedFormattedValue.ToString();
Type type = DGV.CurrentCell.GetType();
if (DGV.CurrentCell.GetType() == typeof(DataGridViewTextBoxCell))
{
DataGridViewTextBoxCell DGVTB = (DataGridViewTextBoxCell)DGV.CurrentCell;
//Not sure how to get caret here
}
switch ((GridDataEnum)DGV.CurrentCell.ColumnIndex)
{
case GridDataEnum.setpoint:
case GridDataEnum.SlopePoint:
e.Handled = !HelperUtils.isValidNumber(curStr, e.KeyChar, HelperUtils.TargetNumberTypeEnum.signedDouble);
break;
case GridDataEnum.lowerX:
case GridDataEnum.upperX:
case GridDataEnum.TransX:
case GridDataEnum.constY:
e.Handled = !HelperUtils.isValidNumber(curStr, e.KeyChar, HelperUtils.TargetNumberTypeEnum.unsignedDouble);
break;
}
}
谢谢
【问题讨论】:
-
EditControlShowing 事件可用于在网格处于编辑模式时获取底层 TextBox 控件。
-
@LarsTech 谢谢你成功了。
标签: c# datagridview textbox caret