To give your textboxes a distinctive look has always been a hassle. In the old VB6 days it was almost not possible. The only thing you could do, was to incorporate the textbox into a ActiveX-control and set the looks of the ActiveX-Control.
With the advent of VB.net there seem to be 3 distinctive ways to accomplish this task. With the XPTextbox-control I tried them all and will explain them shortly and point out the advantages and disadvantages.
Inherit a Textbox
The most elegant way of enhancing the textbox would be inheritance of course. This will give you everything from the textbox plus all the things you add to your derived class.
This works great for any functionality (like input validation, … ) that does not impact the visual design of the textbox. Because of the distinctive way a textbox is incorporated into the OS (especially with WinXP), you do almost nothing to display a different border style, border color or other visual elements inside the textbox.
Because of these problems I had to skip this approach and try the next one
Incorporate a Textbox in a Usercontrol
The next thing I tried, was the same approach you’d take in VB6. I created a user-control that hosts a textbox and all the “skinning” and UI design takes place inside the user-control but outside the textbox itself.
This approach has some advantages like the capsulation of the control and the design-time interaction in VS.net. You only have to override the OnPaint-Method of the user-control and paint whatever you like into the user control.
It has one big disadvantage though: you don’t inherit the textbox, you just incorporate it. To give the developer the same properties, methods and events he is used to from the textbox control you have to wrap every single call or property of the textbox to the user control. This is a tedious task and also quite error prone.
I have done this to some extend with the XPTextBox control of the XPCommonControls suite. It draws a nice rounded border and a focus marker (an orange line like the tab control) when appropriate. But I did not wire up all the properties and methods and events from the text box to the user control. And a lot of mail came in requesting this or that property or method (Including databinding).
And since I don’t like to produce a lot of senseless source code I tried to find another solution to this problem
Using an ExtenderProvider
And while looking for a way of to give the themed controls of the XPCommonControls suite the ability to adapt automatically to any new theme in the OS I found the Skybound Website. Not only do they provide a .net wrapper for the uxTheme calls, they also extend the theming support of any .net Form.
They do so by using an ExtenderControl and then drawing the themes according to the control type.
I found this idea to be a very clever way of doing your custom painting. The approach has all the advantages and almost none of the disadvantages of the other approaches.
You write an extender control that hooks up to your form and then checks for any control type you’d like to owner-draw. You can even add a property that tells the extender if this instance of a control should be drawn or not.
You then override the OnPaint event of your parent form and draw any shape around the textboxes.
This way the developer has full control of any method/property of the textbox (since it’s still the original textbox type provided by Microsoft) but you can add any custom drawing to the form the textbox is attached to.
See the XPCommonControls XPTextBoxProvider class for an implementation of this approach.
Conclusion
As you can see, there are advantages and disadvantages to all approaches from above. But since the .net Framework offers us the possibility to write provider classes this will be my favorite way to add advanced behaviour to any standard control.
Be the first to rate this post
- Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5