Sometimes we are in a custom control, for some reason (such as: Preventing the mode mode error), you want to prohibit the size of the custom control (Height or Width). Initially I realized this, this is also a simple method:
Public class mybutton: System.windows.Forms.Button
{
...
Protected Override Void OnResize (Eventargs E)
{
THIS.HEIGHT = 23;
THIS.WIDTH = 75;
}
}
But I am not very satisfied with such an effect. If I can implement like TextBox, the small square on the edge of the upper and lower edges is ash, and the small square on the left and right is white (indicating that it is impossible to adjust its height). Cool! After some research and to 9CBS, I finally solved this problem, the effect is as shown in the following figure:
Now organize the code, I hope to help everyone.
1. Build a custom control designer class.
///
// / Custom Control Designer Class
/// summary>
Public class mybuttondesigner: system.windows.forms.design.controlDesigner
{
Public mybuttondesigner ()
{
}
Public Override SelectionRules SelectionRules
{
get
{
// Do not allow the height of the control, and see MSDN for details.
SelectionRules Rules = SelectionRules.visible | SelectionRules.Moveable |
SelectionRules.LightsizeAble | SelectionRules.rightSizeable;
Return Rules;
}
}
}
2. Add attributes to custom control classes to associate the control class with the designer class defined above.
[Designer (TypeOf (MyButtonDesigner)]]
Public class mybutton: System.windows.Forms.Button
{
...
}
After the above treatment, the above effect is achieved. But if you care more, you will find that this is only valid when designing, and during runtime, or can change the height of the control. How to avoid this problem? Add the following code in the appropriate position (if you have an unclear place, please check the MSDN).
Public class mybutton: System.windows.Forms.Button
{
...
Protected Override Void SetBoundScore (int X, int y, int width, int tent,
Boundsspecified Specified)
{
Base.setBoundScore (X, Y, Width, 23, Specified);
}
}
Finally, I will remind everyone: To successfully complete the compilation, you must add the system.design reference, and add:
Using system.windows.forms.design;