开发者

How to react to Property Changed of a winform custom control at design time

开发者 https://www.devze.com 2023-01-28 13:50 出处:网络
I can\'t find anu doc 开发者_JS百科or tutorial on how to do this ?Try this - I added an event and fired it in the set method of the property.

I can't find anu doc 开发者_JS百科or tutorial on how to do this ?


Try this - I added an event and fired it in the set method of the property.

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";
    }
}
}

EDIT: sorry if I misread the question. I just noticed you said at design time. I thought you meant adding the event by double clicking on it in the event properties section. this code allows for that, though I am not certain that is what you meant. Please clarify and maybe I can still help.

0

精彩评论

暂无评论...
验证码 换一张
取 消