首页 > 数据库技术 > 详细

动态产生和删除相关组件并生成SQL语句

时间:2015-02-05 07:13:07      阅读:429      评论:0      收藏:0      [点我收藏+]

(**********************************************************************************************************
 *
 *
 * 窗体描述:产生SQL条件语句,系统模块 [计划管理-打卡] 打印大/小卡
 *           能够根据维护进去的条件,生成过软类型: 缸内过软、缸外过软、连续皂洗、空白
 *
 * 关键功能点:1、动态产生和删除子/父等相关组件按钮(注意移动组件位置)
 *             2、组合添加的参数条件,生成 SQL 语句
 *
 *
 **********************************************************************************************************)

unit ProductCardParameters;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, dxExEdtr, dxCntner, dxTL, dxDBCtrl, dxDBGrid,
  DB, ADODB, ComCtrls, dxDBTLCl, dxGrClms, ExtCtrls, StrUtils, cxControls,
  cxSplitter;

type
  //组件的位置信息
  TComPosition = record
    cmbParameter_Left:Integer;
    cmbParameter_Top:Integer;
    cmbParameter_Width:Integer;
    cmbParameter_Height:Integer;

    cmbCondition_Left:Integer;
    cmbCondition_Top:Integer;
    cmbCondition_Width:Integer;
    cmbCondition_Height:Integer;

    edtParameter_Left:integer;
    edtParameter_Top:integer;
    edtParameter_Width:integer;
    edtParameter_Height:integer;

    btnAddChild_Left:Integer;
    btnAddChild_Top:Integer;
    btnAddChild_Width:Integer;
    btnAddChild_Height:Integer;

    btnAddParent_Left:Integer;
    btnAddParent_Top:Integer;
    btnAddParent_Width:Integer;
    btnAddParent_Height:Integer;

    btnDelete_Left:Integer;
    btnDelete_Top:Integer;
    btnDelete_Width:Integer;
    btnDelete_Height:Integer;    

  end;
  TfrmProductCardParameters = class(TForm)
    qryProductCardParameters: TADOQuery;
    dsProductCardParameters: TDataSource;
    adoExecSQL: TADOQuery;
    Splitter1: TSplitter;
    GroupBox1: TGroupBox;
    Panel1: TPanel;
    Panel3: TPanel;
    sb1: TStatusBar;
    Panel4: TPanel;
    btnClose: TSpeedButton;
    btnInit: TSpeedButton;
    btnDelete: TSpeedButton;
    btnEdit: TSpeedButton;
    btnQuery: TSpeedButton;
    btnAppend: TSpeedButton;
    RdoSurfeit: TRadioGroup;
    dxDBGrid1: TdxDBGrid;
    dxDBGrid1Column1: TdxDBGridColumn;
    dxDBGrid1Column7: TdxDBGridCheckColumn;
    dxDBGrid1Column2: TdxDBGridColumn;
    dxDBGrid1Column8: TdxDBGridColumn;
    dxDBGrid1Column3: TdxDBGridColumn;
    dxDBGrid1Column6: TdxDBGridColumn;
    dxDBGrid1Column4: TdxDBGridColumn;
    dxDBGrid1Column5: TdxDBGridColumn;
    GroupBox2: TGroupBox;
    scb1: TScrollBox;
    Panel2: TPanel;
    Label3: TLabel;
    Label2: TLabel;
    Label1: TLabel;
    edtParameter1: TEdit;
    cmbParameter1: TComboBox;
    cmbCondition1: TComboBox;
    chkIsActive: TCheckBox;
    btnAddParent1: TBitBtn;
    btnAddChild1: TBitBtn;
    procedure btnAppendClick(Sender: TObject);
    procedure btnDeleteClick(Sender: TObject);
    procedure btnInitClick(Sender: TObject);
    procedure btnCloseClick(Sender: TObject);
    procedure btnQueryClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure btnAddParent1Click(Sender: TObject);
    procedure btnAddChild1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure dxDBGrid1DblClick(Sender: TObject);
    procedure btnEditClick(Sender: TObject);
   
  private
    procedure initComponent;                                           //初始化
    procedure GetParameterInfo(v1:string=‘‘);                          //获取信息,可以查询哪一个参数
    function GetParameter_eng(iIndex:integer):string;                  //返回参数英文名称
    function GetSurfeit(iIndex:integer):string;                        //返回过软方式
    function GetParameter_ref(iIndex:integer):string;                  //返回关系,大于,等于...
   
    function GetServerTime():String;                                   //获取服务器时间
    procedure GetComponentPosition();                                  //获取控件的位置信息

    procedure AddTerm(Term_T:String;Sender: TObject);                  //增加子/父按钮
    procedure FillComboBox(cmb: TComboBox; itype:Byte);                //赋值
    procedure CreateComboBox(idNo:Byte; type_id, L, T, W, H:integer);  //创建ComBobox组件
    procedure CreateEdit(idNo:Byte; type_id, L, T, W, H:integer);      //创建Edit组件
    procedure Createbtn(idNo:Byte; type_id, L, T, W, H:integer);       //创建Button组件
    procedure FreeUI(Sender: TObject);                                 //点击删除组件
    procedure FreeAllUI();                                             //清除所有组件
    function FindControl(ParentControl: TWinControl;
                         FindControlName: string): TControl;           //查找组件

    procedure MoveControlButton(Sender:TObject);virtual;               //移动组件
    function GetTermMaxTop: Integer;                                   //获取最大高度
    function iif(Expression:Boolean;ValueTrue:Variant;
                 ValueFalse:Variant): Variant; overload;
    function GetParentComboBox(cbb: TComboBox): TComboBox;             //取父条件
    function IsOneParameter(v_Para:string):boolean;                    //判断是否只有一个参数时
    function GetParameterTop(v_Para:string):integer;                   //获取scb1中第一个高度
    function CheckEdtParameter(type_id:byte): Integer;                 //检查是否都输入了值
    procedure DBGridEdit(Sender: TObject);                             //双击DBGrid时,弹出修改SQL语句的框框
    { Private declarations }
  public
    //这个全局变量双击DBGrid时传递给令一窗体的Memo (单元文件:SurfeitParaModified)
    g_SQLSurfeit:string;
    g_chkIsActive:boolean;
    { Public declarations }
  end;

