Make custom controls a container

Source: Internet
Author: User

After opening a custom control, you can drag the control on the toolbar to the custom control. After observing the pannel control, it seems very simple.

Inherited from scrollablecontrol, while scrollablecontrol inherited from control. Both classes do not overwrite the painting, but add some attributes and override some attribute values. Control has a set attribute of controls. It seems that most of the work has been done.

I tried to add a button directly to Control. controls.

Code public class trackpannel: Control
{
//...
Private void drawtrack (Graphics GC, rectangle rect)
{
Int ibottony = rect. Y + rect. height;
Int icur = 0;
// Int iindex = 0;
While (icur <rect. width)
{
// Iindex ++;
Icur = icur + this. tackspacing;
GC. drawline (pens. Black, icur, 2, icur, ibottony );
}
Button BTN = new button ();
BTN. Click + = new eventhandler (btn_click );
This. Controls. Add (BTN );
}
//

 

The button is displayed normally and receives a click event. Prove that I am right. Take a closer look at scrollablecontrol. Two steps are required.

1. Set controlstyles. containercontrol

2. Set Designer

The modified code is as follows.

Code [designer ("system. Windows. Forms. Design. parentcontroldesigner, system. Design ",
Typeof (system. componentmodel. Design. idesigner)]
Public class trackpannel: Control
{
/// <Summary>
/// Constructor
/// </Summary>
Public trackpannel ()
{
Base. setstyle (controlstyles. userpaint, true );
Base. setstyle (controlstyles. containercontrol, true );
}
//...

"Base. setstyle (controlstyles. userpaint, true);" indicates the onpaint that the program uses to override by the control. It cannot be rewritten. You have to set this parameter! At half past one, it's really hard to touch.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.