CollectionEditor

R

russ.roeber

Greetings,
I would like to find an example of using the CollectionEditor during
runtime.
I can't seem to find a simple straightforward example of this
anywhere. I have seen and used examples of the CollectionEditor, but
they where all brought up through the PropertyGrid. One of the
properties was a collection so the PropertyGrid displayed the (...)
thing, I click and there is the CollectionEditor allowing me to edit
the collection.

But how do I bring up the CollectionEditor directly without having to
go through a PropertyGrid?
 
G

Guest

Hi Russ,

At runtime? A little complex, but doable.

For a working example, create a form with a listbox and a button.

Add a click event for your button like so (I hope the code formatting works
ok):

private void button1_Click(object sender, System.EventArgs e)
{
PropertyDescriptor pd = TypeDescriptor.GetProperties(listBox1)["Items"];
UITypeEditor editor = (UITypeEditor)pd.GetEditor(typeof(UITypeEditor));
RuntimeServiceProvider serviceProvider = new RuntimeServiceProvider();
editor.EditValue(serviceProvider, serviceProvider, listBox1.Items);
}

And here's the implementation of the RuntimeServiceProvider (the bare
minimum to get this working):

public class RuntimeServiceProvider : IServiceProvider, ITypeDescriptorContext
{
#region IServiceProvider Members

object IServiceProvider.GetService(Type serviceType)
{
if (serviceType == typeof(IWindowsFormsEditorService))
{
return new WindowsFormsEditorService();
}

return null;
}

class WindowsFormsEditorService : IWindowsFormsEditorService
{
#region IWindowsFormsEditorService Members

public void DropDownControl(Control control)
{
}

public void CloseDropDown()
{
}

public System.Windows.Forms.DialogResult ShowDialog(Form dialog)
{
return dialog.ShowDialog();
}

#endregion
}

#endregion

#region ITypeDescriptorContext Members

public void OnComponentChanged()
{
}

public IContainer Container
{
get { return null; }
}

public bool OnComponentChanging()
{
return true; // true to keep changes, otherwise false
}

public object Instance
{
get { return null; }
}

public PropertyDescriptor PropertyDescriptor
{
get { return null; }
}

#endregion
}

Run your project, click the button, add some items, and when you click ok
they should appear in the listbox. I hope this was what your question was
asking! :)

Hope this helps.

Regards,
Matt Garven
 

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