Problem with C # listbox tags

Hi, I am trying to use a list tag element.

Here is my code.

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

The problem im havin is when I run the program, I get an error message sayin cannot convert the type string to system.ListView, but I did not declare the item as anywher string in my program

Here I add items to the list. Please help. Im on the dead line and do 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); } 
+4
source share
6 answers

Poppy, you add title to listBox1.Items .

title is of type string .

Therefore, when you access it, use string , for example, foreach (string item in listBox1.Items) .

Give it a try. Does it help?

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

This works, you need to show the code in which you add the 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); } 

Change the following code:

You add rows to your ListBox instead of ListViewItem:

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

+2
source

ListBox.Items - ObjectCollection . This means that you can select the type of object to enter.

When you do this:

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

you put string objects in it, so you will need to output them like this:

 foreach (string item in listBox1.Items) 

Instead, you probably want your code to be something 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 can use it the way you wrote it at the beginning:

 foreach (ListViewItem item in listBox1.Items) 
+1
source

Does the tag have a member named photoid? Maybe you need a cast to convert your β€œtag” into everything it was supposed to be?

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

If you didn’t call things strange, I would say that the error is that you are trying to get a ListViewItem from a ListBox.

0
source

Just change the last line of code of the second code fragment, and everything will be fine, which looks as follows.

listBox1.Items.Add (item);

About error

You added strings to the Box list as elements, and in the foreach element (which is a string) tries to convert (caste) to ListViewItem implicitly so that hich does not work and the compiler throws an error.

Hope this works.

0
source

Source: https://habr.com/ru/post/1304462/


All Articles