views:

60

answers:

3

I have a combobox in a dialog box form. I need to fill this combo with the List<> from parent form. How to do that as I cannot pass the List<> via dialog box constructor.

frmChild frm = new frmChild();
frm.ShowDialog();
+6  A: 

You can add a property or method on your form, that takes the List<items> and populates the ComboBox.

For example:

List<ItemType> items = GetItemsForFormsComboBox();
frmChild frm = new frmChild();
frm.SetComboItems(items);
frm.ShowDialog();

// in the form
public void SetComboItems(List<ItemType> items)
{
    foreach(var item in items)
    {
        myCombo.Add( /* construct combo item and use item to populate it here */ );
    }
}
Rob
+1  A: 

You may make a property of your dialog to get/set List<> data.

Arseny
A: 

You can add a property or method on your form, that takes the List and populates the ComboBox

Then overload the constructor.

public class ComboBoxWindow : Window
{
    public ComboBoxWindow (Window origin)
    {
        // Now you can access your parent window's List<>.
    }

    // If necessary you can keep a reference to it.
    private Window _origin;
}

OR

public class ComboBoxWindow : Window
{   
    // If necessary you can keep a reference to it.
    private IList _items;

    public ComboBoxWindow (IList _items)
    {
        // Now you can access your list directly.
    }
}

Both ways are okay.

{enjoy}

Micael Bergeron
The first option is not the best idea in the world, having a child window "grok" back into its parent is just asking for trouble if you try to re-use the window elsewhere, the second option, OR a property/method is by far the better option
Rob
You're right, I mean you get extra overhead using first method.Nevertheless, I used first method once with a Custom window to set a blur Effect on the caller when the window was opened. It worked out great.
Micael Bergeron