SOS......请大侠慷慨相助

c用s户d昵n称 2010-09-14 06:40:30
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
((Button)sender).Click += new EventHandler(YunSuan);
}

private void YunSuan(object sender, EventArgs e)
{

textBox1.Text += ((Button)sender).Text;
}

}
}

为什么第一次按 button(button.text=1) textBox1.Text 显示的是 1
第二次同样按 button(button.text=1) textBox1.Text 显示的是 111 而不是11
也就是说 同样的按钮 你按几次 textBox1.Text 就增加了相同次数的1 而不是只增加 一个1
why?
...全文
165 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
kensouterry 2010-09-14
  • 打赏
  • 举报
回复
哦 是按钮的话就是a+=b
b为按钮上面text中1的个数,然后b是一个常量
c用s户d昵n称 2010-09-14
  • 打赏
  • 举报
回复
看两位高手的解释
貌似 愚昧的我 有点懂了......
wuyq11 2010-09-14
  • 打赏
  • 举报
回复
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text += ((Button)sender).Text;
}
c用s户d昵n称 2010-09-14
  • 打赏
  • 举报
回复
把((Button)sender).Text整体看作textBox1.Text 此话何解?

((Button)sender).Text 不是点击那个按钮时 取得那个按钮的值吗?
风尘浪子 2010-09-14
  • 打赏
  • 举报
回复
把 ((Button)sender).Click += new EventHandler(YunSuan);直接写在外面,令双方不会相互影响。
class Form
{
button1.Click+=new EventHandler(click2);
....
public void click2(object sender,EventArgs e)
{........}
}
kensouterry 2010-09-14
  • 打赏
  • 举报
回复
不知道你的需求是什么,无法改动!
弄清楚你的需求然后再开始编码。
kensouterry 2010-09-14
  • 打赏
  • 举报
回复
textBox1.Text += ((Button)sender).Text;

把((Button)sender).Text整体看作textBox1.Text。将textBox1.Text设为a,代入原式,即得
a+=a;
同样:a=a+a;
注意,你这里是字符串的拼接,而不是数学运算,所以
1+=1 "11"
11+=11 "1111"
1111+=1111 "11111111"
。。。。。。。。

不知道分析过程有没有错?呵呵,请多指教!
c用s户d昵n称 2010-09-14
  • 打赏
  • 举报
回复
那高手......应该怎么改好呢......请赐教
风尘浪子 2010-09-14
  • 打赏
  • 举报
回复
不要学这个歪道,因为编程的时候没有人会这样做的,可能只会学校考试的时候才会把这些端上考卷。
风尘浪子 2010-09-14
  • 打赏
  • 举报
回复
还不明白吗,当你单击button1时
((Button)sender).Click += new EventHandler(YunSuan);
为button1的click事件多添加一个处理方法,这时候YunSuan(object ,EventArgs)被触发一次。
也就是以后button1_click()和YunSuan()将同步运行。
但你偏偏把这个写在button1_click()里面,就是说,这个时候正正button1_click()被触发的时候,YunSuan()又会同步运行一次。
c用s户d昵n称 2010-09-14
  • 打赏
  • 举报
回复
我要详细的 解释过程......
thank you..... 老师也说过......不过,我没听懂
风尘浪子 2010-09-14
  • 打赏
  • 举报
回复
private void YunSuan(object sender, EventArgs e)
{

textBox1.Text += ((Button)sender).Text;
}

111,129

社区成员

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

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

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