Thursday, August 31, 2006

Changing Text on SplitContainer, what an interesting idea....

Today I rummaged through thousands lines of C# GUI code to find out why some things happen and some things didnt. Suddenly I see (through content assist) that one of the project specific controls has a TextChanged event. This control has nothing to do with text so it directly grabbed my attention.

Then I find out that it really isnt the specific control that implements the event, it is a control further up in the hiearchy. I keep going up the inheritance tree, to find that System.Windows.Forms.Control has a property named Text (and a bunch of events for text manipulations). That is odd I think, why would a base Control class have a text property when the deriving classes are not necessary text orientated controls.
  • What happens if I change the Text property on a PictureBox? Would it show the text on top of the picture?
  • What happens if I do it on a SplitContainer?
  • Or on a NumericUpDown control?
This is one of the problems with the .NET framework. It carries to much bagage from C++ and VB into the C# (and .NET) world. The old MFC libraries probably had a SetText() method, so therefore it must exist one in .NET. I wonder why they would have such functionality, and the only idea I can come up with is: It is simple if all text specific properties are named the same, and thus every control should have one. "If we keep it in Control, then we dont have to write code for text handling in every control". But what about all those controls that dont have a text in them? They must override this property and do nothing, so now we have code that hides other code just to remove a functionality. This does not make sense to me.

The MSDN will of course answer the question if the Text property actually works or not. On those controls that do not support them, it will say "This property supports the .NET Framework infrastructure and is not intended to be used directly from your code.". Sheeesh, that makes we want to think twice before using the Text property on a Label or Button. Perhaps the Text property is not intended to be used directly in my code on a button? The best explaination is (of course all these overriden properties have different wording) "The Text has no affect on the appearance of the NumericUpDown control; therefore, it is hidden in the designer and from IntelliSense.".. I would like to see that code in the IntelliSense.

if (control is SplitContainer) || (control is NumbericUpDown)

Or is there any valid reason that every Control has a Text property?