开发者

How to send custom event message just after control instantiation?

开发者 https://www.devze.com 2023-01-29 12:59 出处:网络
I have null exception error on sending ValueChanged() event when creating this custom control and tes开发者_C百科ting it in a client:

I have null exception error on sending ValueChanged() event when creating this custom control and tes开发者_C百科ting it in a client:

Source of custom control:

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

namespace customevent
{
    [DefaultEvent("ValueChanged")]
    public partial class UserControl1 : UserControl
    {
        private int m_value;

        public delegate void ValueChangedHandler();
        [Category("Action")]
        [Description("Value changed.")]
        public event ValueChangedHandler ValueChanged;

        public int Value
        {
            get { return m_value; }
            set { 
                m_value = value;
                ValueChanged();
            }
        }

        public UserControl1()
        {
            InitializeComponent();
        }
        public UserControl1(int iValue)
        {
            this.Value = iValue;
            InitializeComponent();
        }

    }
}

Then in test form:

    private void Form1_Load(object sender, EventArgs e)
    {
       userControl11.Value = 100;
    }

    private void userControl11_ValueChanged()
    {
        MessageBox.Show(userControl11.Value.ToString());
    }

Or instead of form_load, do this in constructor:

    private void InitializeComponent()
    {
        this.userControl11 = new customevent.UserControl1(100);


You should declare the event handling as this:

public event EventHandler ValueChanged;

protected virtual void OnValueChanged(object sender, EventArgs e)
{
    if (ValueChanged != null)
    {
       ValueChanged(sender, e);
    }
}

public int Value
{
    get { return m_value; }
    set { 
        if (m_value == value) return;
        m_value = value;
        OnValueChanged(this, EventArgs.Empty);
    }
}

PS: there is an interface INotifyPropertyChanged, you should use this instead to comply with standard .NET databinding rules.


You're not checking for nulls:

    public int Value
    {
        get { return m_value; }
        set { 
            m_value = value;
            if(ValueChanged != null)
            {
               ValueChanged();
            }
        }
    }

Furthermore, you're also not hooking into this event in your Form:

private void Form1_Load(object sender, EventArgs e)
{
   userControl1.ValueChanged += userControl11_ValueChanged;
   userControl11.Value = 100;
}

private void userControl11_ValueChanged()
{
    MessageBox.Show(userControl11.Value.ToString());
}


Simon got you almost there:

protected virtual void OnValueChanged(object sender, EventArgs e)
{
    var tmp = ValueChanged;
    if (tmp != null)
    {
       tmp(sender, e); // With the tmp, we won't explode if a subscriber changes the collection of delegates underneath us.
    }
}
0

精彩评论

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