Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Creating Panels when Items are Added in CollectionBase at Designtime
 

Creating Panels when Items are Added in CollectionBase at Designtime

Hi,

I am creating a Custom Control in which i have a ToolStrip, when i add ToolStripButton in the Collection

that time i want to create Panels for that particular ToolStripButton at Design Time, so that i can add other

standard controls to that particular panel.

Please let how to do this in C# Windows Forms in .Net 2.0

Thanks & Regards

Ajay

AjayB79  Tuesday, October 09, 2007 11:57 AM

The following code example shows use the CollectionBase to add items and the owner control is updated on insert\remove\delete.

public class ImagesCollection : CollectionBase

{

private ListControl _ownerControl;

public ImagesCollection(ListControl ownerControl)

{

_ownerControl = ownerControl;

}

public ImageHolder this[int index]

{

get

{

return ((ImageHolder)List[index]);

}

set

{

List[index] = value;

}

}

public int Add(ImageHolder value)

{

return (List.Add(value));

}

public int IndexOf(ImageHolder value)

{

return (List.IndexOf(value));

}

public void Insert(int index, ImageHolder value)

{

List.Insert(index, value);

}

public void Remove(ImageHolder value)

{

List.Remove(value);

}

public bool Contains(ImageHolder value)

{

return (List.Contains(value));

}

protected override void OnInsert(int index, Object value)

{

// Insert additional code to be run only when inserting values.

_ownerControl.UpdateElements();

}

protected override void OnRemove(int index, Object value)

{

// Insert additional code to be run only when removing values.

_ownerControl.UpdateElements();

}

protected override void OnSet(int index, Object oldValue, Object newValue)

{

// Insert additional code to be run only when setting values.

_ownerControl.UpdateElements();

}

protected override void OnValidate(Object value)

{

if (value.GetType() != typeof(ImageHolder))

throw new ArgumentException("value must be of type ImageHolder.", "value");

}

}

On the collection ctor, you need to send the onwer control (your CustomToolStrip) and on the update events (insert\remove\set), call the UpdatePanels() method you have in your control, the one that adds panels.

Eli Gazit  Wednesday, October 10, 2007 11:39 AM

The following code example shows use the CollectionBase to add items and the owner control is updated on insert\remove\delete.

public class ImagesCollection : CollectionBase

{

private ListControl _ownerControl;

public ImagesCollection(ListControl ownerControl)

{

_ownerControl = ownerControl;

}

public ImageHolder this[int index]

{

get

{

return ((ImageHolder)List[index]);

}

set

{

List[index] = value;

}

}

public int Add(ImageHolder value)

{

return (List.Add(value));

}

public int IndexOf(ImageHolder value)

{

return (List.IndexOf(value));

}

public void Insert(int index, ImageHolder value)

{

List.Insert(index, value);

}

public void Remove(ImageHolder value)

{

List.Remove(value);

}

public bool Contains(ImageHolder value)

{

return (List.Contains(value));

}

protected override void OnInsert(int index, Object value)

{

// Insert additional code to be run only when inserting values.

_ownerControl.UpdateElements();

}

protected override void OnRemove(int index, Object value)

{

// Insert additional code to be run only when removing values.

_ownerControl.UpdateElements();

}

protected override void OnSet(int index, Object oldValue, Object newValue)

{

// Insert additional code to be run only when setting values.

_ownerControl.UpdateElements();

}

protected override void OnValidate(Object value)

{

if (value.GetType() != typeof(ImageHolder))

throw new ArgumentException("value must be of type ImageHolder.", "value");

}

}

On the collection ctor, you need to send the onwer control (your CustomToolStrip) and on the update events (insert\remove\set), call the UpdatePanels() method you have in your control, the one that adds panels.

Eli Gazit  Wednesday, October 10, 2007 11:39 AM

You can use google to search for other answers

Custom Search

More Threads

• dynamic checkboxes.
• Need to put controls in a horizontal way in a Toolbox
• How to hide "greyed" properties in Properties explorer
• C# control which acts like CRectTracker?
• How to set window system colours using API & c#
• Win32 and C# Window Styles. Why different ?
• Make panel on usercontrol container
• problem with designerhost sample
• set client area of control that inherits from panel
• Retrieving a combobox entry from a supplied index