按钮的单击事件函数的参数 sender,为何名字随便写也行呢?
我为 button1 写个单击事件响应函数,
然后,我用代码调用这个函数,为何一下几种写法都行呢?//sender 参数为 null 时,
button1_Click(null, null);
//sender 为 button1 时,
button1_Click(button1, null);
//为何 sender 参数为 button2时,也会调用button1的单击事件呢?
button1_Click(button2,null);难道,sender 参数可以随便填吗,只要是控件的名就行using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace hh
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("按钮1被单击", "df", MessageBoxButtons.OK, MessageBoxIcon.Information);
} private void button2_Click(object sender, EventArgs e)
{
button1_Click(null, null);
button1_Click(button1, null);
//为何 sender 参数为 button2时,也会调用button1的单击事件呢?
button1_Click(button2,null);
}
}
}
我为 button1 写个单击事件响应函数,
然后,我用代码调用这个函数,为何一下几种写法都行呢?//sender 参数为 null 时,
button1_Click(null, null);
//sender 为 button1 时,
button1_Click(button1, null);
//为何 sender 参数为 button2时,也会调用button1的单击事件呢?
button1_Click(button2,null);难道,sender 参数可以随便填吗,只要是控件的名就行using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace hh
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("按钮1被单击", "df", MessageBoxButtons.OK, MessageBoxIcon.Information);
} private void button2_Click(object sender, EventArgs e)
{
button1_Click(null, null);
button1_Click(button1, null);
//为何 sender 参数为 button2时,也会调用button1的单击事件呢?
button1_Click(button2,null);
}
}
}
解决方案 »
- 弱弱地一下关于正则表达存入string类的问题
- 我的页面在本地测试显示正确,放到服务器上很奇怪,显示的都是程序代码
- 倾家荡产(70分)求一个PostMessage使用的实例
- C# 如何更改DataTable中rows的位置。。。
- 做一个能打印指定路径图片的C#程序
- c#如何编译一个访问网页的ftp应用程序
- datable绑定在datagrid,在得知数据table是否发生了变化。
- 做程序不用ormapping可以吗?
- 请问如何在程序中修改windows系统声音
- aforge.net 视频录入 提示错误算术运算导致溢出
- webBrowser开发中怎么读取js创建的元素呢
- 如何在PictureBox中绘制矢量图形,并且可以进行放大、旋转之类的处理?
但是有些时候需要sender来提供信息,最简单的一个例子,有100个button,点击后显示button的名字.
这时候我们可以绑定到同一事件button_Click,然后通过sender来判断是哪个button