PropertyGrid and CheckedListBox

G

Guest

Can someone show me an example of how to place a "CheckedListBox" property
within a PropertyGrid?
 
L

Linda Liu [MSFT]

Hi Steve,

Thank you for posting.

Do you mean that you want to place a "CheckedListBox" property within a
PropertyGrid in your own program?

If so, I think you should inherite a class from DesignSurface, add a
reference to ISelectionService instance in it and handle the
ISelectionService instance's SeletionChanged event.

Then you could add a CheckedListBox control and a PropertyGrid control on a
form in code. When the program is running, a CheckedListBox control appears
on the form. After you select the CheckedListBox on the form, its property
will be shown in the PropertyGrid.

Below is the sample code.
=====================================HostSurface.cs=========================
=============
using System;
using System.Collections;
using System.ComponentModel.Design;
using System.Windows.Forms;

public class HostSurface : DesignSurface
{
private ISelectionService _selectionService;
public HostSurface() : base()
{
// Set SelectionService - SelectionChanged event handler
_selectionService =
(ISelectionService)(this.ServiceContainer.GetService(typeof(ISelectionServic
e)));
_selectionService.SelectionChanged += new
EventHandler(selectionService_SelectionChanged);
}

// When the selection changes this sets the PropertyGrid's selected
component
private void selectionService_SelectionChanged(object sender,
EventArgs e)
{
if (_selectionService != null)
{
ICollection selectedComponents =
_selectionService.GetSelectedComponents();
PropertyGrid propertyGrid =
(PropertyGrid)this.GetService(typeof(PropertyGrid));
if (propertyGrid != null)
{
object[] comps = new object[selectedComponents.Count];
int i = 0;

foreach (Object o in selectedComponents)
{
comps = o;
i++;
}
propertyGrid.SelectedObjects = comps;
}
}
}

public void AddService(Type type, object serviceInstance)
{
this.ServiceContainer.AddService(type, serviceInstance);
}
}
============================================================================
==========

=====================================Form1.cs===============================
==========
using System.ComponentModel.Design;

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
HostSurface surface = new HostSurface();
surface.BeginLoad(typeof(Form));
Control view = (Control)surface.View;
view.Dock = DockStyle.Fill;
view.Parent = this;
surface.AddService(typeof(PropertyGrid), this.propertyGrid1);

// add a checkedlistbox onto the form designer
IDesignerHost idh =
(IDesignerHost)surface.GetService(typeof(IDesignerHost));
IToolboxUser tbu = idh.GetDesigner(idh.RootComponent as
IComponent) as IToolboxUser;

if (tbu != null)
{
ToolboxItem toolboxitem = new
ToolboxItem(typeof(CheckedListBox));

tbu.ToolPicked((System.Drawing.Design.ToolboxItem)toolboxitem);
}
}
}
============================================================================

For more information on DesignSurface, you may refer to the following link:
http://msdn.microsoft.com/msdnmag/issues/06/03/DesignerHosting/default.aspx

Hope this helps.
If you have anything unclear, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Please accept my appology for not being more clear. I have a PropertyGrid in
a form.

I want to dynamically read values from a file and create a checkedlistbox
from that list.

I want to place in my propertygrid a property that will, when selected,
display a CheckedListBox with the values read from the file. I then wish to
check the appropriate values in the list and have those returned to my
PropertyGrid property for later processing. Is this more clear?

--
-----------
Thanks,
Steve


Linda Liu said:
Hi Steve,

Thank you for posting.

Do you mean that you want to place a "CheckedListBox" property within a
PropertyGrid in your own program?

If so, I think you should inherite a class from DesignSurface, add a
reference to ISelectionService instance in it and handle the
ISelectionService instance's SeletionChanged event.

Then you could add a CheckedListBox control and a PropertyGrid control on a
form in code. When the program is running, a CheckedListBox control appears
on the form. After you select the CheckedListBox on the form, its property
will be shown in the PropertyGrid.

Below is the sample code.
=====================================HostSurface.cs=========================
=============
using System;
using System.Collections;
using System.ComponentModel.Design;
using System.Windows.Forms;

