Need help with a Custom Control.. passing Objects to a Collection

C

Cider123

Trying to figure out how to replicate the same interface as what a
ListView control has.

I have everything working for the most part, with exception of 1 minor
setback.

I can't seem to pass the "ListViewItem" object to a Collection on
"MyControl".


- - - - - -
Breakdown:
- - - - - -


============
ControlItem
============

I have the ListViewItem (object) called ControlItem for use in
"MyControl".

To keep things simple, I made the ControlItem a separate class DLL.

I can declare the ControlItem object.
I can set the "Text" property.
I can set the object.SubItem.Add("parameter").



==========
MyControl
==========


private ControlItemCollection _Items = null;


public class ControlItemCollection
{
private ArrayList _CollectionItems = null;
string strTemp = "";

public ControlItemCollection this [int index]
{
get
{
return (ControlItemCollection)_CollectionItems[index];
}
}

public ControlItemCollection()
{
_CollectionItems = new ArrayList();
}

public void Add(ControlItem controlItem)
{
_CollectionItems.Add(controlItem);
}
}


//
// Notes:
// In an ideal declaration, this is how I would declare it:
//

public ControlItemCollection Items
{
set
{
_Items.Add((ControlItem)value);
}

get
{
return _Items;
}
}

//
// However, it comes back during compile time stipulating:
// Cannot implicitly convert type
'MyControl.myLV.ControlItemCollection' to 'ControlItems.ControlItem'
// It makes sense though. The 'Items' is declared as
'ControlItemCollection'.
//


- - - - - - - - - -
Analysis of Design
- - - - - - - - - -

I won't bore you with the in depth details, unless you need more info.
Trying to keep this short and simple.

Once you create a ListViewItem object and try to add it to the
control:

lvSample.Items.Add(oItem);

If you stop at the ".Add" portion you will see the path:

System.Windows.Forms.ListViewItem
ListViewItemCollection.Add(System.Windows.Forms.ListViewItem value)(+2
overloads)

When I first started tinkering around with this, I used the following
code:


==========
MyControl
==========

public ControlItemCollection Items
{
set
{
_Items.Add(value.ToString());
}

get
{
return _Items;
}
}

public class ControlItemCollection
{
...

public void Add(string text)
{
}
}


And noticed when I executed the code:

lvMyControl.Items.Add(oItem);

If you stop at the ".Add" portion you would have seen the path:

void ControlItemCollection.Add(string text)



So tinkering around I made the following changes:

==========
MyControl
==========

public class ControlItemCollection
{
...

public ControlItem Add(string text)
{
return (new ControlItem());
}
}

And noticed that when you stop at the ".Add" portion you now see:

ControlItems.ControlItem ControlItemCOllection.Add(string text)

It's close, but I still run into the snag of the "ControlItem" object
being passed to the Collection reference that is defined by "Items".




I think the required changes would be something like:

==========
MyControl
==========

public class ControlItemCollection
{
...

public ControlItem Add(ControlItem controlItem)
{
return controlItem;
}
}

But I'm not really sure how to piece it all together, so the
'ControlItem' object is passed and added to the Master collection that
resides within the Control itself.

If anyone can help me out with this complexity.. my hats off to ya.
 
B

Bob Powell [MVP]

Your indexer is returning a ControlItemsCollection type and should also be
read-write.

--
Bob Powell [MVP]
C#, System.Drawing

The November edition of Well Formed is now available.
Learn how to create Shell Extensions in managed code.
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

Read my Blog at http://bobpowelldotnet.blogspot.com

Cider123 said:
Trying to figure out how to replicate the same interface as what a
ListView control has.

I have everything working for the most part, with exception of 1 minor
setback.

I can't seem to pass the "ListViewItem" object to a Collection on
"MyControl".


- - - - - -
Breakdown:
- - - - - -


============
ControlItem
============

I have the ListViewItem (object) called ControlItem for use in
"MyControl".

To keep things simple, I made the ControlItem a separate class DLL.

I can declare the ControlItem object.
I can set the "Text" property.
I can set the object.SubItem.Add("parameter").



==========
MyControl
==========


private ControlItemCollection _Items = null;


public class ControlItemCollection
{
private ArrayList _CollectionItems = null;
string strTemp = "";

public ControlItemCollection this [int index]
{
get
{
return (ControlItemCollection)_CollectionItems[index];
}
}

public ControlItemCollection()
{
_CollectionItems = new ArrayList();
}

public void Add(ControlItem controlItem)
{
_CollectionItems.Add(controlItem);
}
}


//
// Notes:
// In an ideal declaration, this is how I would declare it:
//

public ControlItemCollection Items
{
set
{
_Items.Add((ControlItem)value);
}

get
{
return _Items;
}
}

//
// However, it comes back during compile time stipulating:
// Cannot implicitly convert type
'MyControl.myLV.ControlItemCollection' to 'ControlItems.ControlItem'
// It makes sense though. The 'Items' is declared as
'ControlItemCollection'.
//


- - - - - - - - - -
Analysis of Design
- - - - - - - - - -

I won't bore you with the in depth details, unless you need more info.
Trying to keep this short and simple.

Once you create a ListViewItem object and try to add it to the
control:

lvSample.Items.Add(oItem);

