Wednesday, April 21, 2010

Toggle Control Selection – Custom Form Designer

There are lot of applications in which the user have a capability to design the a win form at runtime using Form Designer API provided in .NET. For more information about custom form designer Visit:http://msdn.microsoft.com/en-us/magazine/cc163871.aspx.

During this operation, there are chances of not allowing the user to delete the controls added to the Form designer.

On the similar lines, we had a requirement where in once the user creates a Form and checks in to the server, then the user is not allowed to delete the controls. In this post Controls are referred as widgets The delete option was not provided due to other important business requirements.

Later, we had a change request as: "Widgets should not be allowed to remove from the Form, but should be made hidden in both Preview & Designer mode"

“Visible” property cannot be used here, because it gives a different meaning. Visible property is used to hide the control at the Run-time (preview mode) not during the design time. So, we suggested to have a custom property for each control as “Hidden”, Once the “Hidden” is set to true, Visible will be set to False as well and the control is hidden from the Designer surface as well.

Everything looked simple till here. But later when started implementing the code we started to face lot of issues.

Ø Even though the Visible property is changed, the same is not reflected in Property Grid pane

Ø When the user uses Keyborad Shortcut – Ctrl + A, selects even Hidden Controls

Updating Property Grid:

For most of the Control Properties, if the value is changed programmatically, the same is reflected in the Property Grid as well. For example, set TextBox.Name = “NewTextBox”, and check the Property Grid pane, “NewTextBox” is shown in “Name” item. Similarly if the set TextBox.Visible = “False”, check the property grid pane, “Visible” item still shows as “True”. Weird!!!

Solution:

To resolve this issue, subscribe for Property Grid Value Changed event. Based on the user selection find the Item with in the Property grid and we need to explicitly set the value.

Code Snippet:
private System.Windows.Forms.PropertyGrid pgrdWidgets;
pgrdWidgets.PropertyValueChanged += pgrdWidgets_PropertyValueChanged;

private void pgrdWidgets_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
{
if (e.ChangedItem.Label.ToUpper(CultureInfo.InvariantCulture).Equals("VISIBLE") && (bool) e.ChangedItem.Value)
{
foreach (var control in ((PropertyGrid)(s)).SelectedObjects)
{
var ctl = control as IWigdetCustomProperty;
if (ctl != null)
{
if (ctl.IsHidden)
{
ctl.IsHidden = false;
}
}
}
}
}

By now we were able to fix the first issue.

Next, if the user selects all the controls use Keyboard shortcut: Ctrl + A, the hidden widgets are also selected. To resolve this issue we did the following trick:

Code Snippet:

private void ToggleWidgetSelection()
{
if (selectionService_ != null)
{
IList hiddenControls = new List();
foreach (var component in selectionService_.GetSelectedComponents())
{
Control control = component as Control;
if (control != null)
{
if (IsWidgetHidden(control))
{
if (!hiddenControls.Contains(component))
{
hiddenControls.Add(component);
}
if(control.GetType().ToString().Equals("Symyx.Notebook.Sections.Forms.Widgets.GroupBox"))
{
GetChildControls(control, hiddenControls);
}
}
}
}
selectionService.SetSelectedComponents(hiddenControls, SelectionTypes.Toggle);
}

To create our own custom form designer, we need to implement certain Interfaces. ISelectionService is one of the Interface from which we can manage control's selection status.


No comments:

Post a Comment