var
  frmProductCardParameters: TfrmProductCardParameters;
  recComPosition :TComPosition;
  icmbParameter, icmbCondition, iEdt, iParentbtn, iChidbtn, ibtnDelete, iNum: integer;                //控件的序号值 从 1 开始
  strCondition: array of string;                                        //保存产生的条件语句

//  iTop:Integer;
implementation

uses Main, SurfeitParaModified;

{$R *.dfm}

{ TfrmProductCardParameters }

//初始化函数
procedure TfrmProductCardParameters.initComponent;
var
  i:integer;
begin
  for i:=0 to Self.ComponentCount-1 do
   begin
     if Self.Components[i] is TEdit then (Self.Components[i] as TEdit).Clear;
     if Self.Components[i] is TComboBox then (Self.Components[i] as TComboBox).ItemIndex := 0;
     if Self.Components[i] is TRadioGroup then (Self.Components[i] as TRadioGroup).ItemIndex := -1;
   end;
end;

//查询函数
procedure TfrmProductCardParameters.GetParameterInfo(v1: string=‘‘);
var
  strSQL: string;
begin
  strSQL := ‘SELECT * FROM pdProductCardParameters WHERE 1=1 ‘;

  if v1 <> ‘‘ then
    strSQL := strSQL + ‘ AND Parameters_chs=‘‘‘ + v1 +‘‘‘‘;

  with qryProductCardParameters do
  begin
    Close;
    SQL.Clear;
    SQL.Add(strSQL);
    Open;
  end;

end;

//新增
procedure TfrmProductCardParameters.btnAppendClick(Sender: TObject);
var
  strSQL: string;

  iIndex:Byte;
  i, j, iTop, k:Integer;
  id_Name, sPar, sOper, sParValue, SQLCondition, SQLCondition_c:string;
  cbb, cppParent: TComboBox;
  bTag, bSQLCondition: boolean;
  strSurfeit:string;                    //过软类型
begin

  //检查条件
  if RdoSurfeit.ItemIndex = -1 then
  begin
    Application.Messagebox(Pchar(‘请选择过软类型!‘),Pchar(‘提示‘),Mb_IconInforMation+MB_OK);
    RdoSurfeit.SetFocus;
    exit;
  end;

  if cmbParameter1.ItemIndex=-1 then
  begin
    Application.Messagebox(Pchar(‘请选择参数名称!‘),Pchar(‘提示‘),Mb_IconInforMation+MB_OK);
    cmbParameter1.SetFocus;
    exit;
  end;

  if cmbCondition1.ItemIndex=-1 then
  begin
    Application.Messagebox(Pchar(‘请选择参数条件!‘),Pchar(‘提示‘),Mb_IconInforMation+MB_OK);
    cmbCondition1.SetFocus;
    exit;
  end;

  if edtParameter1.Text=‘‘ then
  begin
    Application.Messagebox(Pchar(‘请输入参数内容!‘),Pchar(‘提示‘),Mb_IconInforMation+MB_OK);
    edtParameter1.SetFocus;
    exit;
  end;

  //参数名称
  iIndex := cmbParameter1.ItemIndex;

  strSurfeit := GetSurfeit(RdoSurfeit.ItemIndex);                //过软类型

  sPar := GetParameter_eng(cmbParameter1.itemIndex);             //获取参数英文名称
  sOper := GetParameter_ref(cmbCondition1.itemIndex);            //获取操作符
  sParValue:=edtParameter1.Text;                                 //获取实际参数内容

  //组合可视化的组件条件语句
  SQLCondition := ‘(‘ + sPar + ‘ ‘ + sOper +‘ ‘‘‘ + iif(pos(‘LIKE‘,sOper)>0, ‘%‘, ‘‘) +
                  sParValue+iif(pos(‘LIKE‘,sOper)>0, ‘%‘, ‘‘) + ‘‘‘) or‘;


  //如果有参数控件,检查是否都输入了内容
  if IsOneParameter(‘edtParameter‘) then
  begin
    k := CheckEdtParameter(0);
    if k <> -1 then
    begin
      Application.Messagebox(Pchar(‘请输入参数内容!‘),Pchar(‘提示‘),Mb_IconInforMation+MB_OK);
