I have tried to write my own component to change some control properties. The idea is that I drop this component from toolbox to the form, and in property windows in designer I can select for which control it should be used, so like this:
My code is:
public class ShapeForm : Component
{
private int _shape;
public ShapeForm()
{
Shape = 20;
}
[TypeConverter(typeof(ControlTypeConverter))]
public Control TargetControl { get; set; }
[Browsable(false)] // don't show in the property grid
public List<Control> Controls { get; private set; }
public ContainerControl Target { get; set; } = null;
public override ISite Site
{
get { return base.Site; }
set
{
base.Site = value;
IDesignerHost host = value?.GetService(
typeof(IDesignerHost)) as IDesignerHost;
IComponent componentHost = host?.RootComponent;
if (componentHost is ContainerControl)
{
Target = componentHost as ContainerControl;
Controls = new List<Control>();
foreach (Control control in (componentHost as ContainerControl).Controls)
{
Controls.Add(control);
}
;
}
}
}
public class ControlTypeConverter : TypeConverter
{
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
// we only know how to convert from to a string
return typeof(Control) == destinationType;
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value,
Type destinationType)
{
if (typeof(string) == destinationType)
{
Control control = value as Control;
if (control != null)
return control;
}
return "(none)";
}
}
[
Category("Shaping"),
Description("Set corner shape radius"),
Browsable(true)
]
public int Shape
{
set
{
_shape = value;
//SetShaping();
}
get { return _shape; }
}
}'
But I get only one element and not all controls in this form like on first screenshot:
[ ]
Use this code;
[Browsable(true)]
[Editor(typeof(TestDesignProperty), typeof(UITypeEditor))]
[DefaultValue(null)]
public string SelectedObject { get; set; }
public class TestDesignProperty : UITypeEditor
{
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.DropDown;
}
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
var edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
ListBox lb = new ListBox();
var form1 = (Form1) Activator.CreateInstance(typeof (Form1)); //Change with your form class
foreach (Control control in form1.Controls)
{
lb.Items.Add(control.Name);
}
if (value != null)
{
lb.SelectedItem = value;
}
edSvc.DropDownControl(lb);
value = (string)lb.SelectedItem;
return value;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.