日期:2014-05-18  浏览次数:21020 次

c# winform中委托的使用问题???
C# code

namespace test_csharp
{
    public partial class Form1 : Form
    {
        public   delegate void openForm(object o,EventArgs e);
        public event openForm eventOpenForm;
        public Form2 newForm=new Form2();
 
        public Form1()
        {
            InitializeComponent();
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //这样写不行
            this.newForm.Shown;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
             //这样写也不行
            //button1_Click += eventOpenForm(this.newForm, this.newForm.Shown);
        }



    }
       
}



怎么写才能在form1中单机按钮,打开form2,错误提示是

错误 1 只有 assignment、call、increment、decrement 和 new 对象表达式可用作语句 F:\vsprogram\test_csharp\test_csharp\Form1.cs 26 13 test_csharp
错误 2 事件“System.Windows.Forms.Form.Shown”只能出现在 += 或 -= 的左边 F:\vsprogram\test_csharp\test_csharp\Form1.cs 26 26 test_csharp


------解决方案--------------------
C# code

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

        private void Form1_Load(object sender, EventArgs e)
        {
            eventOpenForm += new openForm(button1_Click);
            if (eventOpenForm!=null)
            {
                eventOpenForm(sender, e);
            }
        }