![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 public partial class Form1 : Form 2 { 3 public Form1() 4 { 5 InitializeComponent(); 6 } 7 8 private void button1_Click(object sender, EventArgs e) 9 { 10 Form2 objFrm = new Form2(); 11 12 DialogResult result = objFrm.ShowDialog(); 13 if (result == DialogResult.OK) 14 { 15 this.textBox1.Text = objFrm.currBtnName; 16 } 17 } 18 }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 public partial class Form2 : Form 2 { 3 public string currBtnName; 4 public Form2() 5 { 6 InitializeComponent(); 7 AddButtons(); 8 } 9 10 private void AddButtons() 11 { 12 for (int i = 0; i < 2; i++) 13 { 14 Button objBtn = new Button(); 15 objBtn.Location = new System.Drawing.Point(277 + i * 100, 176); 16 objBtn.Name = "button" + (i + 1).ToString(); 17 objBtn.Size = new System.Drawing.Size(75, 23); 18 objBtn.TabIndex = 0; 19 objBtn.Text = "button" + (i + 1).ToString(); 20 objBtn.UseVisualStyleBackColor = true; 21 objBtn.Click += objBtn_Click; 22 this.Controls.Add(objBtn); 23 } 24 25 } 26 27 void objBtn_Click(object sender, EventArgs e) 28 { 29 Button objBtn = (Button)sender; 30 MessageBox.Show(objBtn.Name); 31 32 currBtnName = objBtn.Name; 33 } 34 35 private void button1_Click(object sender, EventArgs e) 36 { 37 this.DialogResult = DialogResult.OK; 38 this.Close(); 39 } 40 41 private void button2_Click(object sender, EventArgs e) 42 { 43 this.Close(); 44 } 45 }