用textbox和listbox做成一个类似的combobox功能,用一个ListBox模拟下拉框,但关键在于一旦TextBox失去焦点,必须将下拉框收起(失去焦点不难判断,但关键在于鼠标点击了窗体或者Label这样的控件,控件并没有失去焦点,但也要收起下拉框)。  如何实现此种收起下拉框的操作? 

解决方案 »

  1.   

    把textbox放到一个panel里,只判断panel的失去焦点事件,这样就没问题了,我自己也是经常这样做的.
      

  2.   

    如果搂主这个不是一个用户控件,需要在Form类的窗口过程里面判断消息:
    protected override void WndProc(ref Message m)
    {
         if (m.Msg == 0x210) //WM_PARENTNOTIFY
         {
              隐藏列表
         }
         base.WndProc(ref m);
    }
      

  3.   


    用ToolStripDropDown做,把listbox放到ToolStripControlHost,再放到ToolStripDropDown中
    ToolStripDropDown封装了完成焦点和位置问题
      

  4.   

    可不可以在textbox的MouseLeave事件中,鼠标离开后就收起
      

  5.   

    否则是单独控件的话,搂主需要实现这个消息泵接口:IMessageFilter,其实以上两种情况下都可以用这个
    最主要是要实现该接口的下面方法:
    bool IMessageFilter.PreFilterMessage(ref Message m)
    {
    if (m.Msg == (int)Win32.Msgs.WM_LBUTTONUP) //按下鼠标键

    else if (m.Msg == (int)Win32.Msgs.WM_CAPTURECHANGED)

    else if (m.Msg == (int)Win32.Msgs.WM_KEYDOWN && (int)m.WParam == (int)Keys.Escape)//键盘键 return OnPreFilterMessage(ref m);
    }
      

  6.   

    http://auction1.taobao.com/auction/item_detail-0db2-ea4731c3151105140db23b526e63585a.jhtml
      

  7.   

    把listbox放在一个form上 在form 的deactive事件里 form.hide()
      

  8.   

    我用ToolStripDropDown试了一下,发现当它显示的时候,我就看不到焦点在哪个控件了。
    例如,当textbox获得焦点时,显示ToolStripDropDown,这时焦点就不在textbox上了,这个你是怎么解决的呢?
      

  9.   

    我的是一个自定义的控件哦,能够在自定义控件中添加form么?
      

  10.   


    在listbox的lostfocus事件中form.hide()你还需要一个button,在button的mousedown时,去show这个form也就是说,textbox和button是可以放在一个自定义的控件中的,而带listbox的form是动态show/hide的
      

  11.   

    回搂主18楼:
    可以在自己的控件中实现该接口,例如:
    public class MyControl : UserControl, IMessageFilter
    {
       public MyControl()
       {
          Application.AddMessageFilter(this);//把消息循环添加到应用程序
       }   ~MyControl()
       {
          Application.RemoveMessageFilter(this);//删除消息循环从应用程序中
       }   bool IMessageFilter.PreFilterMessage(ref Message m) 
       { 
           //左边鼠标键弹起,注意判断鼠标的位置是否在该控件内部
           if (m.Msg == (int)Win32.Msgs.WM_LBUTTONUP) 
            {
           }
            return OnPreFilterMessage(ref m); 
       }    protected virtual bool OnPreFilterMessage(ref Message m)
       {
           return false;
       }
    }