If you stop at the ".Add" portion you will see the path:

System.Windows.Forms.ListViewItem
ListViewItemCollection.Add(System.Windows.Forms.ListViewItem value)(+2
overloads)

When I first started tinkering around with this, I used the following
code:


==========
MyControl
==========

public ControlItemCollection Items
{
set
{
_Items.Add(value.ToString());
}

get
{
return _Items;
}
}

public class ControlItemCollection
{
...

public void Add(string text)
{
}
}


And noticed when I executed the code:

lvMyControl.Items.Add(oItem);

If you stop at the ".Add" portion you would have seen the path:

void ControlItemCollection.Add(string text)



So tinkering around I made the following changes:

==========
MyControl
==========

public class ControlItemCollection
{
...

public ControlItem Add(string text)
{
return (new ControlItem());
}
}

And noticed that when you stop at the ".Add" portion you now see:

ControlItems.ControlItem ControlItemCOllection.Add(string text)

It's close, but I still run into the snag of the "ControlItem" object
being passed to the Collection reference that is defined by "Items".




I think the required changes would be something like:

==========
MyControl
==========

public class ControlItemCollection
{
...

public ControlItem Add(ControlItem controlItem)
{
return controlItem;
}
}

But I'm not really sure how to piece it all together, so the
'ControlItem' object is passed and added to the Master collection that
resides within the Control itself.

If anyone can help me out with this complexity.. my hats off to ya.
 
B

Bob Powell [MVP]

Just to clarify. The indexer should be:

public ControlItem this [int index]
{
get
{
return (ControlItemCollection)_CollectionItems[index];
}
set{_CollectionItems[index]=value;}
}

Oh and I guess it would help loads if your class was actually a collection
based on, say, CollectionBase. It implements IEnumerable and all sorts of
good stuff.

The collection base class has an internal List member too so you don't need
to define the _CollectionItems.


--
Bob Powell [MVP]
C#, System.Drawing

The November edition of Well Formed is now available.
Learn how to create Shell Extensions in managed code.
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

Read my Blog at http://bobpowelldotnet.blogspot.com

Cider123 said:
Trying to figure out how to replicate the same interface as what a
ListView control has.

I have everything working for the most part, with exception of 1 minor
setback.

I can't seem to pass the "ListViewItem" object to a Collection on
"MyControl".


- - - - - -
Breakdown:
- - - - - -


============
ControlItem
============

I have the ListViewItem (object) called ControlItem for use in
"MyControl".

To keep things simple, I made the ControlItem a separate class DLL.

I can declare the ControlItem object.
I can set the "Text" property.
I can set the object.SubItem.Add("parameter").



==========
MyControl
==========


private ControlItemCollection _Items = null;


public class ControlItemCollection
{
private ArrayList _CollectionItems = null;
string strTemp = "";

public ControlItemCollection this [int index]
{
get
{
return (ControlItemCollection)_CollectionItems[index];
}
}

public ControlItemCollection()
{
_CollectionItems = new ArrayList();
}

public void Add(ControlItem controlItem)
{
_CollectionItems.Add(controlItem);
}
}


//
// Notes:
// In an ideal declaration, this is how I would declare it:
//

public ControlItemCollection Items
{
set
{
_Items.Add((ControlItem)value);
}

get
{
return _Items;
}
}

//
// However, it comes back during compile time stipulating:
// Cannot implicitly convert type
'MyControl.myLV.ControlItemCollection' to 'ControlItems.ControlItem'
// It makes sense though. The 'Items' is declared as
'ControlItemCollection'.
//


- - - - - - - - - -
Analysis of Design
- - - - - - - - - -

I won't bore you with the in depth details, unless you need more info.
Trying to keep this short and simple.

Once you create a ListViewItem object and try to add it to the
control:

lvSample.Items.Add(oItem);

If you stop at the ".Add" portion you will see the path:

System.Windows.Forms.ListViewItem
ListViewItemCollection.Add(System.Windows.Forms.ListViewItem value)(+2
overloads)

When I first started tinkering around with this, I used the following
code:


==========
MyControl
==========

public ControlItemCollection Items
{
set
{
_Items.Add(value.ToString());
}

get
{
return _Items;
}
}

public class ControlItemCollection
{
...

public void Add(string text)
{
}
}


And noticed when I executed the code:

lvMyControl.Items.Add(oItem);

If you stop at the ".Add" portion you would have seen the path:

void ControlItemCollection.Add(string text)



So tinkering around I made the following changes:

==========
MyControl
==========

public class ControlItemCollection
{
...

public ControlItem Add(string text)
{
return (new ControlItem());
}
}

And noticed that when you stop at the ".Add" portion you now see:

ControlItems.ControlItem ControlItemCOllection.Add(string text)

It's close, but I still run into the snag of the "ControlItem" object
being passed to the Collection reference that is defined by "Items".




I think the required changes would be something like:

==========
MyControl
==========

public class ControlItemCollection
{
...

public ControlItem Add(ControlItem controlItem)
{
return controlItem;
}
}

But I'm not really sure how to piece it all together, so the
'ControlItem' object is passed and added to the Master collection that
resides within the Control itself.

If anyone can help me out with this complexity.. my hats off to ya.
 

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