-1

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_Cthulhu_Kid
  • 1,839
  • 1
  • 31
  • 42

2 Answers2

2

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...

Jony Adamit
  • 3,178
  • 35
  • 44
  • Thank you! I knew it was something silly. Now though when I check another tag I get an exception. – The_Cthulhu_Kid Aug 09 '12 at 19:14
  • are you stepping thru the code.. perhaps what you are trying to code for you have your existing code in the wrong event.. – MethodMan Aug 09 '12 at 19:18
  • It would help if you state which exception you're having. – Jony Adamit Aug 09 '12 at 19:21
  • Sorry again, it's an InvalidCastException, Using the debugger I see that the count for chosentags remains 0 even after the first one is selected. – The_Cthulhu_Kid Aug 09 '12 at 19:23
  • @DJ KRAZE all I am doing here is trying to get the values of the tags in order to pass them back to the proper search methods, so I am not sure how it could be the wrong event. – The_Cthulhu_Kid Aug 09 '12 at 19:26
  • 1
    ok then what does this method look like fillDocs(tags: chosenTags); – MethodMan Aug 09 '12 at 19:29
  • *public void fillDocs(List tags = null) { lvDownload.Items.Clear(); if (tags != null) { docs = docManagement.fillUp(tags: tags); } else { docs = docManagement.fillUp(); }* – The_Cthulhu_Kid Aug 09 '12 at 19:32
  • Show us how you fill your checked listbox in the first place. – Jony Adamit Aug 09 '12 at 19:33
  • It then goes on to fill up the listview based on what fillup() returns. That part works fine though. It is trying to get the tags in. I could probably just read the string out and then search for the tag name, get the id and so on but it seems to be the long way round. – The_Cthulhu_Kid Aug 09 '12 at 19:34
  • does it error in that code or on the chosenTags.Add the invalid cast is happening there probably because of the following Tag is inherited from the Control class and is System.Object type. That said, you could do a convert using the Convert class as shown below: – MethodMan Aug 09 '12 at 19:34
  • @JonyAdamnit... I added it to the main question. Also thank you for your time. Both of you. – The_Cthulhu_Kid Aug 09 '12 at 19:38
  • @DJKRAZE it errors on the chlbTags_ItemCheck, but only when more than one tag is checked. – The_Cthulhu_Kid Aug 09 '12 at 19:40
  • hey are you using a ListBox or CheckList box Iam going to add something a little cleaner where you can create your own Class that returns the Tag you can use it if it corrects your problem – MethodMan Aug 09 '12 at 19:41
  • I am using a checked listbox. You edited the code that showed how I populated it. – The_Cthulhu_Kid Aug 09 '12 at 19:42
  • @The_Cthulhu_Kid, I want to see how you fill chlbTags – Jony Adamit Aug 09 '12 at 19:44
  • I had it there but @DJKRAZE deleted it :) I'll add it again. – The_Cthulhu_Kid Aug 09 '12 at 19:45
  • sorry about that ..I didn't mean to delete that .. I was editing the wrong section I meant to add something else to show you.. – MethodMan Aug 09 '12 at 19:45
  • @DJKRAZE no worries! I really appreciate the help. – The_Cthulhu_Kid Aug 09 '12 at 19:46
  • check out the additional code I added to the bottom of my initial answer... if you need to get at the Checked Items then see if listBox.Items has a listBox.Items.Checked Property – MethodMan Aug 09 '12 at 19:47
  • Pause in Debug, and go through all the items in the checked list box. A cast exception could only mean there is an item that is not of type Tag. – Jony Adamit Aug 09 '12 at 19:53
  • Try the new added line I commented out the original line of code of yours, let me know if it works.. – MethodMan Aug 09 '12 at 19:56
  • Also, please note that fillDocs will always add every checked item all over again. You should either use a different event than ItemCheck, or provide some mechanism to make sure you only add each Checked item once. – Jony Adamit Aug 09 '12 at 19:58
  • Dammit - I am not filling it with the Tag, rather the tag.val, the string. Thank you so much. A case of forest for the trees. – The_Cthulhu_Kid Aug 09 '12 at 20:02
  • as to the ItemCheck, what will eventually happen is that the listbox should be updated to show only tags that are used with the tag that is selected. So if 'c#' was selected then only the tags that turn up in documents with the 'c#' tag will be shown. When 2 tags are checked then only those that are in docs with both those tags and so on. – The_Cthulhu_Kid Aug 09 '12 at 20:04
  • You're welcome.. I'd recommend implementing some binding mechanism to make the code more clean and manageable.. but I guess this is a conversation for a different thread :-) Good luck! – Jony Adamit Aug 09 '12 at 20:09
  • look at trying to get at the Tag.String property like this // checkBox is CheckBox string s = checkBox.Tag.ToString(); as an example – MethodMan Aug 09 '12 at 20:14
  • @DJKRAZE, you shouldn't edit other people's answers in order to change it's meaning. Especially if you are incorrect. You have your own answer for this. I hope you don't do this often at Stack Overflow. – Jony Adamit Aug 10 '12 at 15:52
  • What are you talking about Jony..? – MethodMan Aug 10 '12 at 16:02
1

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

MethodMan
  • 18,625
  • 6
  • 34
  • 52
  • The Tag class is automatically generated through Entity Framework. So the tags I am using are all actually in the db and linked through a table/class allocateDocumentTags – The_Cthulhu_Kid Aug 09 '12 at 19:49
  • Oh I didn't realize that.. I see where your casting error is then.. you would want something like this Tag tag = (Tag) item.Tag; I will edit the code to fit your example give me a second – MethodMan Aug 09 '12 at 19:50
  • I still get the exception. The thing is, I am filling the checked listbox with tags anyway, so should I not be able to retrieve tham like that right away? – The_Cthulhu_Kid Aug 09 '12 at 19:58
  • first question .. do you see the tags in the listbox from there you need to get at the listview items or the items that are selected and or checked... – MethodMan Aug 09 '12 at 20:11
  • In reply to your comment above, how can I do that when the Tag carries an ID and a value? Is there a way to display the value of the tag and still have the checked listbox hold the whole thing? – The_Cthulhu_Kid Aug 09 '12 at 20:18
  • so you want to get at the Integer value of the Tag, you need to convert that / cast it for example int number = Convert.ToInt32(item.Tag) does this make since – MethodMan Aug 09 '12 at 20:22
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/15146/discussion-between-dj-kraze-and-the-cthulhu-kid) – MethodMan Aug 09 '12 at 20:24