Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

DB Aware button on a CtrlGrid

42 views
Skip to first unread message

twist...@gmail.com

unread,
Mar 5, 2008, 10:34:56 PM3/5/08
to
Ok, I'm in need of a db aware button to place on a control grid. All
it has to do is copy the field info to the clipboard when clicked.
Thus far, I've only found some code written several years ago on
about.com. It works with one big exception. The button caption isn't
updated with the proper field data when a new panel in the ctrlgrid is
drawn. It does update the caption when you click on the button, but
then all of the button captions get updated. I've played with this
and am clueless as to whether it can be made to update on a ctrlgrid.
Can someone point me in the right direction?

unit DBButton;

interface

uses
DB, DBCtrls, SysUtils, Classes,
Controls, StdCtrls;

type
TDBButton = class(TButton)
private
FDataLink : TFieldDataLink;

function GetDataField: string;
function GetDataSource: TDataSource;
function GetField: TField;
procedure SetDataField(const Value: string);
procedure SetDataSource(const Value: TDataSource);
protected
procedure DataChange(Sender : TObject);
public
constructor Create(AOwner : TComponent);
override;
destructor Destroy; override;

property Field : TField read GetField;
published
property DataSource : TDataSource
read GetDataSource
write SetDataSource;
property DataField : string
read GetDataField
write SetDataField;
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('delphi.about.com',
[TDBButton]);
end;

constructor TDBButton.Create(AOwner: TComponent);
begin
inherited Create(AOwner);

FDataLink := TFieldDataLink.Create;
FDataLink.Control := self;
FDataLink.OnDataChange := DataChange;
end;

destructor TDBButton.Destroy;
begin
FDataLink.Free;
FDataLink := nil;

inherited;
end;

procedure TDBButton.DataChange(
Sender: TObject);
begin
Caption := FDataLink.Field.AsString;
Enabled := NOT FDataLink.Field.IsNull;
end;

function TDBButton.GetField: TField;
begin
result := FDataLink.Field;
end;

function TDBButton.GetDataField: string;
begin
result := FDataLink.FieldName;
end;

procedure TDBButton.SetDataField(
const Value: string);
begin
FDataLink.FieldName := value;
end;

function TDBButton.GetDataSource: TDataSource;
begin
result := FDataLink.DataSource;
end;

procedure TDBButton.SetDataSource(
const Value: TDataSource);
begin
FDataLink.DataSource := Value;
end;

end. //DBButton

0 new messages