I am trying to filter documents based on selected tags in a checkedlistbox -- it is populated with objects of my class Tag -- but am unable to access the items in order to search. I have tried a couple of variations but the method I am using just now is:
private void chlbTags_ItemCheck(object sender, ItemCheckEventArgs e)
{
List<Tag> chosenTags = new List<Tag>();
foreach (object item in chlbTags.CheckedItems)
{
chosenTags.Add((Tag)item);
}
fillDocs(tags: chosenTags);
}
I know it is probably something simple but all I seem to find when I search seems to be related to getting strings back.
EDIT: chosenTags is always null no matter how many tags are checked.
EDIT 2: Thanks to @Jony A damn it... this has been partly sorted. But now I can't check more than one tag without throwing an InvalidCastException.
EDIT 3: How the checked listbox is populated.
public static List<Tag> fillUsed(List<int> docIds = null)
{
List<Tag> used;
if (docIds == null)
{
used = (from t in frmFocus._context.Tags
where t.AllocateDocumentTags.Count > 0
select t).ToList();
}
else
{
used = (from id in docIds
join adt in frmFocus._context.AllocateDocumentTags on
id equals adt.documentId
join t in _tags on adt.tagId equals t.id
select t).ToList();
}
return used;
}
Any help is appreciated, thanks.
This portion works
public void fillDocs(List<Tag> tags = null)
{
lvDownload.Items.Clear();
if (tags != null)
{
docs = docManagement.fillUp(tags: tags);
}
else
{
docs = docManagement.fillUp();
}
}