//      TEdit(‘edtParameter‘+inttostr(k)).SetFocus;
      exit;
    end;

  end;

  //属于可视化的组件下父条件是否有子条件 (子条件动态产生)  
  if not IsOneParameter(‘cmbParameter‘) then                                                     //true为存在, scb1内没有参数
  begin
    for j:=0 to scb1.ControlCount - 1 do                                                          //遍历子条件
    begin
       if LeftStr(Scb1.Controls[j].Name, Length(‘cmbCondition‘))=‘cmbCondition‘ then              //条件组件
       begin
         cbb:=TComboBox(Scb1.Controls[j]);
         id_Name:=RightStr(cbb.Name, Length(cbb.Name)-Length(‘cmbCondition‘));                    //取出序号
         sPar := GetParameter_eng(cmbParameter1.itemIndex);                                       //哪个字段
         sOper := GetParameter_ref(cbb.itemIndex);                                                //哪个操作符号
         sParValue:=TEdit(FindControl(Scb1, ‘edtParameter‘+id_Name)).Text;                        //值

         SQLCondition := SQLCondition +  ‘ (‘ +
                                              sPar + ‘ ‘ + sOper +‘ ‘‘‘ + iif(pos(‘LIKE‘,sOper)>0, ‘%‘, ‘‘) +
                                              sParValue+iif(pos(‘LIKE‘,sOper)>0, ‘%‘, ‘‘) + ‘‘‘) or‘;
       end;
    end;
    SQLCondition := Copy(SQLCondition, 1, Length(SQLCondition)-2) + ‘ ‘;                            //去掉最后面的 or
  end
  else
  begin
    iTop := GetParameterTop(‘cmbParameter‘);
    for j:=0 to scb1.ControlCount - 1 do                                                          //遍历子条件
    begin
       if (LeftStr(Scb1.Controls[j].Name, Length(‘cmbCondition‘))=‘cmbCondition‘) and             //条件组件
          (Scb1.Controls[j].Top<iTop) then
       begin
         cbb:=TComboBox(Scb1.Controls[j]);
         id_Name:=RightStr(cbb.Name, Length(cbb.Name)-Length(‘cmbCondition‘));                    //取出序号
         sPar := GetParameter_eng(cmbParameter1.itemIndex);                                       //哪个字段
         sOper := GetParameter_ref(cbb.itemIndex);                                                //哪个操作符号
         sParValue:=TEdit(FindControl(Scb1, ‘edtParameter‘+id_Name)).Text;                        //值

         SQLCondition := SQLCondition +  ‘ (‘ +
                                              sPar + ‘ ‘ + sOper +‘ ‘‘‘ + iif(pos(‘LIKE‘,sOper)>0, ‘%‘, ‘‘) +
                                              sParValue+iif(pos(‘LIKE‘,sOper)>0, ‘%‘, ‘‘) + ‘‘‘) or‘;
       end;
    end;
    SQLCondition := Copy(SQLCondition, 1, Length(SQLCondition)-2) + ‘ ‘;                            //去掉最后面的 or

  end;


  //下面开始循环处理动态产生的所有组件并组合SQL语句
//  SQLCondition := ‘‘;
  for i:=0 to scb1.ControlCount - 1 do                                                              //遍历父条件
  begin                               
    if LeftStr(Scb1.Controls[i].Name, Length(‘cmbParameter‘))=‘cmbParameter‘ then                   //是参数组件
    begin
      if SQLCondition <> ‘‘ then
        SQLCondition := SQLCondition + ‘ AND ‘;                                                     //下一个参数时,是并列关系

      for j:=0 to scb1.ControlCount - 1 do                                                          //遍历子条件,是 or 的关系
      begin
        if LeftStr(Scb1.Controls[j].Name, Length(‘cmbCondition‘))=‘cmbCondition‘ then
        begin
          cbb:=TComboBox(Scb1.Controls[j]);
          cppParent:=GetParentComboBox(cbb);                                                         //查找这个子条件所属的父条件
          if cppParent.Name = Scb1.Controls[i].Name then                                             //属于这个父条件的子条件
          begin
            id_Name:=RightStr(cbb.Name, Length(cbb.Name)-Length(‘cmbCondition‘));                    //取出序号
            sPar := GetParameter_eng(cppParent.itemIndex);                                           //哪个字段
            sOper := GetParameter_ref(cbb.itemIndex);                                                //哪个操作符号
            sParValue:=TEdit(FindControl(Scb1, ‘edtParameter‘+id_Name)).Text;                        //值

            SQLCondition := SQLCondition +  ‘ (‘ +
                                              sPar + ‘ ‘ + sOper +‘ ‘‘‘ + iif(pos(‘LIKE‘,sOper)>0, ‘%‘, ‘‘) +
                                              sParValue+iif(pos(‘LIKE‘,sOper)>0, ‘%‘, ‘‘) + ‘‘‘) or‘;
          end;
        end;
      end;

      SQLCondition := Copy(SQLCondition, 1, Length(SQLCondition)-2) + ‘ ‘;                            //去掉最后面的 or

    end;

  end;   

  //生成最终插入数据库的语句
  SQLCondition := ‘ WHEN ‘ + SQLCondition + ‘ THEN ‘‘‘ + strSurfeit + ‘‘‘ ‘;

//  ShowMessage(SQLCondition);
//  exit;

  //更新进资料库

                      
  strSQL := ‘INSERT INTO pdProductCardParameters([SQLCondition], [IsActive], [Surfeit]) ‘ +
            ‘ VALUES(:SQLCondition, :IsActive, :Surfeit) ‘;

  with qryProductCardParameters do
  begin
    Close;
    SQL.Clear;
    SQL.Add(strSQL);

    Parameters.ParamByName(‘SQLCondition‘).Value := SQLCondition;                       //SQL条件语句
//    Parameters.ParamByName(‘Oper_Date‘).Value :=  GetServerTime()                     //操作日期,改为由DB Default值GetDate()
    Parameters.ParamByName(‘IsActive‘).Value := chkIsActive.Checked;                    //是否启用
    Parameters.ParamByName(‘Surfeit‘).Value := strSurfeit;                              //过软类型
    ExecSQL;
  end;


  initComponent;              //清空组件内容
  cmbParameter1.ItemIndex := iIndex;
  GetParameterInfo(‘‘);
 
  ShowMessage(‘新增成功!‘);
  sb1.SimpleText := ‘ 新增成功...‘;

end;

//取字段英文名称
function TfrmProductCardParameters.GetParameter_eng(
  iIndex: integer): string;
var
  strRetVal:string;
