我想做一个可以显示多幅图片的一个控件,具体做法就是在一个Panel上动态放若干个Picturebox
要实现的就是当用户将这个控件放到他的form里面,运行之后支持鼠标功能,即当鼠标进入哪个Picturebox时,该Pictruebox获得焦点;当在那个Picturebox里面点鼠标右键时,能弹出下拉菜单。
现在已经能动态生成Picturebox,但鼠标问题搞不定,我需要的是用户在运行时才有鼠标功能,而不是在设计时就有,我用的是下面的代码:
this.ListPanel.Controls[i].MouseEnter +=new EventHandler(this.Picbox_MouseEnter);
this.ListPanel.Controls[i].MouseLeave +=new EventHandler(this.Picbox_MouseLeave);
this.ListPanel.Controls[i].Paint +=new PaintEventHandler(this.Picbox_Paint);Picbox_MouseEnter、Picbox_MouseLeave、Picbox_Paint是我在类里定义的函数
这样用户使用控件时,将控件拖放到Form上面后,还没运行就已经有鼠标功能了,这不是我想要的。
请问问题出现在哪里,如何解决,谢谢

解决方案 »

  1.   

    可能是这些注册语句:this.ListPanel.Controls[i].MouseEnter +=new EventHandler(this.Picbox_MouseEnter);
    this.ListPanel.Controls[i].MouseLeave +=new EventHandler(this.Picbox_MouseLeave);
    this.ListPanel.Controls[i].Paint +=new PaintEventHandler(this.Picbox_Paint);这些代码你放到构造方法里面或Page_Load里面了的,没有放到:
    private void InitializeComponent()
    {
    this.Load += new System.EventHandler(this.Form1_Load);
    }里面.
      

  2.   

    判断控件的Site,如果为空表示运行时
    if (this.Site == null)
    {
        this.ListPanel.Controls[i].MouseEnter +=new EventHandler(this.Picbox_MouseEnter);
    this.ListPanel.Controls[i].MouseLeave +=new EventHandler(this.Picbox_MouseLeave);
    this.ListPanel.Controls[i].Paint +=new PaintEventHandler(this.Picbox_Paint);
    }
      

  3.   

    好像可以,但怎么在测试控件的时候系统会被拖得很慢?
    还有如何实现当鼠标在某个Picturebox中点击右键时能弹出一个下拉菜单,实现AddImage和DeleteImage功能
    谢谢楼上两位,控件完成之后立即给分
      

  4.   

    你这到底是WinForm还是WebForm的??如果是Winform下的,就要用DesignMode属性判断是否在设计模式下