TValueListEditor的使用

今天研究了一下TValueListEditor控件,实现了以下功能:
·按回车键后,自动切换到下一行
·检测输入的值是否为浮点数,如果不是则弹出错误提示
·只允许输入0..9、负号、退格键。

详细功能还是看代码吧.....

 

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids, ValEdit;

type
TForm1
= class(TForm)
lst1: TValueListEditor;
btn1: TButton;
mmo1: TMemo;
btn2: TButton;
procedure btn1Click(Sender: TObject);
procedure lst1KeyPress(Sender: TObject; var Key: Char);
procedure lst1Validate(Sender: TObject; ACol, ARow: Integer; const KeyName,
KeyValue:
string);
procedure btn2Click(Sender: TObject);

private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

//-----------------------------------------------------------
//将输入的值在memo中显示
//-----------------------------------------------------------

procedure TForm1.btn2Click(Sender: TObject);
var
i:Integer;
bb:Extended;
begin
for I := 1 to lst1.RowCount - 1 do
begin
bb :
= StrToFloat(lst1.Cells[2,i]);
mmo1.Lines.Add(FloatToStr(bb));
end;
end;

//-----------------------------------------------------------
//插入一行。
//-----------------------------------------------------------
procedure TForm1.btn1Click(Sender: TObject);
begin
lst1.InsertRow(
'B1','AAA',True);

end;

//-----------------------------------------------------------
//按键处理:
//1.当回车键按下,向下转换焦点,当到达最后一个焦点后,转移到
// 第一个。
//2.输入值只能为数值。
//-----------------------------------------------------------

procedure TForm1.lst1KeyPress(Sender: TObject; var Key: Char);
begin
if Key = #13 then
begin
//ShowMessageFmt('当前行:%d,共有%d行。',[lst1.Row,lst1.RowCount]);
if lst1.Row = (lst1.RowCount -1) then
keybd_event(VK_PRIOR,
0,0,0)
else
keybd_event(VK_DOWN,
0,0,0);
end

else if not (Key in ['0'..'9','.','-',#13,#8]) then
begin
Key :
= #0;
MessageBeep(
1);
end;

end;

//-----------------------------------------------------------
//输入的数值判断:
//1.如果输入的值不能转换为浮点数,则弹出错误提示,同时,
// 光标定位到错误的那一行。
//-----------------------------------------------------------

procedure TForm1.lst1Validate(Sender: TObject; ACol, ARow: Integer;
const KeyName, KeyValue: string);
var
aa:Extended;
begin
//ShowMessageFmt('当前行:%d,当前列:%d,当前值:%s',[arow,acol,keyvalue]);

if not TryStrToFloat(KeyValue,aa) then
begin
ShowMessage(
'Error');
lst1.SetFocus;
lst1.Row :
= ARow;
lst1.Col :
= ACol;
keybd_event(VK_UP,
0,0,0);

end;


end;

end.
posted @ 2011-09-08 00:21  Delcpp  阅读(2244)  评论(0编辑  收藏  举报