我的写法是这样的:
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Collections.Generic;public class WinFormDemo
{
static void Main()
{
CreateMyForm();
} public static void CreateMyForm()
{
Form form1 = new Form(); //添加一个TextBox
TextBox textBox1 = new TextBox();
textBox1.Multiline = true;
textBox1.ScrollBars = ScrollBars.Vertical;
textBox1.AcceptsReturn = true;
textBox1.AcceptsTab = true;
textBox1.WordWrap = true;
textBox1.Text = "Hello world!";
textBox1.Name = "textBox1";
textBox1.Location = new Point(10,10); //添加button1,单击它时显示textBox1的内容
Button button1 = new Button();
button1.Text = "Show";
button1.Location = new Point(10, 40);
button1.Click += new EventHandler(button1_Click);
form1.AcceptButton = button1;
form1.StartPosition = FormStartPosition.CenterScreen;
form1.Controls.Add(button1);
form1.Controls.Add(textBox1);
form1.ShowDialog();
} public static void button1_Click(Object sender,EventArgs e)
{
Button button = (Button)sender;
Form form = button.FindForm();
MessageBox.Show(form.Controls["textBox1"].Text);
}
}
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Collections.Generic;public class WinFormDemo
{
static void Main()
{
CreateMyForm();
} public static void CreateMyForm()
{
Form form1 = new Form(); //添加一个TextBox
TextBox textBox1 = new TextBox();
textBox1.Multiline = true;
textBox1.ScrollBars = ScrollBars.Vertical;
textBox1.AcceptsReturn = true;
textBox1.AcceptsTab = true;
textBox1.WordWrap = true;
textBox1.Text = "Hello world!";
textBox1.Name = "textBox1";
textBox1.Location = new Point(10,10); //添加button1,单击它时显示textBox1的内容
Button button1 = new Button();
button1.Text = "Show";
button1.Location = new Point(10, 40);
button1.Click += new EventHandler(button1_Click);
form1.AcceptButton = button1;
form1.StartPosition = FormStartPosition.CenterScreen;
form1.Controls.Add(button1);
form1.Controls.Add(textBox1);
form1.ShowDialog();
} public static void button1_Click(Object sender,EventArgs e)
{
Button button = (Button)sender;
Form form = button.FindForm();
MessageBox.Show(form.Controls["textBox1"].Text);
}
}
public delegate void ActionHandler(string args);
public event ActionHander MYCLICK;
public event ActionHander MYCLICK;
如果这样做,如何将MYCLICK和button1的Click事件联系到一起呀,单击按钮 这个事件是怎么激发的呀
namespace MyApplication
{
public partial class Form1 : Form
{
delegate void ShowText();
TextBox textBox1 = new TextBox();
Button button1 = new Button(); public Form1()
{
textBox1.Text = "出题的人很无聊...";
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;
} void button1_Click(Object sender, EventArgs e)
{
Invoke(new ShowText(DoShowText));
} void DoShowText()
{
MessageBox.Show(textBox1.Text);
}
}
}
using System;
using System.Windows.Forms;
using System.Drawing;
class TestForm:Form{
private TextBox textBox;
private Button button;
public TestForm(){ textBox=new TextBox();
textBox.Location=new Point(100,100);
textBox.Size=new Size(300,200);
textBox.Multiline=true; button=new Button();
button.Size=new Size(100,30);
button.Location=new Point(textBox.Width/2-button.Width/2+textBox.Location.X,textBox.Location.Y+textBox.Height+30);
button.Text="测试";
button.Click+=delegate{MessageBox.Show(textBox.Text);}; this.Controls.Add(textBox);
this.Controls.Add(button);
this.Size=new Size(textBox.Location.X+textBox.Width+100,button.Location.Y+button.Height+100);
}
static void Main(string[] args){
//Console.WriteLine("Hello,world");
Application.Run(new TestForm());
}
}gvim编辑的。