When I write this code:
ListView lv = new ListView();
foreach (ListViewDataItem item in lv.Items)
{
}
I get "the type or name ListViewDataItem could not be found"
Items is also not found under the lv object.
Basically I need to iterate through each row of the ListView and set a checkbox added using item template.
How can I accomplish that?
The correct way to loop through a listview is to access it's ItemsSource. Then you can cast the item into your view model and do stuff with it.
foreach (var item in lv.ItemsSource)
{
// cast the item
var dataItem = (ListViewDataItem) item;
// then do stuff with your casted item
...
}
I used a for loop to iterate over my listView ChildCount, assigned a var as the Tag of the GetChildAt as ImageAdapterViewHolder and then set my checkbox to false.
class ImageAdapterViewHolder : Java.Lang.Object
{
public ImageView SavedImage { get; set; }
public TextView Description { get; set; }
public CheckBox SelectImage { get; set; }
}
for (int i = 0; i < listView.ChildCount; i++)
{
var row = listView.GetChildAt(i).Tag as ImageAdapterViewHolder;
row.SelectImage.Checked = false;
}
Related
Edit/Update!!!
I wanted to update the question as the answer below only guided me in the right direction but did not completely solve the issue.
However i did manage to fix the problem by changing how items and hopefully this will help someone in the future
Add items to the class list like so
descriptionclb.Items.Add(new listItem { Name = ItemToAdd, Price = Convert.ToDouble(ItemPrice), Quantity = Convert.ToDouble(Quantity.Text) });
And iterate items like so
foreach (Listitem item in descriptionclb.Items)
{
double TotalAmmount = item.Price);
//Do stuff with Item
}
I'm trying to get the value of a list item within a listbox, I keep getting an error "'System.InvalidCastException' Unable to cast object of type 'System.String' to type 'list'."
Any help would be appreciated,and I have tried to do lots of research with no result (Maybe im not phrasing the question right on google). See my code below.
Class ListItem
public class listItem
{
public string Name { get; set; }
public double Price { get; set; }
public double Quantity { get; set; }
public override string ToString()
{
return Name;
}
}
I insert the values here
Globals.li.Name = ItemToAdd;
Globals.li.Price = Convert.ToDouble(ItemPrice);
Globals.li.Quantity = Convert.ToDouble(Quantity.Text);
descriptionclb.Items.Add(Globals.li.ToString());
Globals is a globlal class and li is the listitem li = new listItem
I get the error here
foreach (var item in descriptionclb.Items)
{
double TotalAmmount = Convert.ToDouble(((list)item).Price);
}
You are adding strings here:
descriptionclb.Items.Add(Globals.li.ToString());
So you are saving string rather than listItem object that is why you get the error.
Should be:
descriptionclb.Items.Add(Globals.li);
Also this object descriptionclb.Items should be of type listItem
And the for loop should something like:
foreach (listItem item in descriptionclb.Items)
{
double TotalAmmount = item.Price;
}
In your code you have a type called 'list'. Your foreach loop iterates over a list of strings, therefore 'var item' is a string. You can't convert the 'list' type to a string which is causing the exception.
If you're trying to add all the 'listItem' Price values up then you should try changing the type of 'descriptionclb.Items' from a list of strings to a list of 'listItem' and add the listItem directly. Then you will be able to get rid of the cast.
descriptionclb.Items.Add(Globals.li);
foreach (var item in descriptionclb.Items)
{
double TotalAmmount = item.Price;
}
Just a note; in the for loop you don't do anything with TotalAmmount.
you need to change
descriptionclb.Items.Add(Globals.li);
and
foreach (var item in descriptionclb.Items)
{
double TotalAmmount = item.Price;
}
if TotalAmmount , total fares from list cahnge it to:
double TotalAmmount=0
foreach (var item in descriptionclb.Items)
{
TotalAmmount = TotalAmmount+ item.Price;
}
I have a ComboBox being populated where each object in ComboBox.Items is a List of objects. Currently the ComboBox displays "(Collection)" for each Item.
Is it possible to have the ComboBox display a member of the first object in the List that comprises an Item of the ComboBox?
I am currently populating the ComboBox items by the following:
foreach(List<SorterIdentifier> sorterGroup in m_AvailableSorterGroups)
{
// There are conditions that may result in the sorterGroup not being added
comboBoxSorterSelect.Items.Add(sorterGroup);
}
//comboBoxSorterSelect.DataSource = m_AvailableSorterGroups; // Not desired due to the comment above.
//comboBoxSorterSelect.DisplayMember = "Count"; //Displays the Count of each list.
The value that I would like to have displayed in the ComboBox can be referenced with:
((List<SorterIdentifier>)comboBoxSorterSelect.Items[0])[0].ToString();
((List<SorterIdentifier>)comboBoxSorterSelect.Items[0])[0].DisplayName; // public member
You can create an object wrapper and override the ToString() method:
public class ComboBoxSorterIdentifierItem
{
public List<SorterIdentifier> Items { get; }
public override string ToString()
{
if ( Items == null || Items.Count == 0) return "";
return Items[0].ToString();
}
public BookItem(List<SorterIdentifier> items)
{
Items = items;
}
}
You should override the SorterIdentifier.ToString() too to return what you want like DisplayName.
Now you can add items in the combobox like this:
foreach(var sorterGroup in m_AvailableSorterGroups)
{
item = new ComboBoxSorterIdentifierItem(sorterGroup);
comboBoxSorterSelect.Items.Add(item);
}
And to use the selected item for example, you can write:
... ((ComboBoxSorterIdentifierItem)comboBoxSorterSelect.SelectedItem).Item ...
I could imagine a few ways to do this... you could create a class that extends List<T>, so you have an opportunity to define the value you'd like to display.
public class SortedIdentifier
{
public string Name { get; set; }
}
public class SortedIdentifiers : List<SortedIdentifier>
{
public string SortedIdentifierDisplayValue
{
get { return this.FirstOrDefault()?.Name ?? "No Items"; }
}
}
Then use the new class like this:
comboBox1.DisplayMember = "SortedIdentifierDisplayValue";
var list = new SortedIdentifiers { new SortedIdentifier { Name = "John" } };
comboBox1.Items.Add(list);
I have this class
class ComboboxValue
{
public int Id { get; private set; }
public string Name { get; private set; }
public ComboboxValue(int id, string name)
{
Id = id;
Name = name;
}
public override string ToString()
{
return Name;
}
}
and I set my entries like this:
var list = Funktionen.LoadCustomers();
foreach (var item in list)
{
MyCombo.Properties.Items.Add(new ComboboxValue(item.ID, item.Name));
}
In another function, I will set a item in my combobox by customerID.
How can I do this?
Btw. I´m using Devexpress.
Thank you.
To programmatically select a value for the combo, set the ComboBoxEdit.EditValue property. Here is some sample code:
ComboBoxEdit.EditValue = 2; // select an item which ID = 2
Besides the Selected index you can use the SelectedItem property to select any item within the editor's item list. You need to assign an underlying data object to the SelectedItem property.
Alternatively, you can set its EditValue to '25' that is the ValueMember property value of the desirable item as shown in above example.
Reference these:
Select Item in ComboBoxEdit
how set combobox selected value
To select item in ComboboxValue class :
comboBox1.SelectedItem = comboBox1.Items.Cast<ComboboxValue>()
.Where(i => i.Name == dataGridView1.CurrentRow.Cells[5].Value.ToString()).Single();
var item = MyCombo.Properties.Items.FirstOrDefault(i => i.ID == yoursearchIDhere);
item will be combobox item which you want to get. If you look for it or not, let me know and explain clearly please
LoadCustomers() should return List also.
Try
MyCombo.SelectedItem = MyCombo.Items.SingleOrDefault(x => (x as ComboboxValue).Id == externalID)
I'm having problems removing the data in a list which was entered by a user in my textboxes and then stored in a listbox. I know how to remove the selected item in the listbox but when I click my button to show everything in the list, the selected item I just deleted is still in the list.
Here is my code for removing the selected item in the listbox:
for (int i = 0; i < VehicleListBox.SelectedItems.Count; i++)
VehicleListBox.Items.Remove(VehicleListBox.SelectedItems[i]);
My list is in a class called company and the list is named vehicles. I have looked everywhere for the most part for my answer and cannot seem to find it. I should also mention it's a generic list.
With your class design as I understand it, I wrote the following code in a WPF project. I added a listbox called "listbox1" and a button to the form. I believe the following code does what you want, or at least would guide you to the answer.
public class Company
{
public List<Vehicle> Vehicles;
public Company()
{
Vehicles = new List<Vehicle>() { new Vehicle(1), new Vehicle(2), new Vehicle(3) };
}
}
public class Vehicle
{
private string _vehicleNum;
public Vehicle(int num)
{
_vehicleNum = "Vehicle" + num.ToString();
}
public string getDetails()
{
return _vehicleNum;
}
}
Company ACompany = new Company();
public MainWindow()
{
InitializeComponent();
foreach(Vehicle v in ACompany.Vehicles)
listbox1.Items.Add(v.getDetails());
}
private void Button_Click(object sender, RoutedEventArgs e)
{
for (int i = 0; i < listbox1.SelectedItems.Count; i++)
{
foreach(Vehicle v in ACompany.Vehicles)
{
if (String.Equals(v.getDetails(), listbox1.SelectedItems[i].ToString()))
{
ACompany.Vehicles.Remove(v);
break;
}
}
listbox1.Items.Remove(listbox1.SelectedItems[i]);
}
}
So as I understand you're using getDetails to get the list, then one-by-one adding them to the VehicleListBox.
An option would be to remove the selected items from the List and then update the ListBox accordingly.
You add a simple method to do this:
private void UpdateListBox(List<string> vehicles);
Replacing `List' with the type you're using.
Alternatively have you tried binding the ItemsSource of the ListBox?
In WPF (for example):
<ListBox Name=ExampleListBox, ItemsSource={Binding} />
The in code:
ExampleListBox.DataContext = myList;
This best goes in the Window_Loaded method after you've populated the List.
If necessary then update this when the list changes.
Ok this works I tested it.
foreach (string thing in listBox1.SelectedItems){
myList.remove(thing);
}
Given your current code, you could parse the string back out by reversing whatever getDetails() is doing, and then select the appropriate object from your list.
for (int i = 0; i < VehicleListBox.SelectedItems.Count; i++)
{
var item = (string)VehicleListBox.SelectedItems[i];
VehicleListBox.Items.Remove(item);
// assuming your company class has only a Name and ID...
string name = ... // parse name from item
int id = ... // parse id from item
vehicles.Remove(vehicles.Single(x => x.Name == name && x.ID == id));
}
Using asp.net C#.
I have BulletList, and I would like to add ListItem that will render with childs.
That is, inside every <li> I can add more controls to be rendered.
How can I achieve that?
thanks
I'm not sure why BulletedList has a collection of ListItems. ListItems are generally used for form elements (thus the Selected, Value, Text properties) and doesn't make sense to be used for bulleted lists. There is also no property which contains child ListItems for you to accomplish your goal. I would suggest using your own classes to do this. Here is a quick mockup:
public static class BulletList
{
public static string RenderList(List<BulletListItem> list) {
var sb = new StringBuilder();
if (list != null && list.Count > 0)
{
sb.Append("<ul>");
foreach(var item in list) {
sb.Append(item.Content);
sb.Append(BulletList.RenderList(item.Children));
}
sb.Append("</ul>");
}
return sb.ToString();
}
}
public class BulletListItem
{
public string Content { get; set; }
public List<BulletListItem> Children { get; set; }
}
Then you can create your list with children and output it...
var items = new List<BulletListItem>();
items.Add(new BulletListItem() { Content = "Root 1" });
items.Add(new BulletListItem() { Content = "Root 2", Children = new List<BulletListItem>() { new BulletListItem() { Content = "Child 2.1" }} });
items.Add(new BulletListItem() { Content = "Root 3" });
Response.Write(BulletList.RenderList(items));