经过高人指教之后的代码:
using System;
using System.Windows.Forms;
using System.Drawing;
namespace MyApplication
{
public partial class Form1 : Form
{
private delegate void ShowText();
TextBox textBox1 = new TextBox();
Button button1 = new Button();
public Form1()
{
textBox1.Text = "Hello world!";
textBox1.Location = new Point((Width - textBox1.Width) / 3, (Height - textBox1.Height) / 3);
textBox1.Parent = this;
button1.Text = "button1";
button1.Location = new Point(textBox1.Left + textBox1.Width + 8, textBox1.Top);
button1.Click += new EventHandler(button1_Click);
button1.Parent = this;
this.ShowDialog();
}
void button1_Click(Object sender, EventArgs e)
{
Invoke(new ShowText(DoShowText));
}
void DoShowText()
{
MessageBox.Show(textBox1.Text);
}
static void Main()
{
Form1 form1 = new Form1();
}
}
}