jueves, 26 de marzo de 2015

CheckListBox & List & Classes

public class Tax
{
    public string Description { get; set; }
    public bool Included { get; set; }
    public Tax(string descrip, bool incl)
    {
        Description = descrip;
        Included = incl;
    }
}

private void btLoadCheckListBox_Click(object sender, EventArgs e)
{
    List<Tax> lstTaxes = new List<Tax>();

    lstTaxes.Add(new Tax("Uno", true));
    lstTaxes.Add(new Tax("Dos", false));
    lstTaxes.Add(new Tax("Tres", true));

    int c = 0;

    foreach (var tx in lstTaxes)
    {

        checkedListBox1.Items.Add(tx.Description);
        checkedListBox1.SetItemChecked(c, tx.Included);
        c++;
    }
}

private void btLoadList_Click(object sender, EventArgs e)
{
    List<Tax> lstTaxes = new List<Tax>();

    for (int i = 0; i < checkedListBox1.Items.Count; i++)
    {
        if (checkedListBox1.GetItemChecked(i))
            lstTaxes.Add(new Tax((string)checkedListBox1.Items[i], true));
        else
            lstTaxes.Add(new Tax((string)checkedListBox1.Items[i], false));
    }
}