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";
}


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

...全文
344 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;
        }
代码没全贴,大概就是这么个意思。还是在主窗口堆积了很长的代码。我就像放到类里面。但是不想那种全动态创建控件实现,就是利用已经托到窗口上面的控件示例。不知该怎么搞。 尝试在类里面,引用窗口,不知道怎么写,怎么样类,能获得窗口的引用,如果能获得这个引用,应该就能在类里面操控这些组件了。最终就是想实现,基本上在主窗口,一句代码,就完成效果。
内容概要:本文围绕不确定环境下的多式联运路径优化问题展开研究,提出并实现了基于AFO算法、遗传算法(GA)和粒子群优化算法(PSO)的三种智能优化方法,并借助Matlab平台完成算法编程与仿真。研究构建了考虑时间、成本、转运风险等多重不确定因素的路径优化模型,系统比较了AFO、GA、PSO三种算法在收敛速度、全局寻优能力和稳定性方面的表现,同时引入Matlab自带的全局优化搜索器作为基准对照,深入分析各算法在复杂物流网络中的适用边界与性能差异。研究表明,AFO算法在解决此类组合优化问题时展现出更快的收敛效率和更强的局部规避能力。; 适合人群:具备一定Matlab编程基础与运筹优化知识,从事物流工程、交通运输规划、智能算法开发等相关领域的研究生、科研人员及工程技术人员。; 使用场景及目标:①应用于多式联运、综合货运网络中的路径决策支持系统构建;②为不确定性条件下复杂路径规划问题提供智能算法选型依据与技术实现方案;③支持科研人员复现主流优化算法并开展横向性能对比实验,推动算法改进与实际落地。; 阅读建议:建议读者结合提供的Matlab代码逐模块分析算法实现流程,重点理解目标函数设计、约束条件处理及参数敏感性分析部分,可通过调整问题规模与算法参数进行对比实验,进一步拓展至动态路径规划或大规模网络优化等延伸场景。
内容概要:本文研究了基于QLearning自适应强化学习的PID控制器在自主水下航行器(AUV)运动控制中的应用,通过Matlab代码实现了控制算法的仿真验证。该方法融合强化学习的在线自适应能力与传统PID控制的稳定性优势,利用QLearning算法动态优化PID控制器的比例、积分、微分参数,以应对水下复杂流体环境、模型不确定性及外部干扰等挑战,从而提升AUV轨迹跟踪的精度、鲁棒性与动态响应性能。文中系统阐述了AUV的六自由度非线性动力学建模过程、QLearning算法的状态空间与动作空间设计、奖励函数构造及训练机制,并详细说明了PID参数自整定的闭环控制架构。仿真结果表明,相较于传统固定参数PID控制器,该智能控制策略在多种工况下均展现出更优的控制效果,有效抑制了超调,加快了响应速度,并增强了抗干扰能力。; 适合人群:具备自动控制理论、强化学习基础及Matlab/Simulink仿真能力,从事水下机器人、智能控制、海洋工程、自动化等领域的研究生、科研人员及工程技术人员。; 使用场景及目标:①应用于AUV、UUV等无人水下平台的高精度自主导航与运动控制;②为解决非线性、强耦合、时变系统的控制器参数自适应整定问题提供智能化解决方案;③作为强化学习与经典控制理论深度融合的技术范例,推动智能控制算法在海洋装备中的工程化应用。; 阅读建议:建议读者结合提供的Matlab代码深入理解算法实现细节,重点剖析QLearning的状态-动作-奖励机制设计、PID参数更新逻辑及仿真对比实验结果,有条件者可在更复杂的动力学模型或实际硬件平台上进一步验证与优化算法性能。

111,129

社区成员

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

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

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