initializeComponent还真脆弱

本文解决了在Form1.Designer.cs文件中直接添加事件处理程序导致的错误,并展示了如何在Form1.cs构造函数中正确地添加事件处理程序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我在Form1.Designer.cs中添加了匿名委托

    this.btnEvent.Click += new System.EventHandler(this.MyEventClick);//a event of mine
    this.btnEvent.Click += new System.EventHandler(delegate(object sender, EventArgs e)
            {
                MessageBox.Show("I have no name", "AnonymousMethodOfbtnEvent", MessageBoxButtons.OKCancel);
            });
结果报告了下面的错误:

“The designer cannot process the code at line 90:(这里是我上面的代码)
The code within the method 'InitializeComponent' is generated by the designer and should not be manually modified.Please remove any changes and try opening the designer again.”
意思是“设计器无法处理第90行的代码:(这里是我上面的代码)。方法“initializeComponent”内的代码由设计器生成,不应手动修改。请移除任何更改,然后尝试重新打开设计器”。
所以我只好移往Form1.cs:         
    public Form1()
    {
        InitializeComponent();
        btnEvent.Click += new System.EventHandler(delegate(object sender, EventArgs e)
        {
            MessageBox.Show("I have no name", "AnonymousMethodOfbtnEvent", MessageBoxButtons.OKCancel);
        });
    }

不过它说得吓人--“请移除任何更改,”,结果我加行中规中矩的代码,也没问题:

this.btnEvent.Click += new System.EventHandler(this.MyEventClick);//a event of mine

转载于:https://www.cnblogs.com/Oneirictouch/archive/2009/12/03/1616516.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值