C# 如何在设计时对winform自定义控件的属性更改作出反应

C# 如何在设计时对winform自定义控件的属性更改作出反应,c#,winforms,C#,Winforms,我找不到有关如何执行此操作的anu文档或教程?尝试此操作-我添加了一个事件,并在属性的set方法中激发了它 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace C

我找不到有关如何执行此操作的anu文档或教程?

尝试此操作-我添加了一个事件,并在属性的set方法中激发了它

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CustomControl
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        //add a handle to the property changed event
        myButton1.OnNameChanged += new EventHandler(myButton1_OnNameChanged);
    }

    void myButton1_OnNameChanged(object sender, EventArgs e)
    {
        MessageBox.Show("My Name Changed");
    }
}


public class myButton : Button
{
    private string _Name = "";
    public event EventHandler OnNameChanged;
    public string myName
    {
        get { return _Name; }
        set
        {
            _Name = value;
            if (OnNameChanged != null)
                OnNameChanged(this,EventArgs.Empty);

            //just for Demonstrative purposes I added this so you could see the _Name actually change
            this.Text = _Name;
        }
    }

    //added this to demonstrate the name changing 
    protected override void OnClick(EventArgs e)
    {
        base.OnClick(e);
        myName = "my New Name";
    }
}
}
编辑:
对不起,如果我误读了这个问题。我刚注意到你在设计时说的。我以为你的意思是通过双击事件属性部分中的事件来添加事件。这个代码允许这样做,尽管我不确定你的意思是什么。请澄清,也许我仍然可以提供帮助。

只需重写OnXxx方法即可。如果需要,可以测试DesignMode以检查它是否在设计模式下运行。在构造函数中分配事件处理程序应该是第二个选择。