StringGrid 功能实现:StringGrid 中编辑完A列 回车就显示B列的值 (B列的值由公式:B=1/A得出) 这个怎么实现呢?

hedy_lu 2015-04-13 09:29:21
StringGrid 中编辑完A列 回车就显示B列的值 (B列的值由公式:B=1/A得出) 这个怎么实现呢?求大家帮忙,谢谢
...全文
184 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
yct0605 2015-04-29
  • 打赏
  • 举报
回复

procedure TForm1.StringGrid1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
i:byte;
begin
for i:=1 to 4 do //假设有四行
begin
if (C=0) then //默认第一列
begin
if Key=13 then
begin
if Trim(StringGrid1.Cells[0,i])='0' then
begin
ShowMessage('Error');
StringGrid1.Cells[1,i]:='';
end
else
if Trim(StringGrid1.Cells[0,i])<>'' then
StringGrid1.Cells[1,i]:=FloatToStr(1/StrToInt(StringGrid1.Cells[0,i]))
else
ShowMessage('Null');
end;
end;
end;
end;




在第一列的任意单元格内点击回车实现自动计算。
hedy_lu 2015-04-29
  • 打赏
  • 举报
回复
引用 1 楼 yct0605 的回复:

var
  Form1: TForm1;
  C,R:Longint;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  StringGrid1.Cells[0,0]:='A';
  StringGrid1.Cells[1,0]:='B';
  StringGrid1.Cells[2,0]:='C'; 
end;

procedure TForm1.StringGrid1MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  StringGrid1.MouseToCell(X,Y,C,R);
end;

procedure TForm1.StringGrid1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (C=0) and (R=1) then
  begin
    if Key=13 then
    begin
      if Trim(StringGrid1.Cells[0,1])='0' then
      begin
        ShowMessage('Error');
        StringGrid1.Cells[1,1]:='';
      end
      else
        if Trim(StringGrid1.Cells[0,1])<>'' then
          StringGrid1.Cells[1,1]:=FloatToStr(1/StrToInt(StringGrid1.Cells[0,1]))
        else
          ShowMessage('Null');
    end;
  end;
end;
怎么样设置成整列都可以,这个只是第一行第一列
lyhoo163 2015-04-14
  • 打赏
  • 举报
回复
楼上方向对,要设置成整个列都可以。
doloopcn 2015-04-14
  • 打赏
  • 举报
回复
StringGrid中有一个事件 OnSetEditText(Sender: TObject; ACol, ARow: Integer; const Value: String); 在这个事件中,有足够的参数让你设置公式
yct0605 2015-04-13
  • 打赏
  • 举报
回复



var
Form1: TForm1;
C,R:Longint;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
StringGrid1.Cells[0,0]:='A';
StringGrid1.Cells[1,0]:='B';
StringGrid1.Cells[2,0]:='C';
end;

procedure TForm1.StringGrid1MouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
StringGrid1.MouseToCell(X,Y,C,R);
end;

procedure TForm1.StringGrid1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if (C=0) and (R=1) then
begin
if Key=13 then
begin
if Trim(StringGrid1.Cells[0,1])='0' then
begin
ShowMessage('Error');
StringGrid1.Cells[1,1]:='';
end
else
if Trim(StringGrid1.Cells[0,1])<>'' then
StringGrid1.Cells[1,1]:=FloatToStr(1/StrToInt(StringGrid1.Cells[0,1]))
else
ShowMessage('Null');
end;
end;
end;

16,749

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