Jack
Jack

Reputation: 131

c# derived types to combo box

I have an abstract class

public abstract class PairingMethod : IPairingMethod
 {
    virtual string Name { get; } = "Default Pairing";

    protected ICollection<IPlayer> PlayersToPair { get; set; }


    protected PairingMethod(ICollection<IPlayer> players )
    {
        PlayersToPair = players;
    }

    public virtual void GeneratePairingsForRound(IRound round)
    {
        throw new System.NotImplementedException();
    }

}

Now I've tried to create a combo box based on all types that derive from that base class above. I created the combo box, and it uses class names as items but then when the combo box change event is triggered I need to know which derived class was selected. Then i can create an instance of that class, for generating pairings.

I tried implementing my own combo box with PairingMethods as the items but can't get it to work.

Any ideas/ suggestions?

C

Upvotes: 2

Views: 299

Answers (1)

Jack
Jack

Reputation: 131

Thanks to Mong Zhus advice i did the following

public class PairingComboBox : ComboBox
{
    private List<Type> _availableMethod = DerivedTypes.FindAllDerivedTypes<PairingMethod>();

    public PairingComboBox()
    {
        DataSource = DerivedTypes.FindAllDerivedTypes<PairingMethod>();
        DisplayMember = "Name";
    }
}

public static IPairingMethod CreateInstanceBinder
               (string pairingMethodName, ICollection<IPlayer> players)
{
         var t = Type.GetType(pairingMethodName + ",Pairings");
        return (PairingMethod)Activator.CreateInstance(t, players);
 }

I call the CreatenIstanceBuilder when the combo box changes. Passing in the players from the league.

Upvotes: 1

Related Questions