我有零異常錯誤創建此自定義控件時發送的ValueChanged()事件,並測試它在客戶端:然後在測試形式如何在控件實例化後立即發送自定義事件消息?
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();
}
}
}
:
自定義控件的來源
private void Form1_Load(object sender, EventArgs e)
{
userControl11.Value = 100;
}
private void userControl11_ValueChanged()
{
MessageBox.Show(userControl11.Value.ToString());
}
或代替的Form_Load,在構造函數中做到這一點:
private void InitializeComponent()
{
this.userControl11 = new customevent.UserControl1(100);
非常感謝INotifyPropertyChanged的建議,不知道怎麼用它了,但似乎很有趣的滿足我的需求。 – user310291 2010-12-07 22:18:35