[英]Windows Forms application, how to communicate between custom controls?
I have three user controls. 我有三个用户控件。 Here are the description of them: 1) First user control(ucCountry) contains combobox which displays country names from an xml file.
下面是它们的描述:1)第一个用户控件(ucCountry)包含组合框,该组合框显示xml文件中的国家/地区名称。
2) Second user control(ucChannelType) contains two radio buttons one to select TV and other to select Radio Channel Type. 2)第二个用户控件(ucChannelType)包含两个单选按钮,一个用于选择TV,另一个用于选择Radio Channel Type。
3) Third usercontrol(ucChannels) will populate all the channels where country name is provided by ucCountry and type provided by ucChannelType 3)第三个usercontrol(ucChannels)将填充ucCountry提供国家名称和ucChannelType提供类型的所有通道
Now, how to communicate between these user control in a form. 现在,如何以表格形式在这些用户控件之间进行通信。 I need to decouple the usercontrols from the form.
我需要将用户控件与表单分离。 So, I need to use events.
因此,我需要使用事件。 But if ucCountry fires an event (say CountryChanged event) and ucChannels subscribe the event, how to get the channel type from ucChannelType.
但是,如果ucCountry触发一个事件(例如CountryChanged事件),而ucChannels订阅了该事件,则如何从ucChannelType获取通道类型。
Thanks in advance... 提前致谢...
best solution is to add properties to your custom controls. 最好的解决方案是将属性添加到自定义控件。 there may be no fields in background, the getters will just get data from property of internal standard control.
后台可能没有字段,getter只会从内部标准控件的属性中获取数据。
You could either have a property on ucCountry
which provides the currently selected country. 您可以在
ucCountry
上拥有一个提供当前所选国家/地区的属性。 Something like: 就像是:
public Country SelectedCountry
{
get
{
return (Country) myComboBox.SelectedItem;
}
}
And then when the event fires, the other controls go a get the property. 然后,当事件触发时,其他控件进入get属性。
The other option is to use a custom event delegate, so the event handler for ucCountry.CountryChanged
would have a country parameter: 另一个选项是使用自定义事件委托,因此
ucCountry.CountryChanged
的事件处理程序将具有国家/地区参数:
public delegate void CountryChangedDelegate(Country sender)
public event CountryChangedDelegate CountryChanged;
Event handler in ucChannels: ucChannels中的事件处理程序:
public void ucCountry_CountryChanged(Country sender)
{
//do something with the new country
}
And wire the event to ucChannels: 并将事件连接到ucChannels:
myUcCountry.CountryChanged += new CountryChangedDelegate(ucCountry_CountryChanged);
You need to have public properties for the controls supplying data, and a public method to register events to for the control consuming the data. 您需要具有用于提供数据的控件的公共属性,并需要具有用于将事件注册到使用数据的控件的公共方法。 Here's a quick example:
这是一个简单的示例:
public static void Test()
{
var a = new A();
var b = new B();
var c = new C() {a = a, b = b};
a.OnChange += c.Changed;
b.OnChange += c.Changed;
a.State = "CA";
b.ChannelType = "TV";
}
class A
{
private string _state;
public string State
{
get { return _state; }
set
{
_state = value;
if (OnChange != null) OnChange(this, EventArgs.Empty);
}
}
public event EventHandler<EventArgs> OnChange;
}
class B
{
private string _channelType;
public string ChannelType
{
get { return _channelType; }
set
{
_channelType = value;
if (OnChange != null) OnChange(this, EventArgs.Empty);
}
}
public event EventHandler<EventArgs> OnChange;
}
class C
{
public A a { get; set; }
public B b { get; set; }
public void Changed(object sender, EventArgs e)
{
Console.WriteLine("State: {0}\tChannelType: {1}", a.State, b.ChannelType);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.