Component(50分)

  • 主题发起人 主题发起人 dyxfkj
  • 开始时间 开始时间
D

dyxfkj

Unregistered / Unconfirmed
GUEST, unregistred user!
unit UntBasic;

interface

Uses
Classes, Controls, ComCtrls, StdCtrls,
SysUtils, ExtCtrls, DB, Dialogs;

type

TcyEdit = Class(TEdit)
private
fFieldName: String;
protected
public
published
Property FieldName: String Read fFieldName Write fFieldName;
end;

TcyDateTimePicker = Class(TDateTimePicker)
private
fFieldName: String;
protected
public
published
Property FieldName: String Read fFieldName Write fFieldName;
end;

TcyCombobox = Class(TCombobox)
private
fFieldName: String;
protected
public
published
Property FieldName: String Read fFieldName Write fFieldName;
end;

TcyPanel = Class(TPanel)
private
fTableName: String;
fDataset: TDataSet;
FieldList: TStringList;
ValueList: TStringList;
protected
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;

Procedure GetFieldInfo;
Procedure SaveData;
Procedure UpdateData;
published
Property TableName: String Read fTableName Write fTableName;
Property DataSet: TDataSet Read fDataSet Write fDataSet;
end;



implementation




{ TcyPanel }

constructor TcyPanel.Create(AOwner: TComponent);
begin
inherited;
FieldList := TStringList.Create;
ValueList := TStringList.Create;
end;

destructor TcyPanel.Destroy;
begin
FieldList.free;
ValueList.Free;
inherited;
end;

procedure TcyPanel.GetFieldInfo;
var
i: Integer;
begin
if self.ComponentCount > 0 then
ShowMessage('aa'); ************问题: 为何我在cyPanel中的cyEdit , 用以下这种方式取不到呢?

for i := 0 to Self.ComponentCount - 1 do
begin
if (Self.Components is TcyEdit) then
begin
FieldList.Add(TcyEdit(Self.Components).fFieldName);
ValueList.Add(Trim(TcyEdit(Self.Components).Text));
end else
if (Self.Components is TcyDateTimePicker) then
begin
FieldList.Add(TcyEdit(Components).fFieldName);
ValueList.Add(FormatDateTime('yyyy-mm-dd', TcyDateTimePicker(Components).DateTime))
end else
if (Components is TcyDateTimePicker) then
begin
FieldList.Add(TcyCombobox(Components).fFieldName);
ValueList.Add(Trim(TcyCombobox(Components).Text));
end;
end;
end;


procedure TcyPanel.SaveData;
var
i: Integer;
SQLStr: String;
begin
GetFieldInfo;

for i := 0 to FieldList.Count - 1 do
begin
SQLStr := SQLStr + FieldList;
end;
end;

procedure TcyPanel.UpdateData;
begin

end;

end.
 
for i := 0 to Self.ComponentCount - 1 do
DELPHI在设计其间直接拉到窗体上的组件的所有者都是这个窗体.
这里的Self是cyPanel,而你的cyEdit的所有者应该是窗体Form,当然取不到啊,
除非你自已动态创建的时指定指定为某个cyPanel所有,才取得到,
如cyEdit:= TcyEdit.Create(这里指定为某个cyPanel);
 
这个我试过!

constructor TcyEdit.Create(aOwner: TComponent);
begin
inherited Create(TcyPanel); //这儿怎么写啊?
// Parent := TComponent(TcyPanel);
end;

destructor TcyEdit.Destroy;
begin

inherited Destroy;
end;
 
用Controls集合,ControlCount属性
 
多人接受答案了。
 
后退
顶部