when you create a listview, it has an items property. this returns a collection.
so you can do
for each item in myListView.Items
...
next
now, if i make a class myItemGroup which contains the class myItem and a property items() that returns an array of myItems, how can i use this same functionality that i can do
for each myItem in myItemGroup
...
next
??? thanks for the help.. (i can explain more if this is unclear)
If I can remember, tomorrow I will post a custom collection class that you can modify to create a strongly typed collection of custom objects. I also have it implementing the IBinding interface, which is great for binding the collection to datagrids...
I will try to remember, but if you send me an email, that will help refresh my memory.
OK, first off, I know it is C# code..., but it is all I use anymore, and if I ever get around to it, I will convert it. Someone else can take the time to convert it if they wish.
Now, the class is pretty simple. First, change the namespace to match yours...duh.
Next, change the name of the class to better represent what it is holding. Right now it is named MyObjectCollection. This indicates it holds a collection of MyObjects. So lets say you want it to hold a collection of UserObject's. You would rename it to UserObjectCollection. (of course, your own naming conventions can also apply, mine are just suggestions) When you change the name of the class, you also have to change the name of the constructor to match.
Last but not the least, you need to strongly type your collection. To do that, do a search and replace for "MyObject" and replace with "UserObject" (if that is the object type you are holding in this class).
Now you can create a instance of this class, and add your objects to it. You can foreach through your objects, and you can bind to bindable datasources such as datagrids, combo boxes, listboxes, etc. Use property names as the valuemember/displaymembers and you are all set.
I can code up an example on how this is used, but it won't be till after this weekend sometime. I have a completely full schedule through Tuesday of next week.
Code:
using System;
using System.Collections;
using System.ComponentModel;
namespace MYNAMESPACE // Change
{
public class MyObjectCollection :
System.Collections.CollectionBase, IBindingList
{
#region Main Methods Used
public MyObjectCollection()
{
}
public MyObject this[int index]
{
get
{
return ((MyObject)List[index]);
}
set
{
List[index] = value;
}
}
public int Add(MyObject value)
{
int index = List.Add(value);
OnListChanged(new
ListChangedEventArgs(ListChangedType.ItemAdded, index));
return index;
}
public int IndexOf(MyObject value)
{
return(List.IndexOf(value));
}
public void Insert(int index, MyObject value)
{
List.Insert(index, value);
OnListChanged(new
ListChangedEventArgs(ListChangedType.ItemAdded, index));
}
public void Remove(MyObject value)
{
int index = List.IndexOf(value);
List.Remove(value);
if (index >= 0) OnListChanged(new
ListChangedEventArgs(ListChangedType.ItemDeleted, index));
}
public new void RemoveAt(int index)
{
base.RemoveAt(index);
if (index >= 0) OnListChanged(new
ListChangedEventArgs(ListChangedType.ItemDeleted, index));
}
public bool Contains(MyObject value)
{
// If value is not of type MyObject, this will return
false.
return(List.Contains(value));
}
public object AddNew()
{
MyObject itm = new MyObject();
List.Add(itm);
return itm;
}
#endregion
#region Events
/// <summary>Fires the ListChanged event if any delegates
are registered with it.</summary>
/// <param name="e">Arguments with which to fire the
event.</param>
private void OnListChanged(ListChangedEventArgs e)
{
if (_listChanged != null) _listChanged(this, e);
}
/// <summary>Occurs when the list changes or an item in the
list changes.</summary>
private ListChangedEventHandler _listChanged;
/// <summary>Occurs when the list changes or an item in the
list changes.</summary>
public event ListChangedEventHandler ListChanged
{
add { _listChanged += value; }
remove { _listChanged -= value; }
}
#endregion
#region Forcing DataBind Refresh
/// <summary>Force the list to notify any binders that the
list has been changed.</summary>
public void ResetNotification()
{
OnListChanged(new
ListChangedEventArgs(ListChangedType.Reset, -1));
}
#endregion
#region IBindingList Default Properties
/// <summary>Gets whether you can add items to the list
using AddNew.</summary>
public bool AllowNew { get { return true; } }
/// <summary>Gets whether you can update items in the
list.</summary>
public bool AllowEdit { get { return true; } }
/// <summary>Gets whether you can remove items from the
list, using Remove or RemoveAt.</summary>
public bool AllowRemove { get { return true; } }
/// <summary>Gets whether the list supports
sorting.</summary>
public bool SupportsSorting { get { return false; } }
/// <summary>Gets whether the list supports searching using
the Find method.</summary>
public bool SupportsSearching { get { return false; } }
/// <summary>Gets whether a ListChanged event is raised when
the list changes or an item in the list changes.</summary>
public bool SupportsChangeNotification { get { return true;
} }
#endregion
#region NotSupportedExceptions
/// <summary>Not supported.</summary>
public void
AddIndex(System.ComponentModel.PropertyDescriptor property) { throw new
NotSupportedException(); }
/// <summary>Not supported.</summary>
public void
ApplySort(System.ComponentModel.PropertyDescriptor property,
System.ComponentModel.ListSortDirection direction) { throw new
NotSupportedException(); }
/// <summary>Not supported.</summary>
public int Find(System.ComponentModel.PropertyDescriptor
property, object key) { throw new NotSupportedException(); }
/// <summary>Not supported.</summary>
public void RemoveSort() { throw new
NotSupportedException(); }
/// <summary>Not supported.</summary>
public void
RemoveIndex(System.ComponentModel.PropertyDescriptor property) { throw
new NotSupportedException(); }
/// <summary>Not supported.</summary>
public ListSortDirection SortDirection { get { throw new
NotSupportedException(); } }
/// <summary>Not supported.</summary>
public PropertyDescriptor SortProperty { get { throw new
NotSupportedException(); } }
/// <summary>Not supported.</summary>
public bool IsSorted { get { throw new
NotSupportedException(); } }
#endregion
}
}
Originally posted by hellswraith Last but not the least, you need to strongly type your collection. To do that, do a search and replace for "MyObject" and replace with "UserObject" (if that is the object type you are holding in this class).
what does making my collection strongly typed mean?
Originally posted by marvinklein what does making my collection strongly typed mean?
Like said above, it makes it to where the collection can only contain one specific type of object. The compiler won't let a object of another type be held in the collection. The opposite end would be a object collection, which is what you get with an ArrayList.
Originally posted by marvinklein anybody have an example of this in vb.net rather than c#?
If I have time next week, I can convert it. I just have very little time, and I don't want to have an error in my conversion and you all cursing me out...lol.
Originally posted by hellswraith That collection doesn't do much but let you add. Not that useful.
But it's strongly typed (if I understand the term correctly).
And that is what he was looking for.
And the original question was how to do this with his own properties.
Now, he can just make a property of the collectiontype and it will work.
Normally you would have some other features in there, but since the dude have never made this before, I thought it best to take one step at the time.
The IndexOf and other functions like that are just overriding existing functions, and should be easy to do when you first get the idea. After all, it's just a matter of casting.
I have no doubt that your class is just fine, but it may be a little overkill for a guy just trying to understand what it's all about.
I'm sorry to disappoint you, but what were you expecting? (no offense intended )
Edit: I accidently wrote overloads. That should of course have been override.
Last edited by pax; Jul 24th, 2004 at 02:37 AM.
I wish I could think of something witty to put in my sig...