我写了一个UserControl,UserControl里一个Panel,Dock为Fill,然后在里面加了多个Label(可能以后还要加其它控件),,现在我申明了Panel的MouseEnter和MouseLeave事件,但是鼠标移到Label时,就会触发Panel的MouseLeave事件,还有申明了UserControl的Click事件,但鼠标在Label上单击不能触发UserControl的Click事件,求解决的办法,谢谢

解决方案 »

  1.   

    为Label写鼠标事件,在其中调用panel的鼠标事件,或者自定义事件,这样谁都可以触发
      

  2.   

    自己看看,移到Label时触发Panel的事件??
    不如写个MouseEnter和MouseLeave事件但与Label绑定
      

  3.   

    对啊,当鼠标移到Label时,就离开了Panel,肯定就会触发Panel的MouseLeave事件,我试过了
      

  4.   

    并且我是把Label放在Panel里面的,但还是要触发
      

  5.   

    我也想通过逻辑来控制,但当鼠标进入Label时,panel的MouseLeave发生在Label的MouseEnter前面,所以我就无法控制了
      

  6.   

    我是希望在进入Label不要触发panel的MouseLeave
      

  7.   

    你这个本身就矛盾,你要操作哪个空件就绑定哪个,panel相当于是个母版,所有上面的控件都会受到panel事件的影响啊
      

  8.   

    一样的,主要lable的z顺序在前面
      

  9.   

    触发似乎很难避免。但可以在Panel的MouseLeave事件里进行鼠标位置的判断,以决定是否真正执行                Point pointMouse = this.Parent.PointToClient(Control.MousePosition);
                    Rectangle rectActive = new Rectangle(this.Left, this.Top, this.Width, this.Height);
                    if (!rectActive.Contains(pointMouse))
                    {
                        //...真正离开了
                    }
      

  10.   

    不知道你Panel的MouseEnter和MouseLeave、Click需要做什么?
    感觉你的需求有点不妥,看你的描述好像只需要实现Lable就够了。
      

  11.   

    也就是我需要在Panel的MouseEnter和MouseLeave做处理,但是当鼠标进入lable控件时,就会触发Panel的MouseLeave事件(因为这个时候鼠标还没有离开控件的,所以我不想触发,),并且这个事件在lable的MouseEnter前面触发,所以我也无法通过逻辑来判断,并且MouseEnter和MouseLeave没有鼠标位置信息
      

  12.   

    我原本把enabled设为false就可以了,但是文字颜色又不能修改
      

  13.   


    protected override void OnMouseLeave(EventArgs e)
    {    if(this.ClientRectangle.Contains(this.PointToClient(Control.MousePosition)))
             return;
        else
        {
            base.OnMouseLeave(e);
        }
    }http://stackoverflow.com/questions/2576021/mouseenter-and-mouseleave-events-from-a-panel-and-its-child-controls
      

  14.   

    可以再Label的mouseenter事件里强制触发control的事件!