统一处理同一窗口下多个相同控件相同事件?

robinson00544 2008-05-29 10:52:26
我想题目没有说清楚,我问过这个问题,是不是结贴了就没人回答了?所以再问一次……
比如说:一个窗口下,有N个Button,一个TextBox,现想实现:点任何一个Button都能在TestBox里显示这个Button的Text属性,主要是想实现统一处理,因为有很多Button,所以想通过一种办法统一处理这个问题,省掉一个一个的添加事件……
...全文
133 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
robinson00544 2008-05-30
  • 打赏
  • 举报
回复
1楼的大哥!太感谢了!终于解决了!无以言表!我会好好研究一下的!2楼的大哥,我忘了写了,是想用C#实现……在此也深表谢意!你们的代码我会好好研究一下的。
wzy_love_sly 2008-05-29
  • 打赏
  • 举报
回复
c#还是js?
wzy_love_sly 2008-05-29
  • 打赏
  • 举报
回复
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<script>
function showid()
{
alert(event.srcElement.id+"点了!");
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="b1" type="button" value="button" OnClick="showid()" />
<input id="b2" type="button" value="button" OnClick="showid()" />
</div>
</form>
</body>
</html>
jinjazz 2008-05-29
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsApplication13
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
foreach (Control c in this.Controls)
{
if (c is Button)
{
c.Click += new EventHandler(c_Click);
}
}
}

void c_Click(object sender, EventArgs e)
{
this.textBox1.Text = ((Button)sender).Text;
}
}
}

110,534

社区成员

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

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

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