LazarusでStringGridを使ってみた
参考ページ
https://www.petitmonte.com/bbs/answers?question_id=6229
http://delfusa.main.jp/delfusafloor/archive/www.nifty.ne.jp_forum_fdelphi/samples/00488.html
ポイント
行は
・Row
列は
・Col
幅を変えるのは
・StringGrid1.ColWidths[行]:=40;
行を追加するのは
・StringGrid1.RowCount:=・StringGrid1.RowCount+1;
セルは
・StringGrid1.Cells[列,行]:=’文字列’;
イベントは
・TForm1.StringGrid1Click(Sender: TObject);
表(StringGrid)を選択(クリックした時)行(Row)と列(Col)を利用できる
・TForm1.Edit1Change(Sender: TObject);
入力欄(Edit1)の内容を変更した時
procedure TForm1.FormCreate(Sender: TObject);
begin
StringGrid1.ColCount:=5;
StringGrid1.RowCount:=5;
StringGrid1.Cells[1,0]:='No';
StringGrid1.Cells[2,0]:='data1';
StringGrid1.Cells[3,0]:='data2';
StringGrid1.Cells[4,0]:='data3';
StringGrid1.Cells[1,1]:='1';
StringGrid1.Cells[2,1]:='1-1';
StringGrid1.Cells[3,1]:='1-2';
StringGrid1.Cells[4,1]:='1-3';
StringGrid1.Cells[1,2]:='2';
StringGrid1.Cells[2,2]:='2-1';
StringGrid1.Cells[3,2]:='2-2';
StringGrid1.Cells[4,2]:='2-3';
StringGrid1.Cells[1,3]:='3';
StringGrid1.Cells[2,3]:='3-1';
StringGrid1.Cells[3,3]:='3-2';
StringGrid1.Cells[4,3]:='3-3';
StringGrid1.Cells[1,4]:='4';
StringGrid1.Cells[2,4]:='4-1';
StringGrid1.Cells[3,4]:='4-2';
StringGrid1.Cells[4,4]:='4-3';
StringGrid1.ColWidths[0]:=40;
StringGrid1.ColWidths[1]:=40;
StringGrid1.ColWidths[2]:=80;
StringGrid1.ColWidths[3]:=80;
StringGrid1.ColWidths[4]:=100;
with StringGrid1 do
begin
Edit1.Text := Cells[1,Row];
Edit2.Text := Cells[2,Row];
Edit3.Text := Cells[3,Row];
end;
button1.Caption:='削除';
button2.Caption:='挿入';
end;
procedure TForm1.Edit1Change(Sender: TObject);
begin
with StringGrid1 do
Cells[1,Row]:=Edit1.Text;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
KeepTopRow, KeepRow: Integer;
ARow: Integer;
begin
with StringGrid1 do begin
ARow:=Row;
KeepRow := Row;
KeepTopRow := TopRow;
BeginUpdate;
try
DeleteRow(ARow);
if RowCount > KeepRow then Row := KeepRow
else Row := KeepRow - 1;
TopRow := KeepTopRow;
SetFocus;
finally
EndUpdate;
end;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
//var
// i : Integer;
// k : Integer;
//begin
// https://www.petitmonte.com/bbs/answers?question_id=6229
// http://delfusa.main.jp/delfusafloor/archive/www.nifty.ne.jp_forum_fdelphi/samples/00488.html
var
i,InsCount,InsAt:integer;
begin
InsCount:=1;
InsAt:=StringGrid1.Row;
with StringGrid1 do
begin
RowCount:=RowCount+InsCount;
For i:=RowCount-1 downTo InsAt+InsCount do
begin
Rows[i].assign(Rows[i-InsCount]);
end;
For i:=InsAt to InsAt+InsCount-1 do
begin
Rows[i].clear;
end;
end;
end;
procedure TForm1.Edit2Change(Sender: TObject);
begin
with StringGrid1 do
Cells[2,Row]:=Edit2.Text;
end;
procedure TForm1.Edit3Change(Sender: TObject);
begin
with StringGrid1 do
Cells[3,Row]:=Edit3.Text;
end;
procedure TForm1.StringGrid1Click(Sender: TObject);
begin
with StringGrid1 do
begin
Edit1.Text := Cells[1,Row];
Edit2.Text := Cells[2,Row];
Edit3.Text := Cells[3,Row];
end;
end;