public class HostSurface : DesignSurface
{
private ISelectionService _selectionService;
public HostSurface() : base()
{
// Set SelectionService - SelectionChanged event handler
_selectionService =
(ISelectionService)(this.ServiceContainer.GetService(typeof(ISelectionServic
e)));
_selectionService.SelectionChanged += new
EventHandler(selectionService_SelectionChanged);
}

// When the selection changes this sets the PropertyGrid's selected
component
private void selectionService_SelectionChanged(object sender,
EventArgs e)
{
if (_selectionService != null)
{
ICollection selectedComponents =
_selectionService.GetSelectedComponents();
PropertyGrid propertyGrid =
(PropertyGrid)this.GetService(typeof(PropertyGrid));
if (propertyGrid != null)
{
object[] comps = new object[selectedComponents.Count];
int i = 0;

foreach (Object o in selectedComponents)
{
comps = o;
i++;
}
propertyGrid.SelectedObjects = comps;
}
}
}

public void AddService(Type type, object serviceInstance)
{
this.ServiceContainer.AddService(type, serviceInstance);
}
}
============================================================================
==========

=====================================Form1.cs===============================
==========
using System.ComponentModel.Design;

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
HostSurface surface = new HostSurface();
surface.BeginLoad(typeof(Form));
Control view = (Control)surface.View;
view.Dock = DockStyle.Fill;
view.Parent = this;
surface.AddService(typeof(PropertyGrid), this.propertyGrid1);

// add a checkedlistbox onto the form designer
IDesignerHost idh =
(IDesignerHost)surface.GetService(typeof(IDesignerHost));
IToolboxUser tbu = idh.GetDesigner(idh.RootComponent as
IComponent) as IToolboxUser;

if (tbu != null)
{
ToolboxItem toolboxitem = new
ToolboxItem(typeof(CheckedListBox));

tbu.ToolPicked((System.Drawing.Design.ToolboxItem)toolboxitem);
}
}
}
============================================================================

For more information on DesignSurface, you may refer to the following link:
http://msdn.microsoft.com/msdnmag/issues/06/03/DesignerHosting/default.aspx

Hope this helps.
If you have anything unclear, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
L

Linda Liu [MSFT]

Hi Steve,

Thank you for your quickly response.

I am sorry that I may not understand your meaning.
I want to dynamically read values from a file and create a checkedlistbox
from that list.
Do you mean that you want to initialize the items of the CheckedListBox
with the value in the file?
I want to place in my propertygrid a property that will, when selected,
display a CheckedListBox with the values read from the file.
Do you mean that you want to display the property of the CheckedListBox in
the PropertyGrid at run time?

If I'm off base, please feel free to tell me.

I think you could place a CheckedListBox and a PropertyGrid controls on
your form. In the form's Load event handler, you may initialize the items
of the CheckedListBox and then set this CheckedListBox to the
SelectedObject property of the PropertyGrid to display the properties of
the CheckedListBox in the ProportyGrid.

The sample code is like below.
private void Form1_Load(object sender, EventArgs e)
{
// I add items to the CheckedListBox directly here. You may replace
it with your actual code
this.checkedListBox1.Items.Add("1");
this.checkedListBox1.Items.Add("2");
this.propertyGrid1.SelectedObject = this.checkedListBox1;
}

Hope this helps.


Sincerely,
Linda Liu
Microsoft Online Community Support
 
L

Linda Liu [MSFT]

Hi Steve,

I am closely monitoring the newsgroup and I am contacting you to check the
issue status.

If the problem is not resolved or you have anything unclear, please feel
free to post in the newsgroup and we will follow up.

Thank you for using our MSDN Managed Newsgroup Support Service!


Sincerely,
Linda Liu
Microsoft Online Community Support
 
L

Linda Liu [MSFT]

Hi Steve,

Thank you for your response. I am very glad to hear that you have solved
the problem.

If you have any other questions in the future, please don't hesitate to
contact us. It's always our pleasure to be of assistance.


Sincerely,
Linda Liu
Microsoft Online Community Support
 

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