Hi all,
I have the following problem: I created my own control inherited by the UltraTextEditor and just added two right buttons in right with icons and some code in the event handlers. Everything is fine design time, when I place my control in a form I can see my 2 buttons, but when I build and run the application the VS generates in the IntializeComponent() method code that add my buttons again and as result runtime I see 4 buttons instead 2. I’ll appreciate if anybody tell me why this is happened and how to prevent this.
Thanks in advance.
I had the same problem and I resolved it by using the following code.
In the Inherited control, create a new ReadOnly property called ButtonsRight (or ButtonsLeft if that's what you are using). This property should just return the base control's ButtonsRight property. Mark this property with the attribute DesignerSerializationVisibility set to Hidden.
The drawback to this is that if you try to inherit your inherited control and you want to add more buttons, you will have to do it in code.
I'm using this in several projects and it works just fine for me.
If you need, I can send you a code sample.
I did it in the OnCreateControl() and it seems to be working... but this is something like a patch. When I add the controls in OncreateControl whendrag and drop the control from the tool box desgn time severything is ok, but VS create an instance of the control in order to visualize in in the designer in this moment in the ButtonsRight collections there are 2 buttons already (added from my OnControlCreate method) in this moment VS decide that have to serialize the collection in the from IncializeCompoent(). As result again I have 4 bottons instead 2. I used the fact that OnControlCreate is invoked after the form's InitializeComponent() and wrote a line code that decided my problems "ButtonsRigt.Clear()". And now the InitializeComponent add 2 buttons, OnCreateControl clear and add them again. For now it works but it seems to be patch instead solution.
Just from curiosity if anyone has better idea I'll appreciate it.
Thanks.
You could try overriding OnCreateControl instead of the constructor or another Init method.
-Matt
I tryed to check the collection before adding, but it doesn't work because the control Init metod for the is invoked before the from init method. In that case i have to go to the from designer and delete the buttons, but this is regenerated when i change something in the form. Any other ideas.
PS: The buttons collections is not ovveridable. I had idea to make a boolean property something like "ShowMyButtons" and when i set this to true (the default value) form the form designer it will add the buttons....
Since the ButtonsRight collection is public and seriliazable, it's going to get serialized by the form designer. If the property is overriable, which I don't think it is, you might be able to override the property and mark it with a DesignerSerialzationVisibility(false) attribute.
But what I typically do in a case like this is just check to make sure that the buttons don't already exist before I add them.