如何做到点击窗体上的任何一个控件(label、edit、groupbox等等)都能让listbox1.Visible:=false呢?谢谢

解决方案 »

  1.   

    label、edit、groupbox 都有OnClick 事件函数,在其中运行listbox1.Visible:=false,可以让label、edit、groupbox 响应同一个事件函数
      

  2.   


    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Label1: TLabel;
        ListBox1: TListBox;
        Edit1: TEdit;
        GroupBox1: TGroupBox;
        procedure FormCreate(Sender: TObject);
      private
        procedure OneClick(Sender: TObject);
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
    begin
      Label1.OnClick:=OneClick;
      Edit1.OnClick:=OneClick;
      GroupBox1.OnClick:=OneClick;
    end;procedure TForm1.OneClick(Sender: TObject);
    begin
      ListBox1.Visible:=not ListBox1.Visible;
    end;end.
      

  3.   

    呵呵呵,这样也行,安装一个消息钩子,钩住WM_LBUTTONDWON 再判断句柄是不是ListBox.Handle不是的,就Visible := False
      

  4.   

    label、edit、groupbox 的OnClick 事件函数,写入listbox1.Visible:=false;设置label、edit、groupbox 响应同一个事件函数。
      

  5.   

    呵呵。。先循环找出listbox上都有哪些控件。。再进行操作
      

  6.   

    用controlcount列出窗体上所有对控件数目,用controls[i]来表示某一个控件
    例如,for i := 0 to Form1.ControlCount - 1 do
           if Form1.Controls[i] is TObject then
            begin
             listbox1.Visible:=false;
             ......
             ......
            end;
      

  7.   

    procedure TForm1.Onclick(Sender: tobject);
    begin
      lv1.Visible := False;
    end;procedure TForm1.FormCreate(Sender: TObject);
    var
      i: Integer;
    begin
      for i := 0 to form1.ControlCount -1 do
      begin
        if (Form1.Controls[i] is TEdit) then
          (Form1.Controls[i] as TEdit).OnClick := Onclick;
        if (Form1.Controls[i] is TLabel) then
          (Form1.Controls[i] as TLabel).OnClick := Onclick;
        
      end;
    end;
      

  8.   

    呵呵,前面幾位的方法是太笨了,而且彈性不好,若控件有單擊事件,切換存在問題!
    其實只要理解了所謂的“click”事件,各位就明白怎么實現了,參考代碼如下:interface
      ...
      //重寫父類的wm_command消息處理函數
      procedure WMCommand(var Message: TWMCommand); message WM_COMMAND;  ...implementationprocedure WMCommand(var Message: TWMCommand);
    begin
      if not (message.Ctl=listBox1.Handle) then
        ListBox1.Visible := False;
      inherited; 
    end;   所謂的click,是由wm_command轉化而來,因為當鼠標單擊時,都會產生wm_command
      

  9.   

    上面代碼嚴謹點,應該多一些判斷procedure WMCommand(var Message: TWMCommand);
    begin
      if (message.Ctl<>0) and (message.Ctl<>listBox1.Handle) then
        ListBox1.Visible := False
      else
        ListBox1.Visible := True; 
      inherited; 
    end;同時修正上面的觀點“所謂的click,是由wm_command轉化而來”,準確地說,大部分的控件的click是由這個轉化的,若部分控件沒有處理cn_command(由wm_command轉化而來),在TControl里面會處理WMLButtonUp消息,來形成click事件。因此,這段代碼對groupbox無效
      

  10.   

    當然,對沒有handle的控件也無效
      

  11.   


    顶下 简单点是不是可以这么写:ListBox1.Visable := not ((message.Ctl <>0) and (message.Ctl <>listBox1.Handle));
      

  12.   

    有没有办法对所有控件都有效呢? 上面的方法对groupbox和label好像都无效.谢谢
      

  13.   

    上面已說明,此代碼對groupbox無效,label沒有handle,也無效;由于這類控件的click事件一般來源于tcontrol,必定會設定controlstyle,所以可以遍歷form.controls,判斷csClickEvents是否在controlstyle集合內,若存在,則將此控件的onclick事件指針指向自己定義的事件函數(類似前面幾位的做法)。
    之所以要多一個判斷,是要避免替換其他控件可能已存在的click事件