scott
scott

Reputation: 3071

how do I subscribe to an event of a usercontrol

I have a group of usercontrols that I use multiple instances of through out my form. The usercontrols have contain either a textbox, combobox, or checkbox and a get value method to return the value of it's repective control. Usually I have a button on the form whose clicked event calls the usercontrols getValue function, but now I need for something to happen on the form whenever the usercontrols controls changed event happens. Something like the following. In form1.cs

form1.Controls.Add(UserControl1);
form1.Controls.Add(UserContorl2);
// gets called every time the combobox on UserControl1 has it's
// ValueChanged event raised
private void UserControl1_Changed(object Sender, EventArgs e)
{
    form1.property1 = UserControl1.getValue();
}
// gets called everytime the textbox on UserControl2 has it's
// textChanged event raised
private void UserControl2_Changed(object Sender, EventArgs e)
{
    form1.property2 = UserControl2.getValue();
}

I can't figure out how to throw/catch that event in form. I'm using VS 2005.

here is the code in one of my usercontrols. txtValue is a textbox

public partial class StringParameterControl : BaseParameterControl
{
    public StringParameterControl(string aName, string aValue)
        : base(aName)
    {
        InitializeComponent();
        txtValue.Text = aValue;
    }
    public StringParameterControl(string aName)
        : base(aName)
    {
        InitializeComponent();
    }
    public StringParameterControl()
        : base()
    {
        InitializeComponent();
    }
    public void SetValue(string aValue)
    {
        txtValue.Text = aValue;
    }
    public override object GetValue()
    {
        return txtValue.Text;
    }
}

Upvotes: 0

Views: 2325

Answers (3)

Joel Coehoorn
Joel Coehoorn

Reputation: 415600

UserControl1.Changed += UserControl1_Changed;

Update your control to include the following:

// A delegate type for hooking up change notifications.
// This is _what kind_ of event you want. It sets the signature your event handler methods must have.
public delegate void ChangedEventHandler(object sender, EventArgs e);

//the actual event
public event ChangedEventHandler Changed;

// Method to raise/fire the Changed event. Call this whenever something changes
protected virtual void OnChanged(EventArgs e) 
{
    ChangedEventHandler handler = Changed;
    if (handler != null) handler(this, e);
}

//and update your existing SetValue() function like so:
public void SetValue(string aValue)
{
    txtValue.Text = aValue;
    OnChanged(EventArgs.Empty);
}

You can change your event signature to pass any information you want — for example the old or new value of the property (or both). I just used the standard event arguments for the example.

And speaking or properties, don't write separate Get/Set methods in C# like you just did. If you find yourself doing that, you probably want to use a property instead, which will enforce the correct get/set semantics automatically:

public string Value 
{ 
    get { return txtValue.Text;} 
    set {txtValue.Text = value; OnChanged(EventArgs.Emtpy); } 
}

Upvotes: 2

dexter
dexter

Reputation: 7203

Or you can do with type inference style.

       UserControl.Changed = (sender, e) => this.controlFired = true; //or whatever

The Changed is the public event you expose through a property in your control with the type of the delegate (void(object sender, EventArges e)). You can look up how to publish the event on msdn - there is plenty of articles on that.

Upvotes: 0

Kevin Wienhold
Kevin Wienhold

Reputation: 331

As far as I understand the usercontrols you are using do not fire events whenever their value changes, so you can't just subscribe to some "ValueChanged" event. A possible solution might be to find the control you are interested in (Combobox, Textbox, etc.) in the usercontrols' "Controls" collection and directly subscribe to its appropriate events.

Upvotes: 0

Related Questions