Nugroho's blog.: Delphi: Click a Cell on Stringgrid to Toggle its Value

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

No comments:

323f (5) amp (1) android (12) apple (7) arduino (18) art (1) assembler (21) astina (4) ATTiny (23) blackberry (4) camera (3) canon (2) cerita (2) computer (106) crazyness (11) debian (1) delphi (39) diary (286) flash (8) fortran (6) freebsd (6) google apps script (8) guitar (2) HTML5 (10) IFTTT (7) Instagram (7) internet (12) iOS (5) iPad (6) iPhone (5) java (1) javascript (1) keynote (2) LaTeX (6) lazarus (1) linux (29) lion (15) mac (28) macbook air (8) macbook pro (3) macOS (1) Math (3) mathematica (1) maverick (6) mazda (4) microcontroler (35) mountain lion (2) music (37) netbook (1) nugnux (6) os x (36) php (1) Physicist (29) Picture (3) programming (189) Python (109) S2 (13) software (7) Soliloquy (125) Ubuntu (5) unix (4) Video (8) wayang (3) yosemite (3)