我是楼主自己琢磨了一下。
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;
}
代码没全贴,大概就是这么个意思。还是在主窗口堆积了很长的代码。我就像放到类里面。但是不想那种全动态创建控件实现,就是利用已经托到窗口上面的控件示例。不知该怎么搞。尝试在类里面,引用窗口,不知道怎么写,怎么样类,能获得窗口的引用,如果能获得这个引用,应该就能在类里面操控这些组件了。最终就是想实现,基本上在主窗口,一句代码,就完成效果。
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;
}
代码没全贴,大概就是这么个意思。还是在主窗口堆积了很长的代码。我就像放到类里面。但是不想那种全动态创建控件实现,就是利用已经托到窗口上面的控件示例。不知该怎么搞。尝试在类里面,引用窗口,不知道怎么写,怎么样类,能获得窗口的引用,如果能获得这个引用,应该就能在类里面操控这些组件了。最终就是想实现,基本上在主窗口,一句代码,就完成效果。
解决方案 »
- 请教两个问题,一个是DataTable.Rows.Count,一个关于遍历
- 急!C#写一个从execl文件中读取数据往SQL server中写的程序
- 水晶报表问题
- IIS连接远程的oracle数据库出错。
- 问一个下载相关的问题。
- 欢迎研究.net(C#)的朋友进来,大家一起学习,一起进步。C#天空,群号:14149080
- 想见见上海的程序员
- 关于从远程服务器下载文件
- 高手们,如何在frontpage2000或dreamweaver mx网页中实现realplayer播放视频的功能,调用控件如何设置?谢谢了.
- 如何判断textbox内容为有效的数字?谢谢!
- 捕捉系统键盘输入
- 如何截取字符串中的数据,并一条条插入dataGridView中
给这些控件的Tag属性赋予不同的这个类
使用相同的事件方法,在方法里读取Tag属性实现你想要的东西
Label LabelClick = (Label)sender;
LabelClick.BackColor = SystemColors.Info;
你说的这个我知道,我都不用tag,直接name更省事。现在就是不知道,这个类怎么建。怎么把控件的引用传进去。
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); }
}
}
谁便写写的,大概就是这样
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,那类该怎么写。不想动态创建,不能可视化编辑。
你编译成功后,就可以在设计器上拖MyLabel控件了。
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;
}
我这个写法是对的。唉,就是不能把这些代码单独写到一个类里面去。
{
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; //<--
}
...
}