⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 area.pas

📁 物业管理系统系统特点 一、 实现集中式管理 系统将集团公司、各分公司、各物业管理处连接到一起
💻 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 + -