begin
//1、染色方式??PdBatchHead.Dye_Method
//2、流程?? artdb..rtproductartinfo?operation
//3、整理方式? artdb..rtproductartinfo Finishing_Desc
//4、纱类?.? ArtDB..rtRawArtInfo??yarn_type
//5、布种? ArtDB..rtRawArtInfo??G.Construction_Desc
//6、克重? artdb..rtproductartinfo E.Product_Gmmm
//7、客户? SystemDB..pbCustomerList customer
//8、码重? ArtDB..rtRawArtInfo??Ozyd
//9、门幅? ArtDB..rtRawArtInfo width
//10、色号? FinishingDB.dbo.PdBatchHead color_code
  strRetVal := ‘‘;

  case iIndex of
//    0: strRetVal := ‘PdBatchHead.Dye_Method‘;
//    1: strRetVal := ‘rtproductartinfo.operation‘;
//    2: strRetVal := ‘rtproductartinfo.Finishing_Desc‘;
//    3: strRetVal := ‘rtRawArtInfo.yarn_type‘;
//    4: strRetVal := ‘rtRawArtInfo.Construction_Desc‘;
//    5: strRetVal := ‘rtproductartinfo.Product_Gmmm‘;
//    6: strRetVal := ‘pbCustomerList.customer‘;
//    7: strRetVal := ‘rtRawArtInfo.Ozyd‘;
//    8: strRetVal := ‘rtRawArtInfo.width‘;
//    9: strRetVal := ‘PdBatchHead.color_code‘;
    0: strRetVal := ‘Dye_Method‘;
    1: strRetVal := ‘operation‘;
    2: strRetVal := ‘Finishing_Desc‘;
    3: strRetVal := ‘yarn_type‘;
    4: strRetVal := ‘Construction_Desc‘;
    5: strRetVal := ‘Product_Gmmm‘;
    6: strRetVal := ‘customer‘;
    7: strRetVal := ‘Ozyd‘;
    8: strRetVal := ‘width‘;
    9: strRetVal := ‘color_code‘;
  end;

  Result := strRetVal;
 
end;

//返回符号
function TfrmProductCardParameters.GetParameter_ref(
  iIndex: integer): string;
var
  strRetVal:string;
begin
//包含
//不包含
//等于
//大于
//小于
//大于等于
//小于等于
//不等于

  strRetVal := ‘‘;

  case iIndex of
    0: strRetVal := ‘LIKE‘;
    1: strRetVal := ‘NOT LIKE‘;
    2: strRetVal := ‘=‘;
    3: strRetVal := ‘>‘;
    4: strRetVal := ‘<‘;
    5: strRetVal := ‘>=‘;
    6: strRetVal := ‘<=‘;
  end;

  Result := strRetVal;

end;

//删除
procedure TfrmProductCardParameters.btnDeleteClick(Sender: TObject);
var
  strSQL: string;
begin

  if Application.Messagebox(Pchar(‘你确定要删除这个参数条件吗?‘),Pchar(‘警告‘),MB_ICONWARNING+MB_YesNo) = IDNo then exit;

  strSQL := ‘DELETE FROM pdProductCardParameters WHERE Iden=:Iden ‘;

  with adoExecSQL do
  begin
    Close;
    SQL.Clear;
    SQL.Add(strSQL);
    Parameters.ParamByName(‘Iden‘).Value := qryProductCardParameters.fieldbyname(‘Iden‘).AsInteger;  //.FieldValues[‘Iden‘].AsInteger;
    ExecSQL;
  end;

  initComponent;
  GetParameterInfo(‘‘);

  ShowMessage(‘删除成功!‘);
  sb1.SimpleText := ‘ 删除成功...‘;

end;


//清空
procedure TfrmProductCardParameters.btnInitClick(Sender: TObject);
begin
  FreeAllUI();
  initComponent;
  qryProductCardParameters.Active := false;

end;

//退出
procedure TfrmProductCardParameters.btnCloseClick(Sender: TObject);
begin
  close;
end;

//查询
procedure TfrmProductCardParameters.btnQueryClick(Sender: TObject);
begin
//  GetParameterInfo(Trim(cmbParameter1.Text));
  GetParameterInfo(‘‘);
  sb1.SimpleText := ‘ 查询成功...‘;

end;

//初始化
procedure TfrmProductCardParameters.FormCreate(Sender: TObject);
begin


  initComponent;
  GetParameterInfo(‘‘);

  GetComponentPosition();               //获取控件的位置信息

end;

//获取服务器时间,如果出现错误取本机时间
function TfrmProductCardParameters.GetServerTime: String;
var
  adoqry: TADOQuery;
  strRetVal:string;
begin

  strRetVal := ‘‘;

  adoqry := TADOQuery.Create(Self);
  try
    with adoqry do
    begin
      try
        Close;
        SQL.Clear;
        SQL.Add(‘SELECT CONVERT(NVARCHAR(20), GETDATE(), 120) AS DT1‘);
        Open;

        strRetVal := Trim(fieldbyname(‘DT1‘).AsString);

      except
        on E:Exception do
        begin
          strRetVal := FormatDateTime(‘yyyy-mm-dd hh:nn:ss‘, Now());
          Application.Messagebox(Pchar(‘获取服务器时间出现错误,将使用本机时间!‘),Pchar(‘提示‘),MB_ICONWARNING+MB_OK);
        end;
      end;
    end;
  finally
    adoqry.Free;
  end;

  Result := strRetVal;


end;


