Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Inheritance Is a Wonderful Thing : Page 2

Once you license a domain name, it's yours forever. If you ever let it go, you will never get it back. And most likely, you won't like its destination minutes after you neglect to pay the fee.


advertisement
Inheritance Simplifies Many Tasks
Speaking of inheritance (see how that works?), I've been having the greatest time lately adding features to Windows Forms controls using inheritance. I hang out a lot at www.windowsforms.com (and so should you, if you develop Windows applications with .NET), and answer questions on the public forums there. I learn a lot in the process, and recently, a spate of questions has come up in which people ask, "How do I <x> with the <y> control?" In most cases the answer is the same: Create a new class or control that inherits from the base control class, and expose information that's otherwise set as protected in the base class. As you probably know, members marked as protected are available only in the original class, and in classes that inherit from the class. The designers of the .NET Framework exposed a great deal of functionality in this way, but to get at it, you must inherit from the class.

For example, someone recently asked how you could restrict the size of a CommandButton control at design time. That is, he wanted to be able to allow developers using the CommandButton control in his shop to only create buttons that are sized so that the height is between 50 and 200 units, and the width is between 25 and 100 units. There's nothing public available in the System.Windows.Forms.Button class that allows this, but there is a protected method, SetBoundsCore, that allows you to intercede as the control is moved or sized, and tell the designer exactly how you want the control to work.

To use SetBoundsCore, you'll need to create a new class that inherits from the Button class. Simply add a new class to your project, give it a new name (I'll call mine ButtonMaxSize), and have it inherit from the Button class, like this:

Public Class ButtonMaxSize Inherits Button End Class

You can, if you like, create a new control that includes this class, so you can add it to the Controls toolbox, but I'll leave that as an exercise for the reader.

If you investigate the SetBoundsCore method in the .NET Framework Help, you'll see that it accepts a number of parameters indicating the current coordinates and size of the control, along with a parameter of type BoundsSpecified, an enumerated value that indicates which of the coordinates has been changed (All, None, Height, Width, Location, Size, X, Y). Your job is to determine which coordinate has been changed, and if it's the Size coordinate, ensure that the current coordinates are within the correct bounds. Once you've adjusted the coordinates, your code must call the base class' SetBoundsCore method, passing in the new coordinates.

It's simple stuff, but it's only possible because .NET supports inheritance. I wrote up a simple demo of this behavior:

Protected Overrides Sub SetBoundsCore( _ ByVal x As Integer, ByVal y As Integer, ByVal Width As Integer, ByVal Height As Integer, ByVal specified As BoundsSpecified) If specified And BoundsSpecified.Size Then Width = Math.Min(Width, 200) Width = Math.Max(Width, 50) Height = Math.Min(Height, 100) Height = Math.Max(Height, 25) End If MyBase.SetBoundsCore(x, y, _ Width, Height, specified) End Sub

To test the class, I created a simple form and placed a standard Button control on the form. In the form's code, I replaced all instances of "System.Windows.Forms.Button" with "ButtonMaxSize," and tested it out. As I expected, I was no longer able to size the button at design time larger than 200 by 100, or smaller than 50 by 25. This is cool stuff! I've run across at least three or four other fun examples of this technique—inheriting from a base control class to enhance its functionality—and I'll try to show off a few more in the coming months.

What have you learned this month? In short, inheritance is really cool when it comes to Windows Forms controls. In addition, once you license a domain name, don't let it lapse—you never know who's waiting in the wings, hoping to "inherit" the name from you. Unlike inheritance using controls, domain name inheritance is rarely cool.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date