How can I make the IDE create correct code?
I implement a component, but I can't get it's paraent (a control which contain the component).
So I want to get the paraent from constructor's parameter.
But the IDE can't create corrent code for my new constructor.
How can I make the IDE create correct code?
I want to let the IDE automatic create the correct code, because I want to get the paraent inDesign Time mode.example:
The illegal code:
| |
#region Component Designer generated code/// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.button1 = new System.Windows.Forms.Button(); this.localizeEngine1 = new GrapeCity.Win.Input.Design.LocalizeEngine(this.components); ((System.ComponentModel.ISupportInitialize)(this.localizeEngine1)).BeginInit(); this.SuspendLayout(); // // button1 // ...
|
|
The correct code:
| |
#region Component Designer generated code/// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.button1 = new System.Windows.Forms.Button(); this.localizeEngine1 = new GrapeCity.Win.Input.Design.LocalizeEngine(this.components,this); ((System.ComponentModel.ISupportInitialize)(this.localizeEngine1)).BeginInit(); this.SuspendLayout(); // // button1 // ...
|
|
Thank you!
You should provide information to the designer on how to serialize your object by creating your own class derived from System.ComponentModel.TypeConverter, and use that class on your custom type (LocalizeEngine).From your code, I read that you want the designer to serialize your custom type correctly by calling the correct constructor. If what I assume was correct, your typeConverter should implement a converter for
InstanceDescriptor.Override the CanConvertTo method:
| |
public override bool CanConvertTo(ITypeDescriptorContext context, Type destType) { if (destType == typeof(InstanceDescriptor)) return true; return base.CanConvertTo(context, destType); }
|
Then, put your implementation to overriden ConvertTo method: | |
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destType) { if (destType == typeof(InstanceDescriptor)) { System.Reflection.ConstructorInfo ci = typeof(LocalizeEngine).GetConstructor(new Type [] { <provide your type signatures here> }); return new InstanceDescriptor(ci, null, false); } return base.ConvertTo(context, culture, value, destType); }
|
If this didn't solved the problem, I guess you might want to implement CodeDOM to work with the serialization of your custom type.
Best Regards,
-chris
Hi Kevin,
I think there's another option you have. If you want the parent of a component, you can use a cool hack to get it at design time, store it as a property, and have the code generator set that property for you. Take a look at the following:
| |
private Form _parent; [Browsable(false)] public Form Parent { get { if (_parent == null && DesignMode) { IDesignerHost host = GetService(typeof(IDesignerHost)) as IDesignerHost; if (host != null) _parent = host.RootComponent as Form; } return _parent; } set { if (!DesignMode) { if (_parent != null && _parent != value) { throw new InvalidOperationException(...); } else { _parent = value; } } else { _parent = value; } } }
|
Check out Chris Sells' WinForms programming book for a detailed explanation of why this works and gets you a reference to the parent form.
Thanks,
Ted
@Ted...
The warning "r:\dotnet\...\qlabel.cs(188,15): warning CS0108: The keyword new is required on 'Acme.Objects.QLabel.Parent' because it hides inherited member 'System.Windows.Forms.Control.Parent'" is displayed.
The warning is solvented renaming the property.When you attempt to serialize the component, the follow error is displayed: "The type System.Windows.Forms.Form in Assembly System.Windows.Forms, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 is not marked as serializable."
Regards