views:

499

answers:

9

Hi i'm trying to use the tag item of a listbox.

heres my code.

            int number = 0;
            foreach (ListViewItem item in listBox1.Items)
            {
                Tag tag = (Tag) item.Tag;
                saveSlide(showid, tag.photoid, enumber);
                number++;
            }

problem im havin is when i run the program i get an error message sayin cannot convert type string to system.ListView but i haven't declared item as a string anywher in my program

This is where i add the items to the listbox. Please help. Im on a dead line and have sooo much more to do

private void buttonAdd_Click(object sender, EventArgs e)
{
    //add selected item into listBox
    DataRowView drv = (DataRowView)listBox1.SelectedItem;
    Tag tag = new Tag();
    string title = drv["title"].ToString();
    ListViewItem item = new ListViewItem(title);
    item.Tag = tag;
    tag.photoid = (int)drv["photoid"];

    listBox1.Items.Add(title);
}
A: 

Does Tag has a member named photoid? Maybe you need a cast in there to convert your 'tag' to whatever it's supposed to be?

        //Tag tag = (Tag) item.Tag;
        MyObject tag = (MyObject)item.Tag;
        saveSlide(showid, tag.photoid, enumber); 
        number++; 
galford13x
I'm guessing Tag is a class that Poppy has created - the cast is being done, as the Tag property just returns an object.
Andy Shellam
I think your right :)
galford13x
+2  A: 

Poppy you are adding title to listBox1.Items.

title is of type string.

So when you access it use string type like this foreach (string item in listBox1.Items).

Try. Does it help?

        int number = 0;
        foreach (string item in listBox1.Items)
        {
            Tag tag = (Tag) item.Tag;
            saveSlide(showid, tag.photoid, enumber);
            number++;
        }
TheMachineCharmer
String doesn't have a tag property.
Yuriy Faktorovich
String does not have a Tag-Property.
Bobby
I think `listBox1.Items` are of type `string`. Code in the loop might have other problems as you rightly pointed out.
TheMachineCharmer
ListBox.Items are an ObjectCollection.
Yuriy Faktorovich
Can they have with objects of type `string`?
TheMachineCharmer
Well it is an object, it could be anything.
Yuriy Faktorovich
A: 

Unless you named things weird I'd say the error is that you're trying to get a ListViewItem from a ListBox.

Don
Might have been unnecessarily short. What I mean is that one of the objects in your ListBox is not (and can not be converted) to a ListViewItem.
Don
+1  A: 

This works, you need to show the code where you add items to the list:

private class Tag
{
    public override string ToString()
    {
        return "Tag";
    }
}

ListBox listBox = new ListBox();
listBox.Items.Add(new ListViewItem { Tag = new Tag() });
foreach (ListViewItem item in listBox.Items)
{
    Tag tag = (Tag)item.Tag;
    Console.WriteLine(tag);
}

Edit following more code:

You are adding strings to your ListBox instead of the ListViewItem:

listBox1.Items.Add(title); should be listBox1.Items.Add(item);

Yuriy Faktorovich
A: 

sorry for some reason its not allowin me to comment. (I'm poppy by the way)

The error is on line 2 (the foreach line)

I have a database that contains a photoid and a title. This listbox shows the titles of selected photos (its not bound to the database because it only shows some rows) but i need to keep the photoid. Here I'm trying to retrieve the id of the items in the listbox.

poppy
A: 

Yuriy Faktorovich

where do i add that code? in the class or instead of the code i posted?

poppy
You can edit your post, and please delete this as an answer.
Yuriy Faktorovich
A: 

Here is more code.

This is where i add the items to the listbox. Please help. Im on a dead line and have sooo much more to do

     private void buttonAdd_Click(object sender, EventArgs e)
    {
        //add selected item into listBox
        DataRowView drv = (DataRowView)listBox1.SelectedItem;
        Tag tag = new Tag();
        string title = drv["title"].ToString();
        ListViewItem item = new ListViewItem(title);
        item.Tag = tag;
        tag.photoid = (int)drv["photoid"];

        listBox1.Items.Add(title);
    }
poppy
A: 

ListBox.Items is an ObjectCollection. That means you can choose the kind of object to put in it.

When you're doing this:

string title = drv["title"].ToString();
listBox1.Items.Add(title);

you are putting string objects into it, so you would need to get them out like this:

foreach (string item in listBox1.Items)

Instead, you probably want your code to be more like this:

ListViewItem item = new ListViewItem(title);
item.Tag = tag;
tag.photoid = (int)drv["photoid"];
listBox1.Items.Add(item); // The difference is here - add *item* not *title*

then you'll be able to use this the way you initially wrote it:

foreach (ListViewItem item in listBox1.Items)
JeffH
A: 

Just change the last line of code of the second code-snippet and everything will be ok, which is as follows.

listBox1.Items.Add(item);

About the Error

You added strings to the listBox as items and in the foreach an item(which is a string) is tried to convert(caste) to ListViewItem implicitly to hich doesn't work and the compiler gives the error.

Hope it will work.

inam101