Monday, November 17, 2014

Delphi: Click a Cell on Stringgrid to Toggle its Value

Here we are. The code below is a part of (unfinished) array of JK flip-flop that draw the output on stringgrid. The problem is, we want to change input (J and K) at  the runtime which is easy if the code is not flexible (just add several button), but as we can see, the code is flexible so there is big no no for the manually added button. So we want to click the corresponding cell and the value changed (in this case toggled, 1 to 0 or otherwise).

Here the code



unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    StringGrid1: TStringGrid;
    procedure proses;
    function toStr(a:boolean):string;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
      var CanSelect: Boolean);
  private{ Private declarations }
  public{ Public declarations }
  end;
const n=5;
var
  Form1: TForm1;
  J,K,Q,nQ:array[0..n-1]of boolean;
  clock:boolean=true;
  jalan:boolean=false;
implementation

{$R *.dfm}
function tform1.toStr(a:boolean):string;
begin
  toStr:=inttostr(-1*strtoint(booltostr(a)));
end;
procedure tform1.proses;
var i:integer;
begin
  if clock=false then begin
  //flip-flop1
    if J[0]<>k[0] then Q[0]:=J[0] else begin
      if J[0]=true then Q[0]:= not Q[0];
    end;
    nQ[0]:=not Q[0];
    for i:=0 to n-1 do begin
      stringgrid1.Cells[2,i+1]:=tostr(J[i]);
      stringgrid1.Cells[3,i+1]:=tostr(K[i]);
      stringgrid1.Cells[4,i+1]:=tostr(Q[i]);
      stringgrid1.Cells[5,i+1]:=tostr(nQ[i]);
    end;
  end;
  stringgrid1.Cells[1,1]:=tostr(clock);

end;
procedure TForm1.FormCreate(Sender: TObject);
var i:integer;
begin
  for i:=0 to n-1 do begin
    J[i]:=false;
    K[i]:=false;
    Q[i]:=false;
    nQ[i]:=false;
  end;
  stringgrid1.ColCount:=6;
  stringgrid1.RowCount:=6;
  stringgrid1.Cells[1,0]:='clock';
  stringgrid1.Cells[2,0]:='J';
  stringgrid1.Cells[3,0]:='K';
  stringgrid1.Cells[4,0]:='Q';
  stringgrid1.Cells[5,0]:='nQ';
  for i:= 1 to n do begin
    stringgrid1.Cells[0,i]:='FlipFlop'+inttostr(i);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  jalan:=not jalan;
  while jalan = true do begin
    clock:=not clock;
    proses;
    application.ProcessMessages;sleep(300);
  end;
end;

procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol,
  ARow: Integer; var CanSelect: Boolean);
  var i:integer;
begin
  if ACol=2 then begin
    for i:=1 to n do begin
      if ARow=i then begin
        J[ARow-1]:=not J[ARow-1];
        stringgrid1.Cells[2,i]:=toStr(J[Arow-1]);
      end;
    end;
  end;
    if ACol=3 then begin
    for i:=1 to n do begin
      if ARow=i then begin
        K[ARow-1]:=not K[ARow-1];
        stringgrid1.Cells[3,i]:=toStr(K[Arow-1]);
      end;
    end;
  end;

end;

end.

Here the result