PropertyGrid and Commands Pane

  • Thread starter Thread starter Luis F. Rodas
  • Start date Start date
L

Luis F. Rodas

Hi all,

I developed a form designer using the PropertyGrid control. The commands
pane is not appearing for controls that have "hot commands" (example Tab
Control: AddTab and RemoteTab). I set the "CommandsVisibleIfAvailable"
property for true. Anybody has some idea (or sample) of what it can be
wrong?



Best Regards,

Luis F.

(Indusoft Team)
 
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 = 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.
 
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)

Linda Liu said:
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 = 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.
 
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
 
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,

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
 
Hi Luis,

Sorry for my delayed reply! I was OOF yesterday due to not feeling well.
1) Any way without implement DesignSurface ?

To implement a custom form designer, if you're using VS.NET 2003, you have
to implement all the related design-time services, such as IDesignerHost,
ISelectionService, IComponentChangeService, IMenuCommandService and so on,
which requires much efforts; if you're using VS 2005, you only need to make
use of the DesignSurface class because it has provided several design-time
services automatically.

The DesignSurface class implements what the user perceives as a designer.
DesignSurface is the user interface the user manipulates to change
design-time features. DesignSurface provides a completely self-contained
design surface.

The DesignSurface class can be used by itself, or the user can derive a new
class from it and augment the behavior. In my sample project, I create a
new class derived from the DesignSurface class.
2) The attached sample does not diplay the design verbs (Add Tab and
Remove Tab) of Tab Control in property grid control. Why ? Any workaround ?

The reason why no design verbs of the TabControl control is shown in the
PropertyGrid when your sample application is run is that you didn't add any
design-time function in your sample project at all. In short, when running,
your sample application should function like the Visual Studio IDE.

The workaround is to add desgin-time support in your application either by
implementing all the related design-time services or by using the
DesignSurface class.

Hope this helps.
If you have any other question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support
 
Back
Top