I have a userControl11 (either in winform or wpf) which has a ValueChanged custom event. If I put it in client form and in form_load set its value to 100, it will trigger the ValueChanged event. But if I set this value inside the constructor of UserControl1 the custom event won’t trigger. How can I force it to do so ?
whatever the technical reason, functionally it does make sense. If the object is initializing its value from some sources unknown to the client form and the client form has a textbox bound to this usercontrol value, it is sure convenient that it could refresh its textbox at any time including when the form loads just using one single event handler. Without this the client form has to create another initializer for this bound textbox at form load.
Below the source code of my trials 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)
{
// This will trigger ValueChanged Event
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 won't trigger ValueChanged Event
this.Value = 100;
}
public UserControl1(int iValue)
{
this.Value = iValue;
InitializeComponent();
}
}
}
From within the class you can use the On Events.
On events are protected, so you can’t trigger them from the hosting form, but from within the class you can call them. (if you want to trigger them manually from outside the calss, you’ll need to expose them with a custom method.)
Read about the specific event you are calling, and send the proper EventArgs to the event.
The On events are the methods that call the events.
The On events are protected, you can overload them, just remember to use the base function at the end.
Edit:
According to the Event Design in MSDN, each event has a matching method called OnEvent (the same name with the On prefix).
These methods are used to raise the base events.
The EventArgs that are passed are the same as the event of the same name, so if you want to see the passing arguments, then read the MSDN description of the actual event.
I would suggest to just subscribe to the event, and use breakpoints to see what the EventArgs look like.