【问题标题】:getting caret position from DataGridView从 DataGridView 获取插入符号位置
【发布时间】: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


【解决方案1】:

如果其他人正在寻找它,我在 LarsTech 的帮助下找到了答案。

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;
        if (DGV.CurrentCell.EditType == typeof(DataGridViewTextBoxEditingControl))
            if(DGV.EditingControl != null)
                charIndex = ((TextBox)DGV.EditingControl).SelectionStart;
    }

    switch ((GridDataEnum)DGV.CurrentCell.ColumnIndex)
    {
        case GridDataEnum.setpoint:
        case GridDataEnum.SlopePoint:
            e.Handled = !HelperUtils.isValidNumber(curStr, e.KeyChar, HelperUtils.TargetNumberTypeEnum.signedDouble, charIndex);
            break;
        case GridDataEnum.lowerX:
        case GridDataEnum.upperX:
        case GridDataEnum.TransX:
        case GridDataEnum.constY:
            e.Handled = !HelperUtils.isValidNumber(curStr, e.KeyChar, HelperUtils.TargetNumberTypeEnum.unsignedDouble, charIndex);
            break;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-12
    • 1970-01-01
    • 1970-01-01
    • 2019-07-22
    • 1970-01-01
    • 2015-08-26
    • 2013-04-20
    相关资源
    最近更新 更多