Control Inheritance (VS designer keeps adding ComponentTool)

Answered
0
0

Dear Team,

I have a derived TextBox with the tool functionality to clear TextBox contents.

The derived control is TextBoxExt and it has a property ClearTool, which dynamically adds/removed the tool with a cross (x).

 

The ClearTool property should have default value “true”.

 

I have troubles to make it work in the designer:

 

If I set ClearTool property to “true” outside TextBoxExt.InitializeComponent, the VS2019 designer does not initialize the tools.

If I set ClearTool property to “true” inside TextBoxExt.InitializeComponent, the VS2019 keeps adding multiple ComponentTools to the instance of the textbox.

 

This happens if I change the ClearTool value True->False->True in the designer for the instance of the control (e.g. textBoxExt2), and then close and re-open the designer.

Maybe related is that the “Tools” property of the textbox instance in VS designer shows “Read-Only”, but nevertheless allows modifications of the Tools collection.

 

Because I do not want the tools of the base control to be added to the instance of the control at the design time, I cannot set ClearTool property in the InitializeComponent of the base class.

Maybe I need to take care the visuals myself  during design time.

Attachment
  • You must to post comments
Best Answer
0
0

Here are the issues:

 

  1. You have DefaultValue(true) but the initial value is false.  It will not match and the property is serialized.
  2. When you add a tool by code after the designer is loaded the collection is different and therefore serialized, which is correct.
  3. The code in HandleClearToolState uses Add() instead of Insert() so the clear X will sometime be in front of the menu and sometimes after.

If you want to manage the tools collection by code add:

 [Browsable(false)]
 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
 public new ComponentToolCollection Tools
 {
   get => base.Tools;
 }

 

  • Kizaemon
    Dear Luca, thank you. This is very insightful. I think I can make it work.
  • You must to post comments
Showing 1 result
Your Answer

Please first to submit.