3个label,做的tab标签,怎样能统一管理他们的事件。求个大哥们个办法!

oHongZuoZuo123 2014-04-07 10:07:17


我自己做了一个模拟的选项卡标签,一共有将近10个,tab

每个tab标签的,背景图片,都有3种状态。

分别是:正常状态,对应一个图片;
按下状态,对应一个图片;按下就置当前选项卡。
鼠标进入状态,对应一个图片;
鼠标离开状态,对应一个图片,鼠标离开,如果不是当前选项卡,就恢复正常状态图片,否则就是按下状态图片。

我自己做了两个tab,每个tab都要绑定3个事件。两个就6个事件了,如果做完10个,那就30个事件了;可以想象那个窗口的代码得多长;

     
public string nowtabName = ""; //当前tab
private void bt_lb_MouseDown(object sender, MouseEventArgs e)
{
this.bt_lb.Image = Properties.Resources.bt3;
this.nowtabName = "bt_lb";
}

private void bt_lb_MouseLeave(object sender, EventArgs e)
{
if (this.nowtabName != "bt_lb") {
this.bt_lb.Image = Properties.Resources.bt1;
}
}
private void bt_lb_MouseEnter(object sender, EventArgs e)
{
if (this.nowtabName != "bt_lb")
{
this.bt_lb.Image = Properties.Resources.bt2;
}
}
//离开
private void img_lb_MouseLeave(object sender, EventArgs e)
{
if (this.nowtabName != "img_lb")
{
this.img_lb.Image = Properties.Resources.img1;
}
}
//进入
private void img_lb_MouseEnter(object sender, EventArgs e)
{
if (this.nowtabName != "img_lb")
{
this.img_lb.Image = Properties.Resources.img2;
}
}
//按下
private void img_lb_MouseDown(object sender, MouseEventArgs e)
{
this.img_lb.Image = Properties.Resources.img3;
this.nowtabName = "img_lb";
}


能不能有一种办法,把他们统一管理一下。或者弄个类什么的。真的不想在窗口里面这么多事件代码;
琢磨半天,也没个思路,办法。所以来这里,看看大哥、大侠们,能不能给小弟一个办法。如果可以的话,帮我一下吧。
也不用那种非常特别高级的解决方案,反正就是差不多,把这些代码,放到一个类里面,让窗口的代码少点。

...全文
282 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
gomoku 2014-04-08
  • 打赏
  • 举报
回复
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;  //<--
    }
    ...
}
Oreo。 2014-04-08
  • 打赏
  • 举报
回复
动态生成label 应该可以吧
oHongZuoZuo123 2014-04-08
  • 打赏
  • 举报
回复

        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;
        }
我这个写法是对的。唉,就是不能把这些代码单独写到一个类里面去。
oHongZuoZuo123 2014-04-08
  • 打赏
  • 举报
回复
还是有问题,那位朋友的重写lb,只是换个底色。不能换图片。一个tab标签就对应有3个图片。名字都不一样。那种写法,无法适应我这个控件吧。
特别安静 2014-04-08
  • 打赏
  • 举报
回复
你可以结贴啦
oHongZuoZuo123 2014-04-08
  • 打赏
  • 举报
回复
噢!那这么招,也还行。这个做法比较高级。
oHongZuoZuo123 2014-04-08
  • 打赏
  • 举报
回复
你这个应该是,集成了lb类。完了在循环new,重写事件方法。加入容器里面,那个容器不好用,我开始用的就是他,间距不能为0,时钟有空隙,大概3像素。我用的是,Panel做的容器。
gomoku 2014-04-08
  • 打赏
  • 举报
回复
引用 10 楼 oHongZuoZuo123 的回复:
这个貌似可以。如果不动态创建lb,用窗口存在的lb,那类该怎么写。不想动态创建,不能可视化编辑。
你编译成功后,就可以在设计器上拖MyLabel控件了。
oHongZuoZuo123 2014-04-08
  • 打赏
  • 举报
回复
引用 9 楼 gomoku 的回复:

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;
    }
}
这个貌似可以。如果不动态创建lb,用窗口存在的lb,那类该怎么写。不想动态创建,不能可视化编辑。
gomoku 2014-04-08
  • 打赏
  • 举报
回复

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;
    }
}
assky124 2014-04-08
  • 打赏
  • 举报
回复


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); }
  }
}
谁便写写的,大概就是这样

oHongZuoZuo123 2014-04-08
  • 打赏
  • 举报
回复
引用 6 楼 xc917563264 的回复:
你也可以在前台用javascript的鼠标事件来控制那些个按钮的图片
汗,这个是winform。不是asp.net
绿龙术士 2014-04-08
  • 打赏
  • 举报
回复
你也可以在前台用javascript的鼠标事件来控制那些个按钮的图片
oHongZuoZuo123 2014-04-08
  • 打赏
  • 举报
回复
引用 2 楼 ZIP_xG 的回复:
自己弄一个类,类里面弄些你需要的属性 给这些控件的Tag属性赋予不同的这个类 使用相同的事件方法,在方法里读取Tag属性实现你想要的东西
你说的这个我知道,我都不用tag,直接name更省事。现在就是不知道,这个类怎么建。怎么把控件的引用传进去。
spmzfz 2014-04-08
  • 打赏
  • 举报
回复
然后编程代码比如:
Label LabelClick = (Label)sender;
LabelClick.BackColor = SystemColors.Info;
spmzfz 2014-04-08
  • 打赏
  • 举报
回复
你在桌面上全部选定要共享事件的控件,然后在右边属性事件窗口中某事件中输入要共享的事件名,然后双击事件名进入代码。。。
Kation 2014-04-08
  • 打赏
  • 举报
回复
自己弄一个类,类里面弄些你需要的属性 给这些控件的Tag属性赋予不同的这个类 使用相同的事件方法,在方法里读取Tag属性实现你想要的东西
oHongZuoZuo123 2014-04-08
  • 打赏
  • 举报
回复
我是楼主 自己琢磨了一下。

       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;
        }
代码没全贴,大概就是这么个意思。还是在主窗口堆积了很长的代码。我就像放到类里面。但是不想那种全动态创建控件实现,就是利用已经托到窗口上面的控件示例。不知该怎么搞。 尝试在类里面,引用窗口,不知道怎么写,怎么样类,能获得窗口的引用,如果能获得这个引用,应该就能在类里面操控这些组件了。最终就是想实现,基本上在主窗口,一句代码,就完成效果。

111,079

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