I have userControl11 (either in winform or wpf) that has a special ValueChanged event. If I put it in a client form and set its value to 100 in form_load, it will raise a ValueChanged event. But if I set this value inside the constructor of UserControl1, the user event will not be fired. How can I make it do this?
whatever the technical reason, functionally it makes sense. If an object initializes its value from some sources unknown to the client form, and the client form has a text field attached to this usercontrol value, it is of course convenient that it can update its text field at any time, including when the form is loaded using only one single event handler. Without this, the client form must create another initializer for this associated text field when the form loads.
Below is the source code of my tests in winform:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void userControl11_ValueChanged()
{
MessageBox.Show(userControl11.Value.ToString());
}
private void Form1_Load(object sender, EventArgs e)
{
userControl11.Value = 100;
}
}
}
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;
if (ValueChanged != null)
{
ValueChanged();
}
}
}
public UserControl1()
{
InitializeComponent();
this.Value = 100;
}
public UserControl1(int iValue)
{
this.Value = iValue;
InitializeComponent();
}
}
}
source
share