//获取控件的初始化信息
procedure TfrmProductCardParameters.GetComponentPosition;
begin
  icmbParameter := 1;
  icmbCondition := 1;
  iEdt := 1;
  iParentbtn := 1;
  iChidbtn := 1;
  ibtnDelete := 1;
  iNum := 1;

  recComPosition.cmbParameter_Left := cmbParameter1.Left;
  recComPosition.cmbParameter_Top := cmbParameter1.Top;
  recComPosition.cmbParameter_Width := cmbParameter1.Width;
  recComPosition.cmbParameter_Height := cmbParameter1.Height;

  recComPosition.cmbCondition_Left := cmbCondition1.Left;
  recComPosition.cmbCondition_Top := cmbCondition1.Top;
  recComPosition.cmbCondition_Width := cmbCondition1.Width;
  recComPosition.cmbCondition_Height := cmbCondition1.Height;

  recComPosition.edtParameter_Left := edtParameter1.Left;
  recComPosition.edtParameter_Top := edtParameter1.Top;
  recComPosition.edtParameter_Width := edtParameter1.Width;
  recComPosition.edtParameter_Height := edtParameter1.Height;

  recComPosition.btnAddChild_Left := btnAddChild1.Left;
  recComPosition.btnAddChild_Top := btnAddChild1.Top;
  recComPosition.btnAddChild_Width := btnAddChild1.Width;
  recComPosition.btnAddChild_Height := btnAddChild1.Height;

  recComPosition.btnAddParent_Left := btnAddParent1.Left;
  recComPosition.btnAddParent_Top := btnAddParent1.Top;
  recComPosition.btnAddParent_Width := btnAddParent1.Width;
  recComPosition.btnAddParent_Height := btnAddParent1.Height;

  recComPosition.btnDelete_Left := btnAddParent1.Left + btnAddParent1.Width + 30;
  recComPosition.btnDelete_Top := btnAddParent1.Top;
  recComPosition.btnDelete_Width := btnAddParent1.Width;
  recComPosition.btnDelete_Height := btnAddParent1.Height;
end;

procedure TfrmProductCardParameters.AddTerm(Term_T: String;
  Sender: TObject);
var
  cmbParameter, cmbCondition: TComboBox;
  edtParameter: TEdit;
  btnAddChild, btnAddParent,btnDelete: TButton;
  iTop: integer;
begin
  if Term_T = ‘F‘ then     //父条件
  begin
    //参数名称
    iTop := GetTermMaxTop();
    if iTop = 0 then
    Inc(iTop, 15);
    Inc(iTop, 30);

//    icmbParameter := icmbParameter+1;
    iNum := iNum + 1;
//    recComPosition.cmbParameter_Top := recComPosition.cmbParameter_Top + 30;
    CreateComboBox(iNum,
                   0,
                   recComPosition.cmbParameter_Left,
//                   recComPosition.cmbParameter_Top,
                   iTop,
                   recComPosition.cmbParameter_Width,
                   recComPosition.cmbParameter_Height);
   //条件
//    recComPosition.cmbCondition_Top := recComPosition.cmbCondition_Top + 30;
//    icmbCondition := icmbCondition+1;
    CreateComboBox(iNum,
                   1,
                   recComPosition.cmbCondition_Left,
//                   recComPosition.cmbCondition_Top,
                   iTop,
                   recComPosition.cmbCondition_Width,
                   recComPosition.cmbCondition_Height);  
    //参数内容
//    recComPosition.edtParameter_Top := recComPosition.edtParameter_Top + 30;
//    iEdt := iEdt+1;
    CreateEdit(    iNum,
                   0,
                   recComPosition.edtParameter_Left,
//                   recComPosition.edtParameter_Top,
                   iTop,
                   recComPosition.edtParameter_Width,
                   recComPosition.edtParameter_Height);


    //子按钮
//    recComPosition.btnAddChild_Top := recComPosition.btnAddChild_Top + 30;
//    iChidbtn := iChidbtn+1;
    Createbtn(     iNum,
                   0,
                   recComPosition.btnAddChild_Left,
//                   recComPosition.btnAddChild_Top,
                   iTop,
                   recComPosition.btnAddChild_Width,
                   recComPosition.btnAddChild_Height);

    //父按钮
//    recComPosition.btnAddParent_Top := recComPosition.btnAddParent_Top + 30;
//    iParentbtn := iParentbtn+1;
    Createbtn(     iNum,
                   1,
                   recComPosition.btnAddParent_Left,
//                   recComPosition.btnAddParent_Top,
                   iTop,
                   recComPosition.btnAddParent_Width,
                   recComPosition.btnAddParent_Height);

    //删除
//    recComPosition.btnDelete_Top := recComPosition.btnDelete_Top + 30;
//    ibtnDelete := ibtnDelete + 1;
    Createbtn(     iNum,
                   2,
                   recComPosition.btnDelete_Left,
//                   recComPosition.btnDelete_Top,
                   iTop,
                   recComPosition.btnDelete_Width,
                   recComPosition.btnDelete_Height);   

  end
  else if Term_T = ‘C‘ then    //子条件
  begin
   //条件
    iNum := iNum + 1;
//    recComPosition.cmbCondition_Top := recComPosition.cmbCondition_Top + 30;
//    icmbCondition := icmbCondition+1;
    CreateComboBox(iNum,
                   1,
                   recComPosition.cmbCondition_Left,
//                   recComPosition.cmbCondition_Top,
                   TButton(Sender).Top + 30,
                   recComPosition.cmbCondition_Width,
                   recComPosition.cmbCondition_Height);
    //参数内容
//    recComPosition.edtParameter_Top := recComPosition.edtParameter_Top + 30;
//    iEdt := iEdt+1;
    CreateEdit(    iNum,
                   0,
                   recComPosition.edtParameter_Left,
//                   recComPosition.edtParameter_Top,
                   TButton(Sender).Top + 30,
                   recComPosition.edtParameter_Width,
                   recComPosition.edtParameter_Height);
    //删除
//    recComPosition.btnDelete_Top := recComPosition.btnDelete_Top + 30;  
//    ibtnDelete := ibtnDelete + 1;
    Createbtn(     iNum,
                   2,
                   recComPosition.btnDelete_Left,
//                   recComPosition.btnDelete_Top,
                   TButton(Sender).Top + 30,
                   recComPosition.btnDelete_Width,
                   recComPosition.btnDelete_Height);                    
  end;
