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.
Where/when are you adding the buttons? It's sounds like wherever you are doing this is getting called twice, likely by VS's serialization mechanism.
-Matt
I add the buttons in the control constructor, I tried to do this in InticializeConponent() - the result is the same. When i add this control from the desgner in any form it generates the code that add the buttins again in the form constructor. I know that this is something related with the VS serialization mechanisum, but I don't know how to avoid this. Where in the inherited control I have to write the code in order to be executed only in the inferited control in to not be generated again from the desgner when i place the control anywhere?
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.
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.
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....