我想从一个字符串数组里取最长的一个字符串的长度该怎么取?根据字符串数组动态生成了很多按钮,点关闭时如何自动删除按钮记录???

解决方案 »

  1.   

    1 length可以获取字符串长度,用二分法就可以找出数组里最长的长度。
    2 在OnClose事件里用FreeAndNil释放掉按钮对象
      

  2.   

    这我知道的,可是我怎么试都没成功啊
    var btn:array of Tbutton;procedure TForm2.FormShow(Sender: TObject);
    var
      count:integer;
      top:integer;
      i:integer;
    begin
       top:=0;
       count := 1;
       for i :=0 to high(s) do
       begin
          if s[i]<>'' then
          begin
            setlength(btn,Count);        btn[Count-1]:=TButton.Create(self);
            btn[count-1].parent:=self;
            btn[Count-1].Caption :=s[i];
            btn[count-1].Visible :=true;
            btn[count-1].Width:=btn[i].Width;
            btn[count-1].Height :=30;
            btn[count-1].OnClick  :=btn_click;
            if count mod 2= 1 then
            begin
              top:=top+30;
              btn[count-1].Left :=75;
              btn[count-1].Top :=top ;
            end
            else
            begin
              btn[count-1].Left :=75+btn[i].Width;
              btn[count-1].Top :=top ;
            end;
            Count:=Count+1;
            if i>=6 then
            begin
            panel1.Height:=14+30*((count div 2)+(count mod 2));
            end
            else
            begin
            panel1.Height:=106;
            end;
          end;
       end;
    end;
    麻烦写给我看下啊,我试了好多种方法都没成功~!!
      

  3.   

    根据字符串数组动态生成了很多按钮,点关闭时如何自动删除按钮记录???
    ====================================================================================如果按钮已经InsertControl到窗口,不用自己删除
      

  4.   

    btn[count-1].parent:=self;
    =============================================================
    表示已经插到窗口,不用自己删除
      

  5.   

    可是我关闭窗体后再重新动态生成按钮的时候原来的那些按钮还在啊
    ==================================================================
    这就是你的代码问题了,如果你在窗口建立时可以确定,你可以把代码写在OnCreate时间中,如果写在OnShow中,可以在OnClose中删除:RemoveControl(btn[count-1]);
    btn[count-1].Free;
      

  6.   

    btn := nil;好象就可以释放。
    setlength(btn,0)也可以。。然后再btn:=nil;
    如果要关闭按纽  得调用按纽的free方法 记录一下按纽对象  循环释放一下就可以了
      

  7.   

    btn := nil;是不行的,也可以btn[count-1].Parent := nil;
    btn[count-1].Free;
      

  8.   

    RemoveControl(btn[count-1]);
    btn[count-1].Free;
    ---------------------------------------------------------------
    就把这句写在ONCLOSE里吗?可是它报错啊
      

  9.   

    btn[count-1].Parent := nil;
    btn[count-1].Free;
    写哪呀?
      

  10.   

    就把这句写在ONCLOSE里吗?可是它报错啊
    =========================================================================
    不会吧?那你得检查其它代码,在OnCreate与OnDestyoy对应,OnShow与OnClose对应,如果无其它原因,应该不错,刚试过:unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure FormShow(Sender: TObject);
        procedure FormClose(Sender: TObject; var Action: TCloseAction);
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
        Btn: TButton;
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormShow(Sender: TObject);
    begin
      Btn := TButton.Create(Self);
      Btn.Parent := Self;
    end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
    begin
      Btn.Parent := Self;
      Btn.Free;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
      Close;
    end;end.
      

  11.   

    procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
    begin
      Btn.Parent := Self;
      Btn.Free;
    end;改为procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
    begin
      Btn.Parent := nil;
      Btn.Free;
    end;
      

  12.   

    我那个BTN是var btn:array of Tbutton;也可以这样子改吗?我试试~
      

  13.   

    [Error] Unit2.pas(142): Record, object or class type required
    [Warning] Unit2.pas(63): For loop control variable must be simple local variable
      

  14.   

    你的代码错误太多,我这样帮你改一下,试试看,我没试机:在Form2中添加一个Btn: TList;
    var btn:array of Tbutton;procedure TForm2.FormShow(Sender: TObject);
    var
      count:integer;
      top:integer;
      i:integer;
      B: TButton;
    begin
      top:=0;
      Btn := TList.Create;
      for i :=low(s) to high(s) do
      begin
          if s[i]<>'' then
          begin
            B := TButton.Create(self);
            B.Caption :=s[i];
            B.Width := ????; // 自己决定吧
    //        btn[count-1].Visible :=true;
    //这句本人不懂        btn[count-1].Width:=btn[i].Width;
            B.Height :=30;
            B.OnClick  :=btn_click;
            if count mod 2= 1 then
            begin
              top:=top+30;
              B.Left :=75;
              B.Top :=top ;
            end
            else
            begin
              B.Left :=75+btn[i].Width;
              B.Top :=top ;
            end;
            B.parent:=self;
            Btn.Add(B);
            Count:= Btn.Count;
            if i>=6 then
            begin
            panel1.Height:=14+30*((count div 2)+(count mod 2));
            end
            else
            begin
            panel1.Height:=106;
            end;
          end;
       end;
    end;procedure TForm2.FormClose(Sender: TObject);
    var
      I: Interger;
    begin
      for I := 0 to Btn.Count - 1 do
      begin
        TButton(Btn[I]).Parent := nil;
        TButton(Btn[I]).Free;
      end;
      Btn.Free;
    end;
      

  15.   

    var btn:array of Tbutton;
    ========================================================
    不要了.