怎么得到当前点击的控件的Tag值呀?

尧月 2009-02-05 07:37:53
多个按钮有不同的tag,在同一个事件中,我要确定当前点击的按钮是哪个,怎么办?
...全文
119 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
尧月 2009-02-05
  • 打赏
  • 举报
回复
ok,我试试!!谢谢~!
wuyi8808 2009-02-05
  • 打赏
  • 举报
回复
using System;
using System.Windows.Forms;

class Form1 : Form
{
Form1()
{
Button btnA = new Button();
btnA.Parent = this;
btnA.Text = "按钮&A";
btnA.Tag = "这是按钮A!";
btnA.Click += new EventHandler(ButtonClick);

Button btnB = new Button();
btnB.Parent = this;
btnB.Text = "按钮&B";
btnB.Top = 30;
btnB.Tag = "This is Button B";
btnB.Click += new EventHandler(ButtonClick);
}

void ButtonClick(object sender, EventArgs e)
{
// 这就是你要的:
string tag = (sender as Control).Tag.ToString();
MessageBox.Show(tag);
}

static void Main()
{
Application.Run(new Form1());
}
}
wuyi8808 2009-02-05
  • 打赏
  • 举报
回复
(sender as Control).Tag

111,130

社区成员

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

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

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