简体   繁体   English

我如何订阅用户控件的事件

[英]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. 用户控件包含文本框,组合框或复选框以及get值方法,以返回其相应控件的值。 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. 通常我在窗体上有一个按钮,其被点击的事件调用usercontrols getValue函数,但是现在我需要在用户控件控件更改事件发生时在窗体上发生某些事情。 Something like the following. 像下面这样的东西。 In form1.cs 在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. 我正在使用VS 2005。

here is the code in one of my usercontrols. 这是我的一个用户控件中的代码。 txtValue is a textbox txtValue是一个文本框

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;
    }
}
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. 说到属性,不要像刚才那样在C#中编写单独的Get / Set方法。 If you find yourself doing that, you probably want to use a property instead, which will enforce the correct get/set semantics automatically: 如果发现自己这样做了,则可能要改用一个属性,该属性将自动强制正确的get / set语义:

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

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. 据我了解,您使用的用户控件不会在事件的值更改时触发事件,因此您不能仅订阅某些“ ValueChanged”事件。 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. 可能的解决方案可能是在usercontrols的“Controls”集合中找到您感兴趣的控件(Combobox,Textbox等),并直接订阅其相应的事件。

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)). Changed是您通过控件中的属性公开的公共事件,其中包含委托的类型(void(object sender,EventArges e)​​)。 You can look up how to publish the event on msdn - there is plenty of articles on that. 你可以查看如何在msdn上发布这个事件 - 有很多关于这个的文章。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM