// ¼Ò½º¸¦ ½ÇÇàÇÏ·Á¸é DBGrid1, DBEdit1, DBEdit2, DBEdit3 Àº ÀüºÎ
// DataSource1 ¿¡ ¿¬°á½ÃÄÑ¾ß Çϸç DBEdit1, DBEdit2, DBEdit3´Â
// ÀÓÀÇÀÇ DataField¸¦ °¡Áö°í ÀÖ¾î¾ß Å×½ºÆ®°¡ µË´Ï´Ù
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
TypInfo, StdCtrls, Db, DBTables, Grids, DBGrids, Mask, DBCtrls, Buttons;
type
TForm1 = class(TForm)
DBGrid1: TDBGrid;
Table1: TTable;
DataSource1: TDataSource;
DBEdit1: TDBEdit;
DBEdit2: TDBEdit;
DBEdit3: TDBEdit;
SpeedButton1: TSpeedButton;
procedure SpeedButton1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
// ÁÖ¾îÁø ControlÀÌ data aware control ÀÎÁö °Ë»çÇÏ´Â ÇÔ¼ö
// °Ë»ç ¹æ¹ýÀº data aware control Àº ÀüºÎ DataSource ÇÁ·ÎÆÛƼ¸¦ °¡Áö°í Àִµ¥
// property Á¤º¸¸¦ °Ë»çÇÏ¿© DataSource ÇÁ·ÎÆÛƼÀÇ Á¸Àç¿©ºÎ¸¦ °Ë»çÇÑ´Ù
function IsDBControl(AC: TControl; var Datasource: TDataSource): Boolean;
var
ptrPropInfo: PPropInfo;
begin
Result := False;
if AC <> nil then
begin
// controlÀÇ ÇÁ·ÎÆÛƼµéÁß DataSource ÇÁ·ÎÆÛƼ Á¤º¸¸¦ Àд´Ù(¾øÀ¸¸é nil)
ptrPropInfo := GetPropInfo(AC.ClassInfo, 'DataSource');
if Assigned(ptrPropInfo) and (ptrPropInfo^.PropType^.Kind = tkClass) then
begin
DataSource := Pointer(GetOrdProp(AC , ptrPropInfo));
Result := Assigned(DataSource);
end;
end;
end;
// ÁÖ¾îÁø FormÀÇ ÇöÀç ÆíÁýÁßÀÎ Field¸¦ ±¸ÇÑ´Ù
// ±¸ÇÏ´Â ¹æ¹ýÀº ¸ÕÀú ÁÖ¾îÁø ÆûÀÇ ActiveµÈ ControlÀÌ data aware controlÀÎÁö
// °Ë»çÇÏ°í ±×°ÍÀÌ DBGridÀÌ¸é ¼±ÅÃµÈ Çʵ带 ¸®ÅÏÇÏ°í ±×·¸Áö ¾ÊÀº ControlÀ̸é
// DataField ÇÁ·ÎÆÛƼ¸¦ °¡Áö°í ÀÖÀ¸¸é ±× Çʵ带 ¸®ÅÏÇÑ´Ù
function GetActiveField(Form: TForm): TField;
var
Datasource: TDatasource;
ptrPropInfo: PPropInfo;
AC: TControl;
begin
Result := nil;
if Form <> nil then
begin
AC := Form.ActiveControl; // ÆûÀÇ Æ÷Ä¿½º°¡ ÀÖ´Â control À» ±¸ÇÑ´Ù
if IsDBControl(AC, DataSource) and // AC°¡ data aware control ÀÎÁö °Ë»ç
(Datasource <> nil) and
(Datasource.DataSet <> nil) then
begin
if AC is TCustomDBGrid then // AC°¡ DBGridÀÌ¸é ¼±ÅÃµÈ Çʵ带
Result := (AC as tCustomDBGrid).SelectedField
else
begin
// controlÀÇ ÇÁ·ÎÆÛƼµéÁß DataField ÇÁ·ÎÆÛƼ Á¤º¸¸¦ Àд´Ù(¾øÀ¸¸é nil)
ptrPropInfo := GetPropInfo(AC.ClassInfo, 'DataField');
if Assigned(ptrPropInfo) and (ptrPropInfo^.PropType^.Kind = tkLString) then
Result := Datasource.Dataset.FindField(GetStrProp(AC, ptrPropInfo));
end;
end;
end;
end;
procedure TForm1.SpeedButton1Click(Sender: TObject);
var
cField: TField;
begin
// ActiveµÈ ÆûÀÇ ÇöÀç ÆíÁýÁßÀÎ Field¸¦ ±¸ÇÑ´Ù
cField := GetActiveField(Screen.ActiveForm);
if cField <> nil then
ShowMessage(cField.FieldName);
end;
end. |
|