Hi Luis,
To make the commands pane of a PropertyGrid visible for objects that expose
verbs, we should set the ProprtyGrid's CommandsVisibleIfAvailable property
to true. In fact, this property is set to true by default.
Except that, there's nothing left to get a PropertyGrid to display commands
for objects that expose verbs.
The following is an example that implements a simple form designer using
DesignSurface. The sample contains a main form and a HostSurface class
derived form DesignSurface class. The main form displays a Form designer
and a PropertyGrid.
In the HostSurface there¡¯s an event handler for the ISelectionService's
SelectionChanged event so as to refresh the PropertyGrid when the selected
objects on the form designer changed.
The sampe code is as follows:
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.ComponentModel.Design;
using System.Drawing.Design;
using System.Collections;
// the definition of HostSurface
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[i] = o;
i++;
}
propertyGrid.SelectedObjects = comps;
}
}
}
public void AddService(Type type, object serviceInstance)
{
this.ServiceContainer.AddService(type, serviceInstance);
}
}
// the event handler for the main form's load event
private void Form1_Load(object sender, EventArgs e)
{
// add a custom form designer on the main form
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 TextBox on 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(TabControl));
tbu.ToolPicked((System.Drawing.Design.ToolboxItem)toolboxitem);
}
}
Hope this helps.
If you have any question, 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.
Luis F. Rodas - 10 Jan 2008 16:06 GMT
Hi Linda,
I tried the code that you sent me and it did not work. Could you sent all
files from solution of the sample ?
Best Regards,
Luis F.
(Indusoft Team)
> Hi Luis,
>
[quoted text clipped - 125 lines]
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
Linda Liu[MSFT] - 11 Jan 2008 07:21 GMT
Hi Luis,
Thank you for your reply!
I notice that you post another post related to this one, in which you ask
how to get a context menu to pop up when right-clicking on a custom form
designer.
I have replied to your on that post and I will send you a sample project
that covers the problems you ask in this particular post as well as that
post.
When you run the application, you'll see a TabControl on a form designer.
If you select the TabControl on the form, you should see four commands
appears on the command pane of the PropryGrid. If you righ-click on the
TabControl, a context menu pops up with four menu items in it.
If you have any question, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support
Luis F. Rodas - 14 Jan 2008 13:51 GMT
Hi Linda,
Your sample worked fine. But, do I need to implement an interface of
DesignInterface necessarily ? My program contains forms with some controls
(text boxes, tab controls) and I would like in runtime change your
properties using the design verbs in command pane of the property grid
control. Any way?
Best Regards,
Luis F.
(Indusoft Team)
> Hi Luis,
>
[quoted text clipped - 18 lines]
> Linda Liu
> Microsoft Online Community Support
Linda Liu[MSFT] - 15 Jan 2008 03:32 GMT
Hi Luis,
Thank you for your reply!
> do I need to implement an interface of DesignInterface necessarily ?
To get a context menu to pop up when right-clicking on a custom form
designer, we need to implement the IMenuCommandService interface.
> My program contains forms with some controls (text boxes, tab controls)
and I would like in runtime change your properties using the design verbs
in command pane of the property grid control.
You can make use of the TypeDescriptor class to change the properties of
the control on the custom form designer at runtime. The following MSDN
document introduces a good sample. Although the main purpose of this sample
is to demonstrate how to implement Behavior and smart tag in a custom
control designer, it also tells us how to change properties of controls.
http://msdn2.microsoft.com/en-us/library/ms171820.aspx
Hope this helps.
If you have any question, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support