新手提问,请多指点 : Form1上有很多TEdit和CheckBox,我想按“取消”键里,所有Tedit里输入的内容置空,CheckBox.checked:=false,难道要一个个edit1:=''  有什么好的办法?

解决方案 »

  1.   

    var
      jsq:integer;
    begin
      for jsq:=0 to ComponentCount-1 do
      begin
        if Components[jsq] is TEdit then
           (Components[jsq] as TEdit).Text:=''
        else if Components[jsq is TCheckBox then
           (Components[jsq] as TCheckBox).Checked:=False;
      end;
    end;
      

  2.   

    var i:integer;
    begin
      for i:=0 to Self.ControlCount-1 do
      begin
        if (self.Controls[i] is TEdit) then
          TEdit(self.Controls[i]).Clear;
        if (self.Controls[i] is TCheckBox) then
          TCheckBox(self.Controls[i]).Checked:=false;
          {And So On...}
      end;
    end;
      

  3.   

    上面的例程是通过使用ComponentCount及Components属性来完成的。上述方法很实用的,你可以举一反三。
      

  4.   

    //初始化过程
    procedure ClearValue(Components:Array of Component);
    var
      i:integer;
    begin
      for i:=Low(Components) to High(Components) do
      begin
        if Components[i] is TEdit then
           TEdit(Components[i]).Text:=''
        else if Components[i] is TCheckBox then
           TCheckBox(Components[i]).Checked:=False;
      end;
    end;
    //使用方法
    ClearValue([Edit1,Edit2,CheckBox1,CheckBox2])
      

  5.   

    最好采纳 pilicat(delphi迷) 的说法去自已过展。
      

  6.   

    来个复古的.
    procedure TForm1.Button1Click(Sender: TObject);
    var
      hwndcurr: HWND;
      buffer: array[0..255] of char;
    begin
      hwndcurr := getwindow(self.Button1.handle, GW_HWNDNEXT);//注意这是next,因为button我们不要
      while (hwndcurr <> 0) do begin
        getclassname(hwndcurr, @buffer, 256);
        if strpas(@buffer) = 'TEdit' then begin
          postmessage(hwndcurr, WM_SETFOCUS, 0, 0);
          postmessage(hwndcurr, WM_CLEAR, 0, 0);
             //setwindowtext(hwndcurr ,'');
        end;
        if strpas(buffer) = 'TCheckBox' then begin
          postmessage(hwndcurr, BM_SETCHECK, BST_UNCHECKED, 0);
        end;
        hwndcurr := getwindow(hwndcurr, GW_HWNDNEXT);
      end;
    end;肯定比楼上各位的快,呵呵!
      

  7.   

    完全不懂API..
    请问大家怎么学习API啊??
      

  8.   

    procedure TForm2.suiButton2Click(Sender: TObject);
       var
      jsq:integer;
    begin
      for jsq:=0 to ComponentCount-1 do
      begin
        if Components[jsq] is TEdit then
           (Components[jsq] as TEdit).Text:=''
        else if Components[jsq] is TCheckBox then
           (Components[jsq] as TCheckBox).Checked:=False;
      end;
    end;编译后点了没有变化啊
      

  9.   

    来晚乐,呵呵
    一二楼的是正确的
    补充一下:
    if (self.Controls[i] is TEdit) 
    也可以写成
    if Self.Components[i].ClassType=TEdit then
      

  10.   

    来晚了, pilicat(delphi迷)的方法是不错的而且很简单
      

  11.   

    各位老大:按 pilicat(delphi迷)的方法点了后没有反应啊!什么原因呢?
      

  12.   

    应该不会的,不然的话,你可以新建一个工程,放上若干个CheckBox和Edit,一个按钮,在按钮的OnClick事件中写如上代码。我试过N-1遍,也用过了N-3遍,没有问题啊!
      

  13.   

    //改寫自pilicat(delphi迷)
    procedure TForm1.Button1Click(Sender: TObject);
    var
      i:integer;
    begin
      for i:=0 to ComponentCount-1 do
      begin
        if Components[i] is TEdit then
           (Components[i] as TEdit).Text:='';
        if Components[i] is TCheckBox then
           (Components[i] as TCheckBox).Checked:=False;
        end;
    end;
      

  14.   

    pilicat(delphi迷)  的代码,少写了个‘]’,呵呵
      

  15.   

    (Components[jsq] as TEdit).Text:=''可以写成=>
    TEdit(Componentsp[jsq]).Text吗!??
      

  16.   

    OK了,多谢pilicat(delphi迷)
      

  17.   

    再问:Form上的Edit和Checkbox都可以了,但如果在Form上的Panel里的CheckBox就不行?