WinForm: represents a single ListBox in a foreach statement

How can I represent one item in a list in a foreach statement?

I tried ListBoxItem, but System.Windows.Controls is not considered a valid namespace in my .Net framework (version 4).

foreach(ListBoxItem item in listBoxObject.Items)
{
    ...
}
+3
source share
3 answers

Usually, when someone goes through list items, they try to determine if they are selected or not. If so, try using listBoxObject.SelectedItems instead of listBoxObject.Items. This will only return the items that have been selected.

, ListBoxItem. ( , seletecteditems ). , ( , , , , ).

:

foreach (Object listBoxItem in listBoxObject.SelectedItems)
{
  //Use as object or cast to a more specific type of object.
}

, , foreach. (: , ). - .

foreach (String listBoxItem in listBoxObject.SelectedItems)
{
  //Use as String. It has already been cast. 
}
+1

foreach(Object item in listBoxObject.Items){ ... }

http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.objectcollection.item.aspx

ListBox.Items System.Windows.Forms.ListBox.ObjectCollection System.Windows.Forms.ListBox.ObjectCollection.Item Object. .

+1

You will find that it listBoxObject.Itemsis a collection of objects containing your data objects, not controls.

For example, if I snap a list box like this:

listBox1.DataSource = new string[] { "asdf", "qwerty" };

Then the property .Itemsgives ObjectCollection, containing two lines.

+1
source

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


All Articles