end;


//添加父条件
procedure TfrmProductCardParameters.btnAddParent1Click(Sender: TObject);
begin
  AddTerm(‘F‘, Sender);
end;

//填充ComBobox
procedure TfrmProductCardParameters.FillComboBox(cmb: TComboBox;
  itype: Byte);
var
  i: integer;

  const cmbParameter: array[0..9] of string = (
                                                ‘染色方式‘,
                                                ‘流程‘,
                                                ‘整理方式‘,
                                                ‘纱类‘,
                                                ‘布种‘,
                                                ‘克重‘,
                                                ‘客户‘,
                                                ‘码重‘,
                                                ‘门幅‘,
                                                ‘色号‘);

  const cmbCondition: array[0..7] of string = (
                                                ‘包含‘,
                                                ‘不包含‘,
                                                ‘等于‘,
                                                ‘大于‘,
                                                ‘小于‘,
                                                ‘大于等于‘,
                                                ‘小于等于‘,
                                                ‘不等于‘
                                                  );
begin
  if itype = 0 then
  begin
    cmb.Clear;
    for i:=Low(cmbParameter) to High(cmbParameter) do
        cmb.Items.Add(cmbParameter[i])
  end
  else if itype =1 then
  begin
    cmb.Clear;
    for i:=Low(cmbCondition) to High(cmbCondition) do
        cmb.Items.Add(cmbCondition[i]) 
  end;

end;

//创建ComBobox
procedure TfrmProductCardParameters.CreateComboBox(idNo: Byte; type_id, L,
  T, W, H: integer);
var
  strName: string;
  cbb: TComboBox;
begin
  if type_id =0 then
  begin
    strName := ‘cmbParameter‘
  end
  else if type_id = 1 then
  begin
    strName := ‘cmbCondition‘
  end;
//  ShowMessage(strName);
  cbb := TComboBox.Create(Self);
  cbb.Parent := Self.scb1;
  cbb.Name := strName + IntToStr(idNo);
  cbb.Style := csDropDownList;
  FillComboBox(cbb, type_id);
  cbb.ItemIndex := 0;
//  cbb.Text := strName + IntToStr(idNo);
  cbb.Left := L;
  cbb.Top := T;
  cbb.Width := W;
  cbb.Height := H;
  cbb.Visible := true;
end;

//创建Edit
procedure TfrmProductCardParameters.CreateEdit(idNo: Byte; type_id, L, T,
  W, H: integer);
var  
  strName: string;
  edt: TEdit;
begin
  if type_id =0 then
  begin
    strName := ‘edtParameter‘
  end
  else if type_id = 1 then
  begin
    ;
  end;

  edt := TEdit.Create(Self);
  edt.Parent := Self.scb1;
  edt.Name := strName + IntToStr(idNo);
//  edt.CharCase := ecUpperCase;
  edt.clear;
//  edt.Text := strName + IntToStr(idNo);
  edt.Left := L;
  edt.Top := T;
  edt.Width := W;
  edt.Height := H;
  edt.Visible := true;

end;

//子条件
procedure TfrmProductCardParameters.btnAddChild1Click(Sender: TObject);
begin
  MoveControlButton(Sender);
  AddTerm(‘C‘, Sender);
end;

//创建按钮
procedure TfrmProductCardParameters.Createbtn(idNo: Byte; type_id, L, T, W,
  H: integer);
var
  strName, strCaption: string;
  btn: TButton;
begin          
 
  if type_id =0 then
  begin
    strName := ‘btnAddChild‘;   //子
    strCaption := ‘+子条件‘;
  end
  else if type_id = 1 then     //父
  begin
    strName := ‘btnAddParent‘;
    strCaption := ‘+父条件‘;
  end
  else
  begin
    strName := ‘btnDelete‘;    //删除
    strCaption := ‘删除‘;
  end;

  btn := TButton.Create(Self);
  btn.Parent := Self.scb1;
  btn.Name := strName + IntToStr(idNo); 
  btn.Caption := strCaption;
//  btn.Caption := strName + IntToStr(idNo);
  btn.Left := L;
  btn.Top := T;
  btn.Width := W;
  btn.Height := H;
  btn.Visible := true;

  //设置onclick事件
  if type_id =0 then
  begin
    btn.OnClick := btnAddChild1Click;   //子
  end
  else if type_id = 1 then     //父
  begin
    btn.OnClick := btnAddParent1Click;
  end
  else
  begin
    btn.OnClick := FreeUI;   //删除事件
  end;


end;

//释放
procedure TfrmProductCardParameters.FreeUI(Sender: TObject);
var
  i,j: integer;
  ID_Name: String;
//  obj: TObject;
  obj: TControl;
  itop: integer;
begin

  try
    ID_Name := TButton(Sender).Name;
    ID_Name := rightstr(ID_Name, Length(ID_Name) - Length(‘btndelete‘)); 

    iTop:=0;

    obj := FindControl(scb1, ‘btnAddParent‘+ID_Name);
    if Assigned(obj) then
    begin
      iTop:=iif(iTop<TButton(obj).Top, TButton(obj).Top, iTop);
      TButton(obj).Free;
      obj := nil;
//      FreeAndNil(obj);
  //    recComPosition.btnAddParent_Top := recComPosition.btnAddParent_Top - 30;
    end;

    obj := FindControl(scb1, ‘btnAddChild‘+ID_Name);
    if Assigned(obj) then
    begin
      iTop:=iif(iTop<TButton(obj).Top, TButton(obj).Top, iTop);
      TButton(obj).Free;
      obj := nil;
