一、背景
1、 有Demo窗口,请中有按钮btnSelectAll(全部选择),btnNoSelect(全部取消),btnReverseSelect(反向选择),如下图:
2、 数据集qrybase中有记录如
selectFlag field1 field2
1 a a0
0 b b0
... ... ...
二、实现
1、选择方法,代码如下
procedure TfrmDemo.SetSelectState(VFlag: Integer); //VFlag=0 全部取消 1:全部选择 2:反向选择 begin with qryBase do begin if IsEmpty then Exit; if FindField('selectflag')=nil then Exit; DisableControls; try First; if VFlag<2 then begin while not Eof do begin if not (State in [dsInsert,dsEdit]) then Edit; if FieldByName('selectflag').Value=not Boolean(VFlag) then FieldByName('selectflag').Value:=VFlag; Next; end; end else if VFlag=2 then begin while not Eof do begin if not (State in [dsInsert,dsEdit]) then Edit; FieldByName('selectflag').Value:=not (FieldByName('selectflag').AsBoolean); Next; end; end; finally EnableControls; end; end; end;
2、btnSelectAll调用代码:
procedure TfrmDemo.btnSelectAllClick(Sender: TObject); begin SetSelectState(1); end;
3、btnNoSelect调用代码:
procedure TfrmDemo.btnNoSelectClick(Sender: TObject); begin SetSelectState(0); end;
4、btnReverseSelect调用代码:
procedure TfrmDemo.btnReverseSelectClick(Sender: TObject); begin SetSelectState(2); end;
附:
1、DisableControls方法
procedure TDataSet.DisableControls; begin if FDisableCount = 0 then begin FDisableState := FState; FEnableEvent := deDataSetChange; end; Inc(FDisableCount); end;
2、EnableControls方法
procedure TDataSet.EnableControls; begin if FDisableCount <> 0 then begin Dec(FDisableCount); if FDisableCount = 0 then begin if FDisableState <> FState then DataEvent(deUpdateState, 0); if (FDisableState <> dsInactive) and (FState <> dsInactive) then DataEvent(FEnableEvent, 0); end; end; end;