新建一个form窗口,拖一个button上去,点击按钮, 弹出对话框,显示窗口的name
请问,这个点击事件是怎样写

解决方案 »

  1.   


            private void button1_Click(object sender, EventArgs e)
            {
                MessageBox.Show(this.Name);
            }
      

  2.   


    public Form1()
    {
        InitializeComponent();
        this.button1.Click += new EventHandler(button1_Click);
    }private void button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show(this.Text);
    }
      

  3.   

    为什么不可以写成:MessageBox.Show(Form1.Text);
      

  4.   


    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                MessageBox.Show(this.Name);
            }
        }测试已通过编译!
      

  5.   

    或者说:
    新建一个form窗口,拖2个button上去,点击button1按钮, 弹出对话框,显示button2的name
    请问,这个点击事件是怎样写
      

  6.   

    窗体的Name 和 Text是两个概念
    Name:窗体名称
    Text:窗体标题
      

  7.   

    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                MessageBox.Show(this.button2.Name);
            }
        }
      

  8.   


    private void button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show(this.button2.Name);
    }
      

  9.   

    this指当前对象,在这里就是指当前的Form,this.button2就是当前窗口的button2……
    建议你先学学C#的基础吧……
      

  10.   

    this:表示当前对象,比如在Form1类里新建的对象都可以用this个表示
    如:按钮,文本框等
    还有其它类也一样,只要在类里都表示类中成员
    补充一下:MessageBox.Show(Form1.Text);
    用法错误:
    必须先实例化再取值
    Form1 frm=new Form1();
    MessageBox.Show(frm.Text);