views:

190

answers:

3

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.

2) Second user control(ucChannelType) contains two radio buttons one to select TV and other to select Radio Channel Type.

3) Third usercontrol(ucChannels) will populate all the channels where country name is provided by ucCountry and type provided by 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.

Thanks in advance...

+2  A: 

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.

Andrey
+1 This is the recommended solution by Microsoft.
Will Marcouiller
A: 

You could either have a property on ucCountry which provides the currently selected country. Something like:

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:

public delegate void CountryChangedDelegate(Country sender)

public event CountryChangedDelegate CountryChanged;

Event handler in ucChannels:

public void ucCountry_CountryChanged(Country sender)
{
    //do something with the new country
}

And wire the event to ucChannels:

myUcCountry.CountryChanged += new CountryChangedDelegate(ucCountry_CountryChanged);
adharris
A: 

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);
    }
}
Payton Byrd