//      FreeAndNil(obj);
  //    recComPosition.btnAddChild_Top := recComPosition.btnAddChild_Top - 30
    end;

    obj := FindControl(scb1, ‘edtParameter‘+ID_Name);
    if Assigned(obj) then
    begin
      iTop:=iif(iTop<TEdit(obj).Top, TEdit(obj).Top, iTop);
      TEdit(obj).Free;
      obj := nil;
//      FreeAndNil(obj);
  //    recComPosition.edtParameter_Top := recComPosition.edtParameter_Top - 30
    end;

    obj := FindControl(scb1, ‘cmbCondition‘+ID_Name);
    if Assigned(obj) then
    begin
      iTop:=iif(iTop<TComboBox(obj).Top, TComboBox(obj).Top, iTop);
      TComboBox(obj).Free;
      obj := nil;
//      FreeAndNil(obj);
  //    recComPosition.cmbCondition_Top := recComPosition.cmbCondition_Top - 30;
    end;
   
    obj := FindControl(scb1, ‘cmbParameter‘+ID_Name);
    if Assigned(obj) then
    begin
      iTop:=iif(iTop<TComboBox(obj).Top, TComboBox(obj).Top, iTop);
      TComboBox(obj).Free;
      obj := nil;
//      FreeAndNil(obj);
  //    recComPosition.cmbParameter_Top := recComPosition.cmbParameter_Top - 30;
    end;

    obj := FindControl(scb1, ‘btnDelete‘+ID_Name);
    if Assigned(obj) then
    begin
  //    if Self.ActiveControl is TButton  then
       //(Self.ActiveControl AS TButton).free;
      iTop:=iif(iTop<TButton(obj).Top, TButton(obj).Top, iTop);
      TButton(obj).Free;
      obj := nil;
//      FreeAndNil(obj);
  //    recComPosition.btnDelete_Top := recComPosition.btnDelete_Top - 30;
    end;

    //移动组件到上面30
    for i:=Scb1.ControlCount-1 downto 0 do
    begin
      if Scb1.Controls[i].Top>iTop then
  //        Scb1.Controls[i].Top := Scb1.Controls[i].Top-30
        Scb1.Controls[i].Top:=iif(Scb1.Controls[i].Top-30<iTop, iTop, Scb1.Controls[i].Top-30);
    end;
  except
    on e: exception do
      ShowMessage(e.Message);
  end;
end;

//查找组件
function TfrmProductCardParameters.FindControl(ParentControl: TWinControl;
  FindControlName: string): TControl;
var
  i:Integer;
  obj:TControl;
begin

  obj:=nil;

  if not  Assigned(ParentControl) then exit;
  for i:=ParentControl.ControlCount-1 downto 0 do
  begin
    if ParentControl.Controls[i].Name=FindControlName then
    begin
      obj:=ParentControl.Controls[i];
      Break;
    end;
  end;

  Result := obj;

end;

//关闭窗体时
procedure TfrmProductCardParameters.FormDestroy(Sender: TObject);
begin
  frmProductCardParameters := nil;
end;

procedure TfrmProductCardParameters.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  qryProductCardParameters.Active := false;
  Action := caFree;
end;

//新增子条件时移动组件位置
procedure TfrmProductCardParameters.MoveControlButton(Sender: TObject);
var
  iTop:Integer;
  i:Integer;
begin
  if not Assigned(sender) then Exit;
  if Sender = btnAddChild1 then
    for i:=Scb1.ControlCount-1 downto 0 do
      Scb1.Controls[i].Top:=Scb1.Controls[i].Top+30
  else
  begin
    iTop:=TButton(Sender).Top;
    for i:=Scb1.ControlCount-1 downto 0 do
    begin
      if  Scb1.Controls[i].Top>iTop then
      begin
        Scb1.Controls[i].Top:=Scb1.Controls[i].Top+30;
      end;
    end;
  end;

end;

//取组件的最大高度
function TfrmProductCardParameters.GetTermMaxTop: Integer;
var
  i:Integer;
  iRetVal: Integer;
begin
//  iRetVal:=0;
//  for i:=scb1.ControlCount-1 downto 0 do
//  begin
//    if scb1.Controls[i].Top>iRetVal then iRetVal := scb1.Controls[i].Top
//    else
//      iRetVal := iRetVal;
//  end;
////
//  Result:= iRetVal;

  Result:=0;
  for i:=0 to Scb1.ControlCount-1 do
  begin
    Result:=iif(Scb1.Controls[i].Top>Result, Scb1.Controls[i].Top, Result);
  end;

end;

//自定义三目函数
function TfrmProductCardParameters.iif(Expression: Boolean; ValueTrue,
  ValueFalse: Variant): Variant;
begin
  if Expression then
    Result:=ValueTrue
  else
    Result:=ValueFalse;
end;

//取父条件
function TfrmProductCardParameters.GetParentComboBox(
  cbb: TComboBox): TComboBox;
var i:Integer;
    obj:TControl;
    DiffTop:Integer;
begin
  DiffTop:=999999;
  Result:=cmbParameter1;
  for i:=0 to Scb1.ControlCount-1 do
  begin
    obj:=Scb1.Controls[i];
    if (obj is TComboBox) and (LeftStr(obj.Name, length(‘cmbParameter‘))=‘cmbParameter‘) then
    begin
      if (obj.Top<=cbb.top) and (cbb.Top-obj.Top<DiffTop) then
      begin
        Result:=TComboBox(obj);
        DiffTop:=cbb.Top-obj.Top
      end;
    end;
  end;

end;

//返回过软方式
function TfrmProductCardParameters.GetSurfeit(iIndex: integer): string;
var
  strRetVal:string;
