C# 列表框标签问题
您好,我正在尝试使用列表框的标签项。
这是我的代码。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Poppy,您正在将
title
添加到listBox1.Items
。title
的类型为string
。因此,当您访问它时,请使用
string
类型,例如foreach (listBox1.Items 中的 string item)
。尝试。有帮助吗?
Poppy you are adding
title
tolistBox1.Items
.title
is of typestring
.So when you access it use
string
type like thisforeach (string item in listBox1.Items)
.Try. Does it help?
这是可行的,您需要显示向列表添加项目的代码:
编辑以下更多代码:
您将字符串添加到 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:
Edit following more code:
You are adding strings to your ListBox instead of the ListViewItem:
listBox1.Items.Add(title);
should belistBox1.Items.Add(item);
ListBox.Items 是一个 ObjectCollection 。这意味着您可以选择要放入其中的对象类型。
当你这样做时:
你正在将字符串对象放入其中,因此你需要像这样将它们取出:
相反,你可能希望你的代码更像这样:
然后你就可以这样使用它你最初是这么写的:
ListBox.Items is an ObjectCollection. That means you can choose the kind of object to put in it.
When you're doing this:
you are putting string objects into it, so you would need to get them out like this:
Instead, you probably want your code to be more like this:
then you'll be able to use this the way you initially wrote it:
Tag 有一个名为 photoid 的成员吗?也许您需要在那里将您的“标签”转换为它应该是什么?
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?
除非你把事情命名得很奇怪,否则我会说错误是你试图从 ListBox 中获取 ListViewItem 。
Unless you named things weird I'd say the error is that you're trying to get a ListViewItem from a ListBox.
只需更改第二个代码片段的最后一行代码,一切就可以了,如下所示。
关于错误
您将字符串作为项目添加到 listBox 中,并在 foreach 中尝试将项目(这是一个字符串)隐式转换(种姓)为 ListViewItem ,这不起作用,编译器给出错误。
希望它能起作用。
Just change the last line of code of the second code-snippet and everything will be ok, which is as follows.
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.