2016-02-12 49 views
1

我使用devexpress在Delphi 2006中工作。只允许在CXGrid中使用负数

我有一个cxGrid。我想限制值的负数列中的条目,

我probleme是如何测试的位置“ - ”添加它的细胞

当是一些简单的方法,让只在负数cxgrid的单元格。

在此先感谢

+0

DevEx控件中的“正常”方式是编写一个'OnValidate'处理程序。 –

+0

感谢您的回答,我在按键和onkeydown中完成了这项工作,但是当编辑单元格时为了测试位置而需要单元格的内容时,如果我有' - '或不存在,有一些示例可以做到这一点? –

+0

我建议去DevEx支持网站并在那里搜索OnValidate。他们的支持非常好。 –

回答

1

的eaiest的方式做你想要的是在网格单元使用MaskEdit,但正如您在评论说,你不希望使用一个,我我已经提出了如何做到这一点的答案。

您可以完全在自己的代码中控制用户对单元格文本的编辑,我会告诉你如何做到这一点。

默认情况下,如果您在整数字段的cxGrid列中键入字母键,您会听到蜂鸣声。发生这种情况的原因是键为导致TField.IsValidChar(InputChar: Char)的整数字段链接到单元返回False

如果您想自己处理“错误”的按键,而不使用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; 

当然,如果你真正想实现你在Q的标题说明什么,即只允许负数,就可以使这个代码一个微不足道的变化需要编辑文本中有一个开始减号(如果编辑文本不为空,编辑文本+键将转换为整数)。

Btw,此代码回答您的查询“我怎样才能得到onpresskey的字符串值?”当然。

要在cxGrid细胞在OI使用MaskEdit,选择Object Inspector中cxGrid列,然后

  • 打开属性条目

  • 选择面膜编辑从下拉下拉列表

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

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

+0

cxGrid连接到数据源的问题,然后我不能使用此属性更改为MaskEdit –

+0

嗯,我也连接到TDataSource,并且我没有任何困难时将属性更改为掩码编辑当我测试了我的答案!你不能做什么,有什么症状? – MartynA

+0

好的我已经完成了mask编辑,但是我可以在Edit mask中放置什么,例如“-123”和“123” –