문제

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(); 
   }
}
도움이 되었습니까?

해결책

The code you posted should fail with a NullReferenceException. You should replace List<Tag> chosenTags = null; with List<Tag> chosenTags = new List<Tag>();
It should be fine then...

다른 팁

Like Jony stated This code will fail you have to do more than just assign null to the object.. you need to do what they call "NEWING" the object meaining the key word new

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: this will work if you change it.

private void chlbTags_ItemCheck(object sender, ItemCheckEventArgs e)
{
   List<Tag> chosenTags = new List<Tag>();
   foreach (object item in chlbTags.CheckedItems)
   {
      Tag tag = (Tag) item.Tag;
      chosenTags.Add(tag);  
     -- your code chosenTags.Add((Tag)item);
   }
   fillDocs(tags: chosenTags);
}

Casting has to be done by getting at the string property // checkBox is CheckBox string s = checkBox.Tag.ToString(); you can use something like this to test an individual item or items as well if you like

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top