Hi Ibrahim,
The UserControl needs to define an event which the parent form needs to
subscribe to.
The code below demonstrates how this can be done by inheriting a
NumericUpDown control which fires when the value is set to 2 as well as every
time the value is even. (It will fire on the initial 0 as well, but at that
time no event subscribers exists)
//parent form
protected override void OnLoad(EventArgs e)
{
MyUserControl myc = new MyUserControl();
Controls.Add(myc);
myc.ValueIs2 += new EventHandler(myc_ValueIs2);
myc.ValueIsEven += new
EventHandler<ValueEventArgs>(myc_ValueIsEven);
}
void myc_ValueIsEven(object sender, ValueEventArgs e)
{
MessageBox.Show("Value is even: " + e.Value);
}
void myc_ValueIs2(object sender, EventArgs e)
{
MessageBox.Show("The user control was set to 2");
}
// User control
public class MyUserControl : NumericUpDown
{
public event EventHandler ValueIs2;
public event EventHandler<ValueEventArgs> ValueIsEven;
public override string Text
{
get
{
return base.Text;
}
set
{
if (value == "2")
{
if (ValueIs2 != null)
ValueIs2(this, EventArgs.Empty);
}
if ((Convert.ToDecimal(value)) % 2 == 0)
{
if (ValueIsEven != null)
ValueIsEven(this, new ValueEventArgs(value));
}
base.Text = value;
}
}
}
// custom event args
public class ValueEventArgs : EventArgs
{
private string _value;
public string Value
{
get { return _value; }
set { _value = value; }
}
public ValueEventArgs(string value)
{
Value = value;
}
}
--
Happy Coding!
Morten Wennevik [C# MVP]
Ibrahim. said:
Hello,
I have a user control which is dynamically added to a Form object (mdi
child). and mdi child parent is mdi main.
usercontrol->mdi child -> mdi form.
i want to raise event in "usercontrol" and notify to the "mdiform" as shown
above.
any suggestions?
thanks.