问题:我在设计时动态创建的控件,那个控件是由一个控件生成的,被生成的代码放在当前的Form中。我怎样才能在控件中为那个被生成的控件指定事件?相关贴子:
http://expert.csdn.net/Expert/topic/2216/2216893.xml?temp=.3933222
http://expert.csdn.net/Expert/topic/2179/2179819.xml?temp=.6342737

解决方案 »

  1.   


    http://ww.sjrj.com/yzt/yDBFormWizardDEMO.rar名称 :数据窗口向导DEOM
    作者 :LazyBoy
    日期 :2003-9-4
    Email:[email protected]
    MSN  :[email protected]
    QQ   :59219588
    说明:先安装yDBFormWizard.dcu,安装后面板上多了一个yzt面板。然后打开DEMO/Project1.dpr。在控件yDBFormWizard的属性编辑器上把Active属性设为True。你看到了什么?
    注意:1.yDBFormWizard激活后,他要做的事做完后就"自杀"了!
          2.数据库的字段不能用中文字段名。
          3.使用yDBFormWizard时,当前的Form最好是空的。
          4.如果在同一个窗口中多次使用yDBFormWizard,应把yDBFormWizard生成的所有控件删除掉。否则因为控件同名而无法继续。
          ____________________________________________________________
    yDBFormWizard属性:Active           :开始创建数据窗口
    DataSource       :不用说你也知道吧?
    Fields           :还没有完成:(
    Name             :这个不用解释吧?
    Tag              :鬼才知道这个能干什么事件:
    无方法:

      

  2.   

    http://ww.sjrj.com/yzt/yDBFormWizardDEMO.rar应该为:
    http://www.sjrj.com/yzt/yDBFormWizardDEMO.rar
      

  3.   

    procedure TInplaceEdit._KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    begin
      if key = VK_RETURN then
      begin
        SendMessage(Handle, MM_NEXTCELL, 0, 0);
        key:= 0;
      end;
    end;constructor TInplaceEdit.Create(AOwner: TComponent);
    begin
      inherited Create(AOwner);
      Color:= clWhite;
      FCodeInput:= TCodeInput.Create(self);
      FDateInput:= TDateInput.Create(self);
      FStringInput:= TMaskEdit.Create(self);
      FListInput:= TCombobox.Create(self);  FCodeInput.Parent:= self;
      FDateInput.Parent:= self;
      FStringInput.Parent:= self;
      FListInput.Parent:= self;  FCodeInput.Align:= alClient;
      FDateInput.Align:= alClient;
      FStringInput.Align:= alClient;
      FListInput.Align:= alClient;  FCodeInput.BorderStyle:= bsNone;
      FDateInput.BorderStyle:= bsNone;
      FStringInput.BorderStyle:= bsNone;  FStringInput.OnKeyDown:= _KeyDown;
      FStringInput.OnKeyPress:= _KeyPress;  self.BevelOuter:= bvNone;
      SELF.Visible:= false;
    end;
      

  4.   

    TO: fengjn(小枫)好像不行。PAS文件中没有那段代码。
      

  5.   

    不好意思,原代码在此://yDBFormWizard.pasunit yDBFormWizard;interfaceuses
      Windows, Messages, SysUtils, Classes, Forms, Controls, Dialogs, StdCtrls, DB,DBCtrls,ComCtrls,
      DBGridEh,DBCtrlsEh,DBLookupEh,DesignIntf, ToolsAPI;TYpe  TControlKind = (ckEdit,ckDateTime,cbComboBox,cbComboBoxLookup);
      TYDBFormWizard = class(TComponent)
      private
        { Private declarations }
        FDataSource:TDataSource;
        FActive:Boolean;
        procedure SetDataSource(const Value: TDataSource);
        procedure SetActive(const Value: Boolean);
      protected
        { Protected declarations }
        procedure _DBEditKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
      public
        { Public declarations }
        constructor Create(AOwner: TComponent); override;
        destructor Destroy; override;
      published
        { Published declarations }
        property DataSource: TDataSource read FDataSource write SetDataSource;
        property Active: Boolean read FActive write SetActive;
      end;procedure Register;implementationprocedure Register;
    begin
      RegisterComponents('yzt', [TYDBFormWizard]);
    end;{ TYDBFormWizard }constructor TYDBFormWizard.Create(AOwner: TComponent);
    begin
      inherited;
    end;destructor TYDBFormWizard.Destroy;
    begin  inherited;
    end;procedure TYDBFormWizard.SetActive(const Value: Boolean);
    var
      i,iLoopCount:integer;
      lbl:TLabel;
      DBEdit:TDBEdit;
      PageControl:TPageControl;
      TabSheet:TTabSheet;
      DBGrid:TDBGridEh;
      DBDateTimeEditEh:TDBDateTimeEditEh;
      //DBLookupComboboxEh:TDBLookupComboboxEh;
    begin  if (csDesigning in ComponentState) and (Value=true) then
      begin
        //ShowMessage(DataSource.DataSet.ClassName);
        if DataSource=nil then
        begin
          Application.MessageBox('没有设置DataSource!','yDBFormWizard提示...',MB_OK+MB_ICONWARNING);
          exit;
        end;    if DataSource.DataSet=nil then
        begin
          Application.MessageBox('DataSource没有设置对应的DataSet!','yDBFormWizard提示...',MB_OK+MB_ICONWARNING);
          exit;
        end;    if DataSource.DataSet.Active=false then
        begin
          try
            DataSource.DataSet.Active:=true;
          except
            on E: Exception do
            begin
              Application.MessageBox(Pchar('打开DataSet出错:'+#13+E.Message),'yDBFormWizard提示...',MB_OK+MB_ICONWARNING);
              exit;
            end;
          end;
        end;
        PageControl:=TPageControl.Create(self.Owner);
        PageControl.Name:='PageControl1';
        PageControl.Top:=0;
        PageControl.Left:=0;
        PageControl.Width:= TForm(self.Owner).Width-50;
        PageControl.Height:= TForm(self.Owner).Height-50;
        PageControl.Parent:=TWinControl(self.Owner);    TabSheet:=TTabSheet.Create(PageControl);
        TabSheet.Caption:='详细资料';
        TabSheet.PageControl:=PageControl;
        TabSheet.TabVisible:=true;
        TabSheet.Name:='TabSheet1';
        TabSheet.PageIndex:=0;
        TabSheet.Align:=alClient;
        iLoopCount:=0;
        while iLoopCount<DataSource.DataSet.Fields.Count do
        begin
          for i := 0 to 1 do
          begin        if iLoopCount>=DataSource.DataSet.Fields.Count then break;        lbl:=TLabel.Create(self.Owner);
            lbl.Name:='lbl_Field'+IntToStr(iLoopCount);
            lbl.Left:=20+i*250;
            lbl.Top:=(iLoopCount div 2)*30+10;
            lbl.AutoSize:=false;
            lbl.Width:=60;
            lbl.Alignment:=taRightJustify;
            lbl.Caption:=DataSource.DataSet.Fields[iLoopCount].DisplayLabel;
            lbl.Parent:=TabSheet;//TWinControl(self.Owner);        //if DataSource.DataSet.Fields[iLoopCount].FieldKind =fkLookup then
            //begin
              //DBLookupComboboxEh:=TDBLookupComboboxEh.Create(self.Owner);
              //DBLookupComboboxEh.Name:= 'cb_' + DataSource.DataSet.Fields[iLoopCount].FieldName;
              //DBLookupComboboxEh.Left := 20 + i * 250 + 65;
              //DBLookupComboboxEh.Top := (iLoopCount div 2) * 30 + 10;
              //DBLookupComboboxEh.Width := 150;
              //DBLookupComboboxEh.DataField := DataSource.DataSet.Fields[iLoopCount].FieldName;
              //DBLookupComboboxEh.DataSource := DataSource;
              //DBLookupComboboxEh.ListSource.DataSet:=DataSource.DataSet.Fields[iLoopCount].LookupDataSet;
              //DBLookupComboboxEh.KeyField:=DataSource.DataSet.Fields[iLoopCount].LookupKeyFields;
              //DBLookupComboboxEh.ListField:=DataSource.DataSet.Fields[iLoopCount].LookupResultField;          //DBLookupComboboxEh.Parent := TabSheet;//TWinControl(self.Owner);        //end
            //else
            //begin
              if DataSource.DataSet.Fields[iLoopCount].DataTYpe in [ftDate,ftTime,ftDateTime] then
              begin
                DBDateTimeEditEh := TDBDateTimeEditEh.Create(self.Owner);
                DBDateTimeEditEh.Name:= 'dt_' + DataSource.DataSet.Fields[iLoopCount].FieldName;
                DBDateTimeEditEh.Left := 20 + i * 250 + 65;
                DBDateTimeEditEh.Top := (iLoopCount div 2) * 30 + 10;
                DBDateTimeEditEh.Width := 150;
                DBDateTimeEditEh.DataField := DataSource.DataSet.Fields[iLoopCount].FieldName;
                DBDateTimeEditEh.DataSource := DataSource;
                DBDateTimeEditEh.Parent := TabSheet;//TWinControl(self.Owner);
              end
              else
              begin
                DBEdit:=TDBEdit.Create(self.Owner);
                DBEdit.Name:='ed_'+DataSource.DataSet.Fields[iLoopCount].FieldName;
                DBEdit.Left:=20+i*250+65;
                DBEdit.Top:=(iLoopCount div 2)*30+10;
                DBEdit.Width:=150;
                DBEdit.DataField:=DataSource.DataSet.Fields[iLoopCount].FieldName;
                DBEdit.DataSource:=DataSource;
                DBEdit.Parent:=TabSheet;//TWinControl(self.Owner);
                DBEdit.OnKeyDown:=_DBEditKeyDown;
              end;
            //end;        Inc(iLoopCount);                         //自增      end;
        end;    TabSheet:=TTabSheet.Create(PageControl);
        TabSheet.Caption:='列表';
        TabSheet.PageControl:=PageControl;
        TabSheet.TabVisible:=true;
        TabSheet.Name:='TabSheet2';
        TabSheet.PageIndex:=1;
        TabSheet.Align:=alClient;    DBGrid:=TDBGridEh.Create(self.Owner);
        DBGrid.Name:='DBGridEh1';
        DBGrid.DataSource:=DataSource;
        DBGrid.Top:=4;
        DBGrid.Left:=4;
        DBGrid.Height:=TabSheet.Height-8;
        DBGrid.Width:=TabSheet.Width-8;
        DBGrid.Parent:=TabSheet;
        DBGrid.Visible:=true;
        
        Free();
        
      end;  //FActive := Value;
      
    end;procedure TYDBFormWizard.SetDataSource(const Value: TDataSource);
    begin
      if Value<>nil then
      begin
        FDataSource := Value;
      end
      else
      begin
        FDataSource:=nil;
      end;
    end;
    procedure TYDBFormWizard._DBEditKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    begin
      ShowMessage('Test');
    end;end.
      

  6.   

    http://www.skin-studio.com 使用皮肤吧
      

  7.   

    DBGrid.OnKeyDown:= _KeyDown;
      DBGrid.OnKeyPress:= _KeyPress;在你创建控件时加上上面类似代码,就是将控件的事件重定向到一个你指定的过程,
      

  8.   

    是不是这个意思
    procedure TForm1.Button1Click(Sender: TObject);
    var
     MyBtn:TButton;
    begin
      MyBtn:=TButton.Create(form1);
      MyBtn.Left:=100;
      MyBtn.Top:=100;
      MyBtn.Height:=20;
      MyBtn.Width:=100;
      MyBtn.Caption:='新生成控件';
      MyBtn.Parent:=form1;
      MyBtn.OnClick:=MyBtnClick;
    end;procedure TForm1.MyBtnClick(Sender: TObject);
    begin
      showmessage('生成控件的事件');
    end;