Thursday, October 19, 2006

Custom Control Selection At Design Time

So I found this going through some old code this past week. I had built a custom header to interact with a Wizard control. I don't particularly care for the sidebar and I wanted navigation tabs floating above the Wizard. That was relatively easy to put together. It uses the ActiveStepIndex on the Wizard to determine its tab appearance, so when I dropped it into the Designer, I wanted to be able to select the Wizard1 control as the Wizard for my header to interact with. How you do that is with a TypeConverter.

A TypeConverter allows you to specify, at design time, the controls you would like your property to interact with. In this example, I have a string property on my WizardHeader control that I would like to set to "Wizard1", the ID of the Wizard on my WebForm. Now I could type that in, but I could also type in any number of other things that would not be the type of control I wish to use and would break when I went after the ActiveStepIndex property during execution.

So I built a custom TypeConverter that would return to me the IDs for the controls on the page that were of type Wizard. Here's how it's done:

First, I will inherit from StringConverter, which is the base Converter for capturing properties of type string. There are a couple of overrides. The most important is GetStandardValues. This allows you to customize how you want to acquire the values that are available through your converter.

By looping through the controls that are available in the component context container, I can check for their control type, and if they match the type I am looking for (in this case System.Web.UI.WebControls.Wizard), I add those IDs to a collection. At the end I build a string list and hand back a StandardValuesCollection based on that list.


using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Collections;

namespace WebControls.Wizards
{
public class WizardListConverter : StringConverter
{
private static
TypeConverter.StandardValuesCollection myReferences;

public WizardListConverter()
{

}

public override bool
GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}

public override bool
GetStandardValuesExclusive(ITypeDescriptorContext context)
{
return true;
}

public override TypeConverter.StandardValuesCollection
GetStandardValues(ITypeDescriptorContext context)
{
ArrayList matchingReferences = new ArrayList();

for (int i = 0; i < context.Container.Components.Count; i++)
{
if (context.Container.Components[i]
is System.Web.UI.WebControls.Wizard)
{
System.Web.UI.Control contr =
(System.Web.UI.Control)context.Container.Components[i];

matchingReferences.Add(contr.ID);
}
}

matchingReferences.Sort(0, matchingReferences.Count, null);

if (context.Container.Components.Count > 0)
{
string sSplitReferences = "";

for (int j = 0; j < matchingReferences.Count; j++)
{
if (matchingReferences[j] + "" != "")
{
sSplitReferences += "," + matchingReferences[j];
}
}

string[] references = sSplitReferences.Split(',');

myReferences = new
TypeConverter.StandardValuesCollection(references);
}
else
{
myReferences = new
TypeConverter.StandardValuesCollection(new string[] { "" });
}


return myReferences;
}
}
}


Now that that is done, I need to go back to my WizardHeader control and add the WizardListConverter as an attribute:


using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web.UI;

namespace WebControls.Wizards
{
public class WizardHeader : System.Web.UI.Control
{
//the id of the control I wish to interact with
private string m_sWizardControl;

[TypeConverter(typeof(WizardListConverter))]
public string WizardControl
{
get { return m_sWizardControl; }
set { m_sWizardControl = value; }
}
}
}


Now when I drop the WizardHeader onto a WebForm, I can simply select the appropriate wizard in the property list.



So now I can make it a lot easier to control the interaction between my custom controls at design time to make sure I am only interacting with the types of controls I want.

Talk to you later!

Rob