?? area.pas
字號:
unit area;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, dbcgrids, Grids, DBGrids, DB, ADODB, StdCtrls, Buttons, ExtCtrls,
StrUtils, ExtDlgs, ComCtrls, math;
type
Tf_area = class(TForm)
BDelete: TButton;
BPageDown: TButton;
BESC: TButton;
BInsert: TButton;
StringGrid1: TStringGrid;
procedure BPageDownClick(Sender: TObject);
procedure BESCClick(Sender: TObject);
procedure BDeleteClick(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure BInsertClick(Sender: TObject);
procedure ListView1CustomDrawItem(Sender: TCustomListView;
Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
procedure FormCreate(Sender: TObject);
procedure FocusTAB(var Grid: TStringGrid);
Function InfoIsNull: Boolean;
procedure StringGrid1KeyPress(Sender: TObject; var Key: Char);
procedure FormShow(Sender: TObject);
procedure StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
var CanSelect: Boolean);
procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
private
{ Private declarations }
public
{ Public declarations }
bMod:bool;
col,row: Integer;
sNum:Array[0..100] of AnsiString;
sProvider:Array[0..100] of AnsiString;
sPrice:Array[0..100] of AnsiString;
sProv:string;
sStyle:string;
sTable:string;
end;
var
f_area: Tf_area;
implementation
uses data, main;
{$R *.dfm}
//初始化
procedure Tf_area.FormCreate(Sender: TObject);
var
Query:TADOQuery;
i:integer;
begin
bOK:=false;
Col := 0;
Row := 1;
stringgrid1.DefaultRowHeight:=18;
sTable:='計量單位';
i:=0;
Query:=TADOQuery.Create(nil);
Query.Connection:=DataModuleADO.ADOConnection1;
Query.SQL.Add('select * from '+sTable+' order by 編號');
Query.Open ;
while not query.Eof do
begin
inc(i);
if i>1 then
begin
stringgrid1.RowCount:=stringgrid1.RowCount+1;
stringgrid1.row:=stringgrid1.row+1;
stringgrid1.Col:=1;
end;
with stringgrid1 do
begin
Cells[0,i]:=inttostr(i);
Cells[1,i]:=Query.FieldByName('編號').Value;
Cells[2,i]:=Query.FieldByName('名稱').Value;
end;
query.Next ;
end;
end;
//顯示
procedure Tf_area.FormShow(Sender: TObject);
begin
bOK:=true;
with StringGrid1 do
begin
ColWidths[0]:=19;
ColWidths[1]:=80;
ColWidths[2]:=210;
Cells[0,0]:='序';
Cells[1,0]:='編號';
Cells[2,0]:='名稱';
Cells[0,1]:='1';
end;
end;
//Tab
procedure Tf_area.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
i:integer;
d,d1,d2:double;
begin
if key=VK_INSERT then BInsertClick(Sender)
else if key=VK_DELETE then BDeleteClick(Sender)
else if key=VK_NEXT then BPageDownClick(Sender)
else if key=VK_ESCAPE then BESCClick(Sender);
end;
//增加
procedure Tf_area.BInsertClick(Sender: TObject);
begin
StringGrid1.RowCount:=StringGrid1.RowCount+1;
StringGrid1.row:=StringGrid1.row+1;
StringGrid1.Col:=1;
StringGrid1.Cells[0,StringGrid1.row]:=IntToStr(StringGrid1.Row);
if row > 1 then StringGrid1.Cells[0,row-1]:=inttostr(row-1);
end;
//刪除
procedure Tf_area.BDeleteClick(Sender: TObject);
var
i,j,rows:integer;
d1,d2:double;
s:string;
Query:TADOQuery;
begin
if(Application.MessageBox(PChar('確認要刪除所選擇記錄嗎?') ,'提示',Mb_OKCANCEL+Mb_IconInformation)=2) then exit;
if row = 1 then
begin
for i:=1 to 2 do
stringgrid1.Cells[i,1]:='';
end
else
begin
for i:=row to stringgrid1.RowCount-1 do
begin
stringgrid1.Cells[0,i]:=inttostr(i);
stringgrid1.Cells[1,i]:=stringgrid1.Cells[1,i+1];
stringgrid1.Cells[2,i]:=stringgrid1.Cells[2,i+1];
end;
if StringGrid1.RowCount>2 then StringGrid1.RowCount := StringGrid1.RowCount-1;
end;
Query:=TADOQuery.Create(nil);
Query.Connection := DataModuleADO.ADOConnection1;
DataModuleADO.ADOConnection1.BeginTrans;
query.sql.Clear ;
Query.SQL.Text:='delete from '+sTable+' where 編號='''+stringgrid1.Cells[1,row]+'''';
query.ExecSQL;
DataModuleADO.ADOConnection1.CommitTrans;
query.Close;
query.Free;
showmessage('刪除操作成功。');
end;
//保存
procedure Tf_area.BPageDownClick(Sender: TObject);
var
i,j:integer;
s,ss,rr:string;
Query:TADOQuery;
Query1:TADOQuery;
begin
for i:=1 to stringgrid1.RowCount-1 do
begin
if stringgrid1.Cells[1,i]='' then
begin
row := i;
BDeleteClick(Sender);
end;
end;
if InfoIsNull = true then exit;
if(Application.MessageBox(PChar('確認要保存該信息嗎?') ,'提示',Mb_OKCANCEL+Mb_IconInformation)=2) then exit;
Query:=TADOQuery.Create(nil);
Query.Connection:=DataModuleADO.ADOConnection1;
Query1:=TADOQuery.Create(nil);
Query1.Connection:=DataModuleADO.ADOConnection1;
for i:=1 to stringgrid1.RowCount-1 do
begin
query.sql.Clear ;
Query.SQL.Add('select * from '+sTable+' where 編號='''+stringgrid1.Cells[1,i]+'''');
Query.Open ;
if query.RecordCount =0 then
begin
s := 'insert into '+sTable+'(編號,名稱)'+' values(:s1,:s2)';
query.sql.Clear ;
query.sql.add(s);
query.Parameters.ParamByName('s1').Value:=stringgrid1.Cells[1,i];
query.Parameters.ParamByName('s2').Value:=stringgrid1.Cells[2,i];
query.execsql;
end
else
begin
if query.Fieldbyname('名稱').AsString <> stringgrid1.Cells[1,i] then
DataModuleADO.ADOConnection1.BeginTrans;
query.sql.Clear ;
Query.SQL.Text:='update '+sTable+' set 名稱='''+stringgrid1.Cells[2,i]+''' where 編號='''+stringgrid1.Cells[1,i]+'''';
query.ExecSQL;
DataModuleADO.ADOConnection1.CommitTrans;
end;
end;
query.Close;
query.Free;
query1.Close;
query1.Free;
showmessage('保存成功。');
bOK:=true;
end;
//返回
procedure Tf_area.BESCClick(Sender: TObject);
var
b:bool;
begin
If bOK=false Then
begin
if(Application.MessageBox(PChar('還沒保存,要退出嗎?') ,'提示',Mb_OKCANCEL+Mb_IconInformation)=1) then
b := True;
end
Else
b := True;
If b = True Then
begin
close();
End;
end;
//1 StringGrid1 Tab 加行
Procedure Tf_area.FocusTAB(var Grid: TStringGrid);
begin
if Grid.Col<Grid.ColCount-1 then Grid.Col:=Grid.Col+1
else if Grid.row<>Grid.RowCount-1 then //判斷是否是最后1行
begin
Grid.row:=Grid.row+1;
Grid.Col:=1;
end
else
begin
Grid.RowCount:=Grid.RowCount+1;
Grid.row:=Grid.row+1;
Grid.Col:=1;
StringGrid1.Cells[0,Grid.RowCount-1]:=inttostr(Grid.RowCount-1);
end;
end;
//2 StringGrid1 KeyPress 數量、單價只能為double值
procedure Tf_area.StringGrid1KeyPress(Sender: TObject; var Key: Char);
begin
if key=#13 then Self.FocusTAB(StringGrid1);
if (col = 5)or(col = 6) then
if not( Key in ['0'..'9',#8,'.']) then
Key := #0;
end;
//3 StringGrid1 SelectCell 設置只讀欄
procedure Tf_area.StringGrid1SelectCell(Sender: TObject; ACol,
ARow: Integer; var CanSelect: Boolean);
begin
Row := ARow;
Col := ACol;
if (acol=1) or (acol=2) then //列號
stringgrid1.Options := stringgrid1.Options + [goediting]
else
stringgrid1.Options :=stringgrid1.Options - [goediting];
end;
//4 StringGrid1 DrawCell 設置欄對齊
procedure Tf_area.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
s: String;
R: TRect;
begin
with StringGrid1 do
begin
Canvas.FillRect(Rect);
S := Cells[ACol,ARow];
R := Rect;
if (ACol>4) and (ACol<10) then DrawText(Canvas.Handle,PChar(s),Length(s),r,DT_RIGHT or DT_SINGLELINE or DT_VCENTER)
else if ACol=0 then DrawText(Canvas.Handle,PChar(s),Length(s),r,DT_CENTER or DT_SINGLELINE or DT_VCENTER)
else DrawText(Canvas.Handle,PChar(s),Length(s),r,DT_LEFT or DT_SINGLELINE or DT_VCENTER);
end;
end;
procedure Tf_area.ListView1CustomDrawItem(Sender: TCustomListView;
Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
begin
if Item.Selected = true then
begin
Sender.Canvas.Brush.Color := clMenuHighlight;
(Sender as TListView).Canvas.Font.Color := clWhite;
end;
end;
//檢查編號、名稱是否有重復
function Tf_area.InfoIsNull: Boolean;
var
i,j,k: Integer;
query:TADOQuery;
begin
Query:=TADOQuery.Create(nil);
Query.Connection := DataModuleADO.ADOConnection1;
For i := 1 to StringGrid1.RowCount-1 do
begin
For j := i+1 to StringGrid1.RowCount-1 do
begin
if Trim(StringGrid1.Cells[1,i])=Trim(StringGrid1.Cells[1,j]) then
begin
showmessage('編號重復。');
StringGrid1.row := j;
StringGrid1.col :=1;
Result :=true;
exit;
end;
end;
For j := i+1 to StringGrid1.RowCount-1 do
begin
if Trim(StringGrid1.Cells[2,i])=Trim(StringGrid1.Cells[2,j]) then
begin
showmessage('名稱重復。');
StringGrid1.row := j;
StringGrid1.col :=2;
Result :=true;
exit;
end;
end;
end;
Result :=false;
end;
end.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -