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)First user control(ucCountry)包含combobox,它显示来自xml文件的国家名称。
2) Second user control(ucChannelType) contains two radio buttons one to select TV and other to select Radio Channel Type.
2)第二用户控制(ucChannelType)包含两个单选按钮,一个选择电视,另一个选择广播通道类型。
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.
现在,如何在表单中这些用户控件之间进行通信。我需要将usercontrols与表单分离。我需要使用事件。但是如果ucCountry触发一个事件(比如CountryChanged事件)并ucChannels订阅该事件,那么如何从ucChannelType获取通道类型。
Thanks in advance...
提前谢谢…
3 个解决方案
#1
3
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只会从内部标准控件的属性中获取数据。
#2
2
You could either have a property on ucCountry
which provides the currently selected country. Something like:
您可以在ucCountry上拥有一个属性,该属性提供当前选择的国家。喜欢的东西:
public Country SelectedCountry
{
get
{
return (Country) myComboBox.SelectedItem;
}
}
And then when the event fires, the other controls go a get the property.
然后当事件触发时,其他控件去获取属性。
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:
并将事件连接到ucchannel:
myUcCountry.CountryChanged += new CountryChangedDelegate(ucCountry_CountryChanged);
#3
0
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);
}
}
#1
3
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只会从内部标准控件的属性中获取数据。
#2
2
You could either have a property on ucCountry
which provides the currently selected country. Something like:
您可以在ucCountry上拥有一个属性,该属性提供当前选择的国家。喜欢的东西:
public Country SelectedCountry
{
get
{
return (Country) myComboBox.SelectedItem;
}
}
And then when the event fires, the other controls go a get the property.
然后当事件触发时,其他控件去获取属性。
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:
并将事件连接到ucchannel:
myUcCountry.CountryChanged += new CountryChangedDelegate(ucCountry_CountryChanged);
#3
0
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);
}
}