C# 列表框标签问题

发布于 2024-08-25 10:36:14 字数 831 浏览 4 评论 0原文

您好,我正在尝试使用列表框的标签项。

这是我的代码。

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

我遇到的问题是,当我运行程序时,我收到一条错误消息,说无法将类型字符串转换为 system.ListView,但我没有在程序中的任何位置将项目声明为字符串,

这是我将项目添加到列表框的位置。请帮忙。我已经到了最后期限,还有很多事情要做

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

街角迷惘 2024-09-01 10:36:14

Poppy,您正在将 title 添加到 listBox1.Items

title 的类型为string

因此,当您访问它时,请使用 string 类型,例如 foreach (listBox1.Items 中的 string item)

尝试。有帮助吗?

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

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++;
        }
北座城市 2024-09-01 10:36:14

这是可行的,您需要显示向列表添加项目的代码:

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

编辑以下更多代码:

您将字符串添加到 ListBox 而不是 ListViewItem:

listBox1.Items.Add(title);应该是listBox1.Items.Add(item);

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

天气好吗我好吗 2024-09-01 10:36:14

ListBox.Items 是一个 ObjectCollection 。这意味着您可以选择要放入其中的对象类型。

当你这样做时:

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

你正在将字符串对象放入其中,因此你需要像这样将它们取出:

foreach (string item in listBox1.Items)

相反,你可能希望你的代码更像这样:

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*

然后你就可以这样使用它你最初是这么写的:

foreach (ListViewItem item in listBox1.Items)

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)
寄人书 2024-09-01 10:36:14

Tag 有一个名为 photoid 的成员吗?也许您需要在那里将您的“标签”转换为它应该是什么?

        //Tag tag = (Tag) item.Tag;
        MyObject tag = (MyObject)item.Tag;
        saveSlide(showid, tag.photoid, enumber); 
        number++; 

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++; 
走过海棠暮 2024-09-01 10:36:14

除非你把事情命名得很奇怪,否则我会说错误是你试图从 ListBox 中获取 ListViewItem 。

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

┼── 2024-09-01 10:36:14

只需更改第二个代码片段的最后一行代码,一切就可以了,如下所示。

listBox1.Items.Add(item);

关于错误

您将字符串作为项目添加到 listBox 中,并在 foreach 中尝试将项目(这是一个字符串)隐式转换(种姓)为 ListViewItem ,这不起作用,编译器给出错误。

希望它能起作用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文