begin

  strRetVal := ‘‘;

  case iIndex of

    0: strRetVal := ‘缸内过软‘;
    1: strRetVal := ‘缸外过软‘;
    2: strRetVal := ‘连续皂洗‘;
    3: strRetVal := ‘‘;

  end;

  Result := strRetVal;

end;

//不只一个字段,存在返回true
function TfrmProductCardParameters.IsOneParameter(v_Para: string): boolean;
var
  i: integer;
  bRetval:Boolean;
begin

  bRetval := false;

  for i:=0 to scb1.ControlCount - 1 do
  begin
    if LeftStr(Scb1.Controls[i].Name, Length(v_Para)) = v_Para then
    begin
      bRetval := true;
      Break;
    end;
  end;

  Result := bRetval;

end;

//获取scb1中的第一个参数高度
function TfrmProductCardParameters.GetParameterTop(
  v_Para: string): integer;
var
  i, iTop: integer;
begin

//  iTop := 0;

  //先取一个
  for i:=0 to scb1.ControlCount - 1 do
  begin
    if LeftStr(Scb1.Controls[i].Name, Length(v_Para)) = v_Para then
    begin
      iTop := Scb1.Controls[i].Top;
      Break;
    end;
  end;

  //循环对比
  for i:=0 to scb1.ControlCount - 1 do
  begin
    if LeftStr(Scb1.Controls[i].Name, Length(v_Para)) = v_Para then
    begin     
      iTop := iif(iTop < Scb1.Controls[i].Top, iTop, Scb1.Controls[i].Top);
    end;
  end;

  Result := iTop;
 
end;

//清除所有组件
procedure TfrmProductCardParameters.FreeAllUI();
var
  i:Integer;
begin
//  ShowMessage(IntToStr(scb1.ControlCount));exit;
  for i:=Self.scb1.ControlCount-1 downto 0 do
  begin
    if (Self.scb1.Controls[i] is TComboBox) then (Self.scb1.Controls[i] as TComboBox).Free
    else if (Self.scb1.Controls[i] is TButton) then (Self.scb1.Controls[i] as TButton).Free
    else if (Self.scb1.Controls[i] is TEdit) then (Self.scb1.Controls[i] as TEdit).Free;
  end;

end;

//检查所有Edit是否为空
function TfrmProductCardParameters.CheckEdtParameter(
  type_id: byte): Integer;
var
  i:Integer;
begin

  i := 9999;

  if type_id = 0 then
  begin

    for i:=Self.scb1.ControlCount-1 downto 0 do
    begin
      if (scb1.Controls[i] is TEdit) and (LeftStr(scb1.Controls[i].Name, length(‘edtParameter‘))=‘edtParameter‘) then
      begin
        if TEdit(scb1.Controls[i]).Text = ‘‘ then
        begin
//          i := StrToInt(RightStr(scb1.Controls[i].Name, Length(scb1.Controls[i].Name)-length(‘edtParameter‘)));
          Break;
        end;
      end;
    end;
  end;
 
  Result := i;

end;

//双击DBGrid时,弹出修改SQL语句的框框
procedure TfrmProductCardParameters.DBGridEdit(Sender: TObject);
var
  strSQL:string;
  iIden, iRetval:integer;
  adoqrySurfeit:TADOQuery;
begin
  if ((not qryProductCardParameters.Active) or qryProductCardParameters.IsEmpty) then
  begin
    Application.Messagebox(Pchar(‘没有记录或当前数据集没有激活,请重新查询后操作!‘),Pchar(‘提示‘),Mb_IconInforMation+MB_OK);
    exit;
  end;

  iIden := qryProductCardParameters.FieldByName(‘Iden‘).AsInteger;
  g_SQLSurfeit := qryProductCardParameters.FieldByName(‘SQLCondition‘).AsString;
  g_chkIsActive := qryProductCardParameters.FieldByName(‘IsActive‘).AsBoolean;
 
  //弹出修改SQL语句的框框
  iRetval := TfrmSurfeitParaModified.Create(Application).ShowModal;

  if iRetval = mrOK then
  begin
                      
    adoqrySurfeit := TADOQuery.Create(Self);
    try
      with adoqrySurfeit do
      begin
        Connection := fmMain.adocntFinishing;
        Close;
        SQL.Clear;
        SQL.Add(‘UPDATE pdProductCardParameters SET SQLCondition=:SQLCondition,IsActive=:IsActive WHERE IDen=:IDen ‘);
        Parameters.ParamByName(‘SQLCondition‘).Value := g_SQLSurfeit;
        Parameters.ParamByName(‘IsActive‘).Value := g_chkIsActive;
        Parameters.ParamByName(‘IDen‘).Value := iIden;
       
        try
  //        Active := true;
          ExecSQL;
        except
          on e:Exception do
            begin
              Application.Messagebox(Pchar(‘请注意!本次修改SQL参数不成功!‘+e.Message),Pchar(‘提示‘),
                  MB_ICONWARNING+MB_OK);
              exit;
            end;
        end;       
      end;                        
    finally
      adoqrySurfeit.Free;
    end;
  end;

  //重新载入查询
  GetParameterInfo(‘‘);
  sb1.SimpleText := ‘ 查询成功...‘;

end;

//双击DBGrid
procedure TfrmProductCardParameters.dxDBGrid1DblClick(Sender: TObject);
begin
  DBGridEdit(Sender);
end;

//编辑
procedure TfrmProductCardParameters.btnEditClick(Sender: TObject);
begin
  DBGridEdit(Sender);
end;

end.
procedure TfrmProductCardParameters.Panel1Click(Sender: TObject);
begin

end;



本文出自 “畅想天空” 博客,请务必保留此出处http://kinwar.blog.51cto.com/3723399/1611590

动态产生和删除相关组件并生成SQL语句

原文:http://kinwar.blog.51cto.com/3723399/1611590

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!