item and subitem collections

A

Ash Jones

I trying to do nested items in a collection.

I've got an item which one of the properties is subitems, which is a
collection of item. can do the root level but if i do a subitem at design
time i get an object not set error. it's like a menuitem or treenode which
can have subitems of the same type. cant find any help on nested items.

is there anyone out there that help?

Many thanks,

ash.
 
A

Ash Jones

ITEM CLASS:
using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace AssistApplications.Windows.Forms
{
[DesignTimeVisible(false)]
public class CategoryItem : System.ComponentModel.Component
{
private string fTitle;
private CategoryItemCollection fSubItems;
private string fValue;

public CategoryItem()
{
fSubItems = new CategoryItemCollection();
}

public string Title
{
get { return fTitle; }
set { fTitle = value; }
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public CategoryItemCollection SubItems
{
get { return fSubItems; }
}
public string Value
{
get { return fValue; }
set { fValue = value; }
}

}
}
ITEM COLLECTION:
using System;
using System.Collections;
using System.Windows.Forms;

namespace AssistApplications.Windows.Forms
{
public class CategoryItemCollection : System.Collections.CollectionBase
{
public delegate void CollectionChange(int index, object value);
private event CollectionChange fInserting;
private event CollectionChange fInsertComplete;

protected override void OnInsert(int index, object value)
{
CategoryItem item = value as CategoryItem;
if(item.Title == null)
item.Title = String.Concat("Item", Count.ToString());
base.OnInsert(index, item);
if(fInserting != null)
{
fInserting(index, value);
}
}
protected override void OnInsertComplete(int index, object value)
{
base.OnInsertComplete (index, value);
if(fInsertComplete != null)
{
fInsertComplete(index, value);
}
}


public CategoryItemCollection(){}
public int Add(CategoryItem item)
{
if(Contains(item)) return -1;
int index = List.Add(item);
//OnAddPanel(new PanelEventArgs(panel));
return index;
}
public void AddRange(CategoryItem[] items)
{
foreach(CategoryItem item in items)
Add(item);
}
public void Remove(int index)
{
if((index >= Count) || (index < 0))
return;
List.RemoveAt(index);
}
public CategoryItem this[int index]
{
get
{
if(index < 0 || index >= Count)
return null;
return (CategoryItem)List[index];
}
}
public void Insert(int index, CategoryItem item)
{
List.Insert(index, item);
}
public int IndexOf(CategoryItem item)
{
return List.IndexOf(item);
}
public bool Contains(CategoryItem item)
{
return InnerList.Contains(item);
}


public event CollectionChange Inserting
{
add { fInserting += value; }
remove { fInserting -= value; }
}
public event CollectionChange InsertComplete
{
add { fInsertComplete += value; }
remove { fInsertComplete -= value; }
}

}
}
Control class:
refences the items as in item but with the name as Items e.g.

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public CategoryItemCollection Items
{
get { return fItems; }
}

What I'm trying to do is like treenode and menitems they can subitems of the
same type but trying to do this at design time.
if i load it at runtime it works fine. I have no idea on how to do this and
there is no help out there.

Thanks,

ash.
 
C

codewriter

I tried this and it worked fine:
private void button1_Click(object sender, System.EventArgs e)

{

CategoryItemCollection oCol = new CategoryItemCollection();

for(int i = 0; i<10; i++)

{

CategoryItem oItem = new CategoryItem();

oItem.Title = "Item" + i.ToString();

for(int j=0; j<10; j++)

{

CategoryItem oSubItem = new CategoryItem();

oSubItem.Title = "Subitem" + j.ToString();

oItem.SubItems.Add(oSubItem);

}

oCol.Add(oItem);

}

this.lstResources.Clear();

ColumnHeader oHeader = new ColumnHeader();

oHeader.Text = "Name";

oHeader.Width = lstResources.Width;

this.lstResources.Columns.Add(oHeader);

foreach(CategoryItem oColItem in oCol)

{

ListViewItem oItem;

oItem = new ListViewItem(new string[] {oColItem.Title}

, -1, Color.Empty, Color.White, null);

this.lstResources.Items.Add(oItem);

foreach(CategoryItem oColItem1 in oColItem.SubItems)

{

ListViewItem oItem1;

oItem1 = new ListViewItem(new string[] {oColItem1.Title}

, -1, Color.Empty, Color.White, null);

this.lstResources.Items.Add(oItem1);

}

}

}

Ash Jones said:
ITEM CLASS:
using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace AssistApplications.Windows.Forms
{
[DesignTimeVisible(false)]
public class CategoryItem : System.ComponentModel.Component
{
private string fTitle;
private CategoryItemCollection fSubItems;
private string fValue;

public CategoryItem()
{
fSubItems = new CategoryItemCollection();
}

public string Title
{
get { return fTitle; }
set { fTitle = value; }
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public CategoryItemCollection SubItems
{
get { return fSubItems; }
}
public string Value
{
get { return fValue; }
set { fValue = value; }
}

}
}
ITEM COLLECTION:
using System;
using System.Collections;
using System.Windows.Forms;

namespace AssistApplications.Windows.Forms
{
public class CategoryItemCollection : System.Collections.CollectionBase
{
public delegate void CollectionChange(int index, object value);
private event CollectionChange fInserting;
private event CollectionChange fInsertComplete;

protected override void OnInsert(int index, object value)
{
CategoryItem item = value as CategoryItem;
if(item.Title == null)
item.Title = String.Concat("Item", Count.ToString());
base.OnInsert(index, item);
if(fInserting != null)
{
fInserting(index, value);
}
}
protected override void OnInsertComplete(int index, object value)
{
base.OnInsertComplete (index, value);
if(fInsertComplete != null)
{
fInsertComplete(index, value);
}
}


public CategoryItemCollection(){}
public int Add(CategoryItem item)
{
if(Contains(item)) return -1;
int index = List.Add(item);
//OnAddPanel(new PanelEventArgs(panel));
return index;
}
public void AddRange(CategoryItem[] items)
{
foreach(CategoryItem item in items)
Add(item);
}
public void Remove(int index)
{
if((index >= Count) || (index < 0))
return;
List.RemoveAt(index);
}
public CategoryItem this[int index]
{
get
{
if(index < 0 || index >= Count)
return null;
return (CategoryItem)List[index];
}
}
public void Insert(int index, CategoryItem item)
{
List.Insert(index, item);
}
public int IndexOf(CategoryItem item)
{
return List.IndexOf(item);
}
public bool Contains(CategoryItem item)
{
return InnerList.Contains(item);
}


public event CollectionChange Inserting
{
add { fInserting += value; }
remove { fInserting -= value; }
}
public event CollectionChange InsertComplete
{
add { fInsertComplete += value; }
remove { fInsertComplete -= value; }
}

}
}
Control class:
refences the items as in item but with the name as Items e.g.

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public CategoryItemCollection Items
{
get { return fItems; }
}

What I'm trying to do is like treenode and menitems they can subitems of the
same type but trying to do this at design time.
if i load it at runtime it works fine. I have no idea on how to do this and
there is no help out there.

Thanks,

ash.



codewriter said:
Post your code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top