Dragged a custom control into a form , I want to give a property of the custom control load multiple values, how can I do?Dragged into a form in a custom control before or after what the property would trigger?
code like this:
public partial class MyControl : System.Windows.Forms.TextBox
{
public MyControl()
{
InitializeComponent();
}
private ShowFields _showFields = new ShowFields();
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Editor(typeof(ShowField_CollectionEditor), typeof(System.Drawing.Design.UITypeEditor))]
public ShowFields ShowFields
{
get
{
//I want to add two ShowField like this, when this custom control is dragged into a Form in the first time.
//But always add the two ShowField ,when the form is opened.//if (_showFields == null)
//{
// _showFields = new ShowFields();
// _showFields.AddRange(new ShowField[] { new ShowField("OurCode", "Code"), new ShowField("OurName", "Name") });
//}
return _showFields;
}
}
}
public class ShowField
{
private string _colName;
[Category("ShowItem")]
[DefaultValue(typeof(string), "")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public string ColName
{
get { return _colName; }
set
{
_colName = value;
}
}
private string _showName;
[Category("ShowItem")]
[DefaultValue(typeof(string), "")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public string ShowName
{
get { return _showName; }
set { _showName = value; }
}
public ShowField(string colName, string showName)
{
_colName = colName;
_showName = showName;
}
public ShowField()
{
}
}
public class ShowFields : System.Collections.CollectionBase
{
public ShowFields()
{
}
public ShowField Add(ShowField si)
{
this.InnerList.Add(si);
return si;
}
public void Remove(ShowField si)
{
this.InnerList.Remove(si);
}
public bool Contains(ShowField si)
{
return this.InnerList.Contains(si);
}
public ShowField this[int i]
{
get { return (ShowField)this.InnerList[i]; }
set
{
this.InnerList[i] = value;
}
}
public void AddRange(ShowField[] si)
{
this.InnerList.AddRange(si);
}
public ShowField[] GetValues()
{
ShowField[] si = new ShowField[this.InnerList.Count];
this.InnerList.CopyTo(0, si, 0, this.InnerList.Count);
return si;
}
}
public class ShowField_CollectionEditor : System.ComponentModel.Design.CollectionEditor
{
private Type[] types;
public ShowField_CollectionEditor(Type type)
: base(type)
{
types = new Type[] { typeof(ShowField) };
}
protected override Type[] CreateNewItemTypes()
{
return types;
}
}