如何自动选取数据集?(100分)

  • 主题发起人 主题发起人 philips
  • 开始时间 开始时间
P

philips

Unregistered / Unconfirmed
GUEST, unregistred user!
在Action中关于数据导航的操作能自动根据在屏幕上选取的数据敏感控件
自动对相应的数据集进行操作,怎么在自己的程序中达到相同效果?
 

比如当前选择的对象为DBGrid, 则可通过DBGrid.DataSource.DataSet访问数据集。
不知我是否理解了你的意思?
 
不知所云。
 
首先感谢两位回复我的问题!

我的意思是:

当窗体中有对多个表的编辑控件时,(比如两个dbgrid 或 dbedit(连接到不同表上))
当焦点移到不同的控件上时,数据集的导航控件会自动识别,而且不在编辑控件的获得
焦点的事件编写代码。

Delphi的Action标准按钮里已经有这样的功能。现在我不用它,自己应在哪个事件里
写代码?(我知道:肯定不是在每个数据敏感的控件的得到焦点事件)

不知我说清楚没有?
 
如何知道屏幕上那个控件具有焦点?
 
怎样知道焦点发生了变化?
 
ActiveControl.Name
 
我想:

D5 自带的 Action是这样实现的: 封装一组按钮,在启动时,取得屏幕的活动控件,
若是数据集敏感控件,就将导航按钮用来控制该数据集(进行数据集设置)。

当焦点发生变化时,重复上面的操作(当然可以检测后在改变)。
 
你说的是 TDataSetEdit, TDataSetFirst 等从 TDataSetAction 继承而来的标准 Action
吗?它们都有个 DataSource 属性,必须事先指定一个 DataSource 才行的。也不能
自动执行吧,Enabled/Disabled 的状态倒是自动变化的。
 
这是 Borland 原代码:

{*******************************************************}
{ }
{ Borland Delphi Visual Component Library }
{ }
{ Copyright (c) 1999 Inprise Corporation }
{ }
{*******************************************************}

unit DBActns;

{$H+,X+}

interface

uses Classes, ActnList, Db;

type
{ DataSet actions }
TDataSetAction = class(TAction)
private
FDataSource: TDataSource;
procedure SetDataSource(Value: TDataSource);
protected
function GetDataSet(Target: TObject): TDataSet; virtual;
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
public
function HandlesTarget(Target: TObject): Boolean; override;
property DataSource: TDataSource read FDataSource write SetDataSource;
end;

TDataSetFirst = class(TDataSetAction)
public
procedure ExecuteTarget(Target: TObject); override;
procedure UpdateTarget(Target: TObject); override;
published
property DataSource;
end;

TDataSetPrior = class(TDataSetAction)
public
procedure ExecuteTarget(Target: TObject); override;
procedure UpdateTarget(Target: TObject); override;
published
property DataSource;
end;

TDataSetNext = class(TDataSetAction)
public
procedure ExecuteTarget(Target: TObject); override;
procedure UpdateTarget(Target: TObject); override;
published
property DataSource;
end;

TDataSetLast = class(TDataSetAction)
public
procedure ExecuteTarget(Target: TObject); override;
procedure UpdateTarget(Target: TObject); override;
published
property DataSource;
end;

TDataSetInsert = class(TDataSetAction)
public
procedure ExecuteTarget(Target: TObject); override;
procedure UpdateTarget(Target: TObject); override;
published
property DataSource;
end;

TDataSetDelete = class(TDataSetAction)
public
procedure ExecuteTarget(Target: TObject); override;
procedure UpdateTarget(Target: TObject); override;
published
property DataSource;
end;

TDataSetEdit = class(TDataSetAction)
public
procedure ExecuteTarget(Target: TObject); override;
procedure UpdateTarget(Target: TObject); override;
published
property DataSource;
end;

TDataSetPost = class(TDataSetAction)
public
procedure ExecuteTarget(Target: TObject); override;
procedure UpdateTarget(Target: TObject); override;
published
property DataSource;
end;

TDataSetCancel = class(TDataSetAction)
public
procedure ExecuteTarget(Target: TObject); override;
procedure UpdateTarget(Target: TObject); override;
published
property DataSource;
end;

TDataSetRefresh = class(TDataSetAction)
public
procedure ExecuteTarget(Target: TObject); override;
procedure UpdateTarget(Target: TObject); override;
published
property DataSource;
end;

implementation

{ TDataSetAction }

