开发者

Tag problem c# listbox

开发者 https://www.devze.com 2022-12-23 03:01 出处:网络
Hi i\'m trying to use the tag item of a listbox. heres my code. int number = 0; foreach (ListViewItem item in listBox1.Items)

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_Clic开发者_Go百科k(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 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++;
        }


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);


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)


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++; 


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消