我是楼主自己琢磨了一下。
       public string nowtabName = ""; //当前选项卡
        private void Form_init()
        {
            this.bt_lb.MouseLeave += new EventHandler(tab_lb_MouseLeave);
            this.bt_lb.MouseEnter += new EventHandler(tab_lb_MouseEnter);
            this.bt_lb.MouseDown  += new MouseEventHandler(tab_lb_MouseDown); 
        }
        private void tab_lb_MouseLeave(object sender,EventArgs e)
        {
            Label lb = (Label)sender;
            if (this.nowtabName != lb.Name) 
            {
                if (lb.Name == "bt_lb") { lb.Image = Properties.Resources.bt1; }
                if (lb.Name == "img_lb") { lb.Image = Properties.Resources.img1; }
            }
        }
        private void tab_lb_MouseEnter(object sender, EventArgs e)
        {
            Label lb = (Label)sender;
            if (this.nowtabName != lb.Name)
            {
                if (lb.Name == "bt_lb") { lb.Image = Properties.Resources.bt2; }
                if (lb.Name == "img_lb") { lb.Image = Properties.Resources.img2; }
            }  
        }
        private void tab_lb_MouseDown(object sender, EventArgs e)
        {
            Label lb = (Label)sender;
            if (this.nowtabName != lb.Name)
            {
                if (lb.Name == "bt_lb") { lb.Image = Properties.Resources.bt3; }
                if (lb.Name == "img_lb") { lb.Image = Properties.Resources.img3; }
            }
            this.nowtabName = lb.Name;
        }
代码没全贴,大概就是这么个意思。还是在主窗口堆积了很长的代码。我就像放到类里面。但是不想那种全动态创建控件实现,就是利用已经托到窗口上面的控件示例。不知该怎么搞。尝试在类里面,引用窗口,不知道怎么写,怎么样类,能获得窗口的引用,如果能获得这个引用,应该就能在类里面操控这些组件了。最终就是想实现,基本上在主窗口,一句代码,就完成效果。

解决方案 »

  1.   

    自己弄一个类,类里面弄些你需要的属性
    给这些控件的Tag属性赋予不同的这个类
    使用相同的事件方法,在方法里读取Tag属性实现你想要的东西
      

  2.   

    然后编程代码比如:            
    Label LabelClick = (Label)sender;
    LabelClick.BackColor = SystemColors.Info;
      

  3.   


    你说的这个我知道,我都不用tag,直接name更省事。现在就是不知道,这个类怎么建。怎么把控件的引用传进去。
      

  4.   

    你也可以在前台用javascript的鼠标事件来控制那些个按钮的图片
      

  5.   

    汗,这个是winform。不是asp.net
      

  6.   

    CreateLables(){
      for(int i=0;i<10;i++){
        Label lb = new Label();
        lb.Text = "标签" + (i+1);
        lb.Name = "Label" + i;
        lb.Click += (s,e) =>{ MessageBox.Show((s as Label).Text); }
      }
    }
    谁便写写的,大概就是这样
      

  7.   


    public partial class Form1 : Form
    {
        FlowLayoutPanel layoutPanel = new FlowLayoutPanel() { Dock = DockStyle.Fill};
        public Form1()
        {
            for (int i = 0; i < 20; i++)
            {
                MyLabel lable = new MyLabel()
                {
                    Text = "hello " + i,
                    Name = "MyLabel" + i,
                };
                this.layoutPanel.Controls.Add(lable);        }
            this.Controls.Add(layoutPanel);
        }
    }public class MyLabel : Label
    {
        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
            this.BackColor = Color.PeachPuff;
        }    protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            this.BackColor = SystemColors.Control;
        }    protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            this.BackColor = Color.Plum;
        }
    }
      

  8.   


    这个貌似可以。如果不动态创建lb,用窗口存在的lb,那类该怎么写。不想动态创建,不能可视化编辑。
      

  9.   


    你编译成功后,就可以在设计器上拖MyLabel控件了。
      

  10.   

    你这个应该是,集成了lb类。完了在循环new,重写事件方法。加入容器里面,那个容器不好用,我开始用的就是他,间距不能为0,时钟有空隙,大概3像素。我用的是,Panel做的容器。
      

  11.   

    还是有问题,那位朋友的重写lb,只是换个底色。不能换图片。一个tab标签就对应有3个图片。名字都不一样。那种写法,无法适应我这个控件吧。
      

  12.   


            private void Form_init()
            {
                this.bt_lb.MouseLeave += new EventHandler(tab_lb_MouseLeave);
                this.bt_lb.MouseEnter += new EventHandler(tab_lb_MouseEnter);
                this.bt_lb.MouseDown  += new MouseEventHandler(tab_lb_MouseDown);
                this.img_lb.MouseLeave += new EventHandler(tab_lb_MouseLeave);
                this.img_lb.MouseEnter += new EventHandler(tab_lb_MouseEnter);
                this.img_lb.MouseDown += new MouseEventHandler(tab_lb_MouseDown); 
            }
            private void tab_lb_MouseLeave(object sender,EventArgs e)
            {
                Label lb = (Label)sender;
                if (this.nowtabName != lb.Name) 
                {
                    if (lb.Name == "bt_lb") { lb.Image = Properties.Resources.bt1; }
                    if (lb.Name == "img_lb") { lb.Image = Properties.Resources.img1; }
                }
            }
            private void tab_lb_MouseEnter(object sender, EventArgs e)
            {
                Label lb = (Label)sender;
                if (this.nowtabName != lb.Name)
                {
                    if (lb.Name == "bt_lb") { lb.Image = Properties.Resources.bt2; }
                    if (lb.Name == "img_lb") { lb.Image = Properties.Resources.img2; }
                }  
            }
            private void tab_lb_MouseDown(object sender, EventArgs e)
            {
                Label lb = (Label)sender;
                if (this.nowtabName != lb.Name)
                {
                    if (lb.Name == "bt_lb") { lb.Image = Properties.Resources.bt3; }
                    if (lb.Name == "img_lb") { lb.Image = Properties.Resources.img3; }                if (this.nowtabName == "bt_lb") { this.bt_lb.Image = Properties.Resources.bt1; }
                    if (this.nowtabName == "img_lb") { this.img_lb.Image = Properties.Resources.img1; }
                }
                this.nowtabName = lb.Name;
            }
    我这个写法是对的。唉,就是不能把这些代码单独写到一个类里面去。
      

  13.   

    动态生成label 应该可以吧
      

  14.   

    public class MyLabel : Label
    {
        public Image ImageOnMouseEnter {get; set;}
        public Image ImageOnMouseLeave{get; set;}
        public Image ImageOnMouseDown{get; set;}
        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
            this.Image= ImageOnMouseEnter;  //<--
        }
        ...
    }