For example, to override the Control of a TextBox class, you naturally want it to be like a TextBox. After being dragged to the form, you can only change the width, not the height, unless you modify the Font. The implementation of this function is actually very simple. It is convenient for you to paste it out.
Example code:
[Designer (typeof (XControlDesigner)]
Public class XControl: Control
{
... // Implementation
}
Public class XControlDesigner: System. Windows. Forms. Design. ControlDesigner
{
Public XControlDesigner () // required in 2003, but not in 2005
{
}
Public override SelectionRules
{
Get
{
// For specific control items, refer to the enumerated members of SelectionRules.
SelectionRules rules = SelectionRules. Visible | SelectionRules. Moveable |
SelectionRules. LeftSizeable | SelectionRules. RightSizeable;
Return rules;
}
}
}
This function is implemented. If you use code to modify the height of the control, you will find that the height can still be modified. You must rewrite a method in the control:
Protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
{
// Calculate the height of the current Font
Int hgt = Font. Height; // The Height of the custom control may need to be calculated.
Base. SetBoundsCore (x, y, width, hgt, specified );
}