function TDataSetAction.GetDataSet(Target: TObject): TDataSet;
begin
{ We could cast Target as a TDataSource since HandlesTarget "should" be
called before ExecuteTarget and UpdateTarget, however, we're being safe. }
Result := (Target as TDataSource).DataSet;
end;

function TDataSetAction.HandlesTarget(Target: TObject): Boolean;
begin
{ Only handle Target if we don't already have a DataSource assigned and the
Target is a TDataSource with a non nil DataSet assigned. }
Result := (DataSource <> nil) and (Target = DataSource) and
(DataSource.DataSet <> nil) or (DataSource = nil) and
(Target is TDataSource) and (TDataSource(Target).DataSet <> nil);
end;

procedure TDataSetAction.Notification(AComponent: TComponent;
Operation: TOperation);
begin
inherited Notification(AComponent, Operation);
if (Operation = opRemove) and (AComponent = DataSource) then DataSource := nil;
end;

procedure TDataSetAction.SetDataSource(Value: TDataSource);
begin
if Value <> FDataSource then
begin
FDataSource := Value;
if Value <> nil then Value.FreeNotification(Self);
end;
end;

{ TDataSetFirst }

procedure TDataSetFirst.ExecuteTarget(Target: TObject);
begin
GetDataSet(Target).First;
end;

procedure TDataSetFirst.UpdateTarget(Target: TObject);
begin
with GetDataSet(Target) do
Enabled := Active and not Bof;
end;

{ TDataSetPrior }

procedure TDataSetPrior.ExecuteTarget(Target: TObject);
begin
GetDataSet(Target).Prior;
end;

procedure TDataSetPrior.UpdateTarget(Target: TObject);
begin
with GetDataSet(Target) do
Enabled := Active and not Bof;
end;

{ TDataSetNext }

procedure TDataSetNext.ExecuteTarget(Target: TObject);
begin
GetDataSet(Target).Next;
end;

procedure TDataSetNext.UpdateTarget(Target: TObject);
begin
with GetDataSet(Target) do
Enabled := Active and not Eof;
end;

{ TDataSetLast }

procedure TDataSetLast.ExecuteTarget(Target: TObject);
begin
GetDataSet(Target).Last;
end;

procedure TDataSetLast.UpdateTarget(Target: TObject);
begin
with GetDataSet(Target) do
Enabled := Active and not Eof;
end;

{ TDataSetInsert }

procedure TDataSetInsert.ExecuteTarget(Target: TObject);
begin
GetDataSet(Target).Insert;
end;

procedure TDataSetInsert.UpdateTarget(Target: TObject);
begin
with GetDataSet(Target) do
Enabled := Active and CanModify;
end;

{ TDataSetRefresh }

procedure TDataSetRefresh.ExecuteTarget(Target: TObject);
begin
GetDataSet(Target).Refresh;
end;

procedure TDataSetRefresh.UpdateTarget(Target: TObject);
begin
with GetDataSet(Target) do
Enabled := Active and CanModify;
end;

{ TDataSetPost }

procedure TDataSetPost.ExecuteTarget(Target: TObject);
begin
GetDataSet(Target).Post;
end;

procedure TDataSetPost.UpdateTarget(Target: TObject);
begin
with GetDataSet(Target) do
Enabled := Active and CanModify and (State in dsEditModes);
end;

{ TDataSetCancel }

procedure TDataSetCancel.ExecuteTarget(Target: TObject);
begin
GetDataSet(Target).Cancel;
end;

procedure TDataSetCancel.UpdateTarget(Target: TObject);
begin
with GetDataSet(Target) do
Enabled := Active and CanModify and (State in dsEditModes);
end;

{ TDataSetEdit }

procedure TDataSetEdit.ExecuteTarget(Target: TObject);
begin
GetDataSet(Target).Edit;
end;

procedure TDataSetEdit.UpdateTarget(Target: TObject);
begin
with GetDataSet(Target) do
Enabled := Active and CanModify and not (State in dsEditModes);
end;

{ TDataSetDelete }

procedure TDataSetDelete.ExecuteTarget(Target: TObject);
begin
GetDataSet(Target).Delete;
end;

procedure TDataSetDelete.UpdateTarget(Target: TObject);
begin
with GetDataSet(Target) do
Enabled := Active and CanModify and not (Bof and Eof);
end;

end.

你研究一下,肯定解决。
 
多人接受答案了。
 
后退
顶部