我的写法是这样的:
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);
    }
}

解决方案 »

  1.   

    就是拿记事本来写,然后再命令行编译并调试。请教给位高手还有没有别的方法来做这个题目呀?我在这里写了button1.Click += new EventHandler(button1_Click); 因为我只使用过EventHandler(Object sender,EventArgs e),面试官问我能否用自定义的委托来实现?请问如何实现呢?是不是要用自定义委托传递参数(就是textBox1的值)?
      

  2.   

    想考你让你自定义个委托类比如
    public delegate void ActionHandler(string args);
    public event ActionHander MYCLICK;
      

  3.   

    public delegate void ActionHandler(string args); 
    public event ActionHander MYCLICK; 
    如果这样做,如何将MYCLICK和button1的Click事件联系到一起呀,单击按钮 这个事件是怎么激发的呀
      

  4.   

    呵呵,你让他自己写写看没IDE环境,光大小写就让人头痛如果IDE环境,这个代码又简单了,因为有智能提示,关联事件 +=后面的事件按下tab键IDE自己就完成了so,我就不明白他想考啥了,是想靠你的大小写吗?
      

  5.   

    好无聊的题目,出题目的人还停留在用notebook打出新浪网用来孤芳自赏的层次
      

  6.   


    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);
            }        
        }
    }
      

  7.   

    难道MS推出.net,就是为了用记事本手写,用csc来编译么?我晕.
      

  8.   

    不会呀,我测试过了。我觉得还是wartim 的写法好!
      

  9.   

    那是 他的写法其实是在IDE环境里面写的....你就不会到IDE写了以后在用记事本复制粘贴!
      

  10.   

    你没注意他的排版吗?明显是在IDE里写的...再说了,.NET的优势就是RAD,手写?半吊子才想得出来...
      

  11.   

    vrhero 所言极是啊,可是人家出这样的面试题目你作为应聘者怎么总不能不写吧,真是头大
      

  12.   


    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编辑的。