我要对多个串口进行编程,如何象VB一样在窗体上放一个MSComm控件,然后设定index为0,在窗体生成的时候Load MScomm(i),当下位机有数据上传时,通过MSComm_onComm的事件进行处理,在Delphi如何实现(动态生成控件)?

解决方案 »

  1.   

    Delphi中的控件数组举例:unit Unit1;
    {New一个Application,copy代码覆盖原代码,在对象监视器(Object Inspector)
    中通过下拉选择,设置Form1的OnCreate事件为FormCreate,然后直接运行就可看到效果}
    interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        procedure FormCreate(Sender: TObject);
        procedure MyClick(Sender:TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;
      a: array[1..100] of tbutton;implementation{$R *.dfm}procedure TForm1.MyClick(Sender: TObject);
    var
      i: integer;
      nam: string;
    begin
      i := strtoint(Trim(Copy(TButton(Sender).Name,7,3)));
      TButton(Sender).Visible := not TButton(Sender).Visible;
      i := i + 1;
      nam := 'button' + inttostr(i);
      if i <= 100 then
        TButton(FindComponent(nam)).Visible := not TButton(FindComponent(nam)).Visible
    end;procedure TForm1.FormCreate(Sender: TObject);
    var
      i: integer;
    begin
      for i := 1 to 100 do
      begin
        a[i] := tbutton.Create(self);
        with a[i] do
        begin
          Top := (i-1) div 10 * 20;
          Left := (i-1) mod 10 * 20;
          Height := 20;
          Width := 20;
          Name := 'button' + inttostr(i);
          Caption := '';
          Parent := Form1;
          OnClick := MyClick;
        end;
      end;
    end;end.
      

  2.   

    //动态创建dbedit
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls, Mask, DBCtrls, ExtCtrls;type
      TForm1 = class(TForm)
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
       procedure test(sender : TObject);
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
    var
        t : Tdbedit;
    begin
    t :=Tdbedit.create(application);
      with t do begin
        parent := self;
        width := 100;
        height := 29;
        name := 'text001';
        top := 100;
        left := 50;
        onclick := test;
        OnMouseMove:=text001MouseMove;
        show;
      end;
    end;
    procedure TForm1.test(sender: TObject);
    begin
        showmessage(Tdbedit(sender).name);
    end;
      

  3.   

    //delphi中组件数组的替换品是:  for j:=0 to ComponentCount-1 do
        begin      if (Components[j] is TMSComm)  then
                      (Components[j] as TMSComm).;  //在这做你要做的操作    end;