By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,659 Members | 910 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,659 IT Pros & Developers. It's quick & easy.

PropertyGrid and CheckedListBox

P: n/a
Can someone show me an example of how to place a "CheckedListBox" property
within a PropertyGrid?
--
-----------
Thanks,
Steve
Jul 20 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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.c s=========================
=============
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.GetServi ce(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[i] = 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(IDesigner Host));
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/is...g/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/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

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

Jul 21 '06 #2

P: n/a
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 [MSFT]" wrote:
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.c s=========================
=============
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.GetServi ce(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[i] = 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(IDesigner Host));
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/is...g/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/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

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

Jul 21 '06 #3

P: n/a
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
Jul 21 '06 #4

P: n/a
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
Jul 25 '06 #5

P: n/a
I was able to come up with a solution. Sorry for not letting you know sooner.
--
-----------
Thanks,
Steve
"Linda Liu [MSFT]" wrote:
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
Jul 25 '06 #6

P: n/a
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
Jul 26 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.