如何刚过控制实例发送自定义事件消息? [英] How to send custom event message just after control instantiation?

查看:100
本文介绍了如何刚过控制实例发送自定义事件消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有空异常错误创建此自定义控件时发送的ValueChanged()事件,并在客户端测试它:



自定义控件

来源:



 使用系统; 
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Drawing中;
使用System.Data这;
使用System.Text;使用System.Windows.Forms的
;

命名空间自定义事件
{
[DefaultEvent(的ValueChanged)]
公共部分类的UserControl1:用户控件
{
私人诠释m_value;

公众委托无效ValueChangedHandler();
[类别(行动)]
[说明(价值的变化。)]
公共事件ValueChangedHandler的ValueChanged;

公共int值
{
{返回m_value; }
集合{
m_value =价值;
的ValueChanged();
}
}

公众的UserControl1()
{
的InitializeComponent();
}
公众的UserControl1(INT iValue)
{
THIS.VALUE = iValue;
的InitializeComponent();
}

}
}

然后在考试形式:

 私人无效Form1_Load的(对象发件人,EventArgs五)
{
userControl11.Value = 100;
}

私人无效userControl11_ValueChanged()
{
MessageBox.Show(userControl11.Value.ToString());
}

或代替的Form_Load,在构造函数中做到这一点:

 私人无效的InitializeComponent()
{
this.userControl11 =新customevent.UserControl1(100);


解决方案

您应声明的事件处理这样的:

 公共事件的ValueChanged事件处理程序; 

受保护的虚拟无效OnValueChanged(对象发件人,EventArgs五)
{
如果(的ValueChanged!= NULL)
{
的ValueChanged(发件人,E) ;
}
}

公共int值
{
{返回m_value; }
集合{
如果(m_value ==值)回报;
m_value =价值;
OnValueChanged(这一点,EventArgs.Empty);
}
}



PS:还有一个接口INotifyPropertyChanged的,你应该使用这个代替符合标准.NET数据绑定规则。


I have null exception error on sending ValueChanged() event when creating this custom control and testing 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.

这篇关于如何刚过控制实例发送自定义事件消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