Delphi CXGrid中只允许负数

Delphi CXGrid中只允许负数,delphi,tcxgrid,Delphi,Tcxgrid,我在Delphi 2006与DevXpress合作 我有一个cxGrid。我想限制输入负数列的值 我的问题是如何在将“-”添加到单元格时测试它的位置 是一种只允许cxgrid单元格中出现负数的简单方法 提前感谢做你想做的事情最简单的方法是在网格单元中使用MaskEdit,但是正如你在评论中所说的,你不想使用MaskEdit,我已经将如何做到这一点移动到了这个答案的末尾 您可以使用自己的代码完全控制用户对单元格文本的编辑,我将向您展示如何做到这一点 默认情况下,如果在cxGrid列中为整数字段键入

我在Delphi 2006与DevXpress合作

我有一个cxGrid。我想限制输入负数列的值

我的问题是如何在将“-”添加到单元格时测试它的位置

是一种只允许cxgrid单元格中出现负数的简单方法


提前感谢

做你想做的事情最简单的方法是在网格单元中使用MaskEdit,但是正如你在评论中所说的,你不想使用MaskEdit,我已经将如何做到这一点移动到了这个答案的末尾

您可以使用自己的代码完全控制用户对单元格文本的编辑,我将向您展示如何做到这一点

默认情况下,如果在cxGrid列中为整数字段键入字母键,则会听到嘟嘟声。这是由于键导致链接到单元格的整数字段返回
False
,从而导致
TField.IsValidChar(InputChar:Char)

如果您想自己处理这样的“错误”键,而不使用MaskEdit,您可以在EditKeyPressed事件中完成。下面的代码显示,您可以自己执行用户可以在字段中输入的内容,而不必像下面的注释中引用的那样,模拟编辑按键。请仔细注意代码中的注释

procedure TForm1.cxGrid1DBTableView1EditKeyPress(Sender:
    TcxCustomGridTableView; AItem: TcxCustomGridTableItem; AEdit:
    TcxCustomEdit; var Key: Char);
var
  AField : TField;
  strValue : String;
  V : Variant;
  i,
  InsertPoint,
  EC : Integer;

  function CharIsOK(Ch : Char) : Boolean;
  begin
    Result := CharInSet(Ch, ['0', '1', '2', '3', '4', '5', '6',
                        '7', '8', '9', '-', '+']);
    // Or Result := Ch in ['0', ... for Delphi prior to D2009
  end;

begin
  if AItem = cxGrid1DBTableView1Value then begin
    //  The following manually cleans up input into a TIntegerField column
    // whose Properties is set to TextEdit

    // First, pick up the text in the inplace editor
    V := AEdit.EditingValue;
    if not VarIsNull(V) then
      strValue := AEdit.EditingValue
    else
      strValue := '';

    if strValue <> '' then begin
      //  Next, check that the Key is a valid one for an Integer field.
      if CharIsOk(Key) then begin
      //  The fact that the Key is a valid character for an Integer field
      //  does not in itself guarantee that the entire editing string, including the Key
      //  which is about to be added to in, is a valid string representation of an integer,
      //  e.g. it might be '--', '+1-5', etc
      //  So, we add the Key to the existing editing string and see if it converts to an integer
      //  Of course, there is the wrinkle that the caret may not be at the end of the editing text
      //  so we need to find out where the caret is.  First we need 
      //  to check that AEdit is a TcxtextEdit so that we can access
      // its SelStart property
        Assert(AEdit is TcxTextEdit);
        InsertPoint := TcxTextEdit(AEdit).SelStart;
        Insert(Key, strValue, InsertPoint + 1);
        Val(strValue, i, EC);
        //  if EC is non-zero, the conversion failed, so we suppress the Key
        if EC <> 0 then
          Key := Chr(0);
       end
       else
         //  the Key might be a backspace, so permit that
         if Ord(Key) <> VK_Back then
           Key := Chr(0);
    end
  end;
end;
程序TForm1.cxGrid1DBTableView1EditKeyPress(发送方:
TcxCustomGridTableView;AItem:TcxCustomGridTableItem;AEdit:
TcxCustomEdit;变量键:Char);
变量
外地:特菲尔德;
strValue:字符串;
V:变体;
我
插入点,
EC:整数;
函数CharIsOK(Ch:Char):布尔;
开始
结果:=CharInSet(Ch,['0','1','2','3','4','5','6',',
'7', '8', '9', '-', '+']);
//或结果:=CHIN['0',…对于D2009之前的Delphi
结束;
开始
如果AItem=cxGrid1DBTableView1Value,则开始
//以下步骤手动清除到TIntegerField列中的输入
//其属性设置为TextEdit
//首先,在inplace编辑器中拾取文本
V:=AEdit.EditingValue;
如果不是Varisnell(V),则
strValue:=AEdit.EditingValue
其他的
标准值:='';
如果为strValue“”,则开始
//接下来,检查该键对于整数字段是否有效。
如果是CharIsOk(键),则开始
//键是整数字段的有效字符这一事实
//本身并不保证整个编辑字符串,包括键
//将要添加到中的,是整数的有效字符串表示形式,
//例如,它可能是“--”、“+1-5”等
//因此,我们将键添加到现有的编辑字符串中,并查看它是否转换为整数
//当然,有一点是,插入符号可能不在编辑文本的末尾
//所以我们需要找到插入符号的位置。首先我们需要
//检查AEdit是否为TCXTEDIT,以便我们可以访问
//它的SelStart属性
断言(AEdit是tcxtedit);
插入点:=tcxtedit(AEdit).SelStart;
插入(键、标准值、插入点+1);
Val(标准值,i,EC);
//如果EC为非零,则转换失败,因此我们抑制键
如果EC为0,则
键:=Chr(0);
结束
其他的
//关键可能是退格,所以请允许这样做
如果Ord(键)VK_返回,则
键:=Chr(0);
结束
结束;
结束;
当然,如果你真的想实现你在q的标题中所说的,即只允许负数,你可以对这段代码做一点小小的修改,要求编辑文本以减号开头(如果编辑文本不是空的,编辑文本+键将转换为整数)


顺便说一句,这段代码当然回答了您的问题“如何在onpresskey中获取字符串值?”

要在cxGrid单元格中使用MaskEdit,请在对象检查器中选择cxGrid列,然后单击

  • 转到OI中的属性条目

  • 从下拉列表中选择“蒙版编辑”

  • 设置负数的编辑掩码。如果需要帮助,请参阅联机帮助:基本上,输入负号,后跟数字9


然后,用户只能输入负数,并且不能编辑掉前导减号。

正常值DevEx控件中的方法是编写一个
OnValidate
处理程序。感谢您的回答,我在presskey和onkeydown中完成了此操作,但当我想在编辑单元格时获取单元格内容,以便测试我是否具有“-”的位置时,有一些示例可以执行此操作?我建议转到DevEx支持站点并搜索OnVali日期。他们的支持非常好。我如何在onpresskey中获取字符串值?cxGrid连接到数据源的问题,然后我无法使用此属性更改为MaskEditWell,我的属性也连接到TDataSource,并且在测试答案时,我没有遇到任何困难将属性更改为Mask Edit!有什么不能解决的是的,症状是什么?好的,我做了掩码编辑,但是我可以在编辑掩码中添加什么,例如“-123”和“123”编辑掩码是OLH解释的。在任何情况下,我以为您试图避免用户在没有前面减号的情况下输入“123”。事实上,我想接受正数和负数