this.label1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.label_MouseDown);this.label2.MouseDown += new System.Windows.Forms.MouseEventHandler(this.label_MouseDown);private void label_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(((Label)sender).Text==label1.Text)
{
MessageBox.Show("you click label1");
}
if(((Label)sender).Text==label2.Text)
{
MessageBox.Show("you click label2");
}
}
没什么异常啊
{
if(((Label)sender).Text==label1.Text)
{
MessageBox.Show("you click label1");
}
if(((Label)sender).Text==label2.Text)
{
MessageBox.Show("you click label2");
}
}
没什么异常啊
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace 获取消息发送者
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// panel1
//
this.panel1.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label2,
this.label1});
this.panel1.Location = new System.Drawing.Point(8, 8);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(136, 88);
this.panel1.TabIndex = 0;
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 56);
this.label1.Name = "label1";
this.label1.TabIndex = 1;
this.label1.Text = "label1";
this.label1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.OnMouseDown);
//
// label2
//
this.label2.Location = new System.Drawing.Point(24, 16);
this.label2.Name = "label2";
this.label2.TabIndex = 2;
this.label2.Text = "label2";
this.label2.MouseDown += new System.Windows.Forms.MouseEventHandler(this.OnMouseDown);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(224, 118);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.panel1});
this.Name = "Form1";
this.Text = "Form1";
this.panel1.ResumeLayout(false);
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void OnMouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
MessageBox.Show(((Control)sender).Name );
} }
}