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.cs2613test_csharp
错误2事件“System.Windows.Forms.Form.Shown”只能出现在 += 或 -= 的左边F:\vsprogram\test_csharp\test_csharp\Form1.cs2626test_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); } }