How to embed a third-party control into a toolstrip control and provide design-time support

Source: Internet
Author: User

Recently, due to work needs, I have studied how to embed a third-party control into a toolstrip control and provide support in design-time.

The following describes how to embed the system monthcalendar control into the toolstrip control.

The following two images show the final appearance.

 

 

Run-time: 1

 

 

Design-time: 1

 

 

How to embed third-party controls into toolstrip

 

Microsoft implements a toolstripcontrolhost class, which is a basic class of toolstripcombobox, toolstriptextbox, and toolstripprogressbar. toolstripcontrolhost provides the ability to embed third-party controls into toolstrip. We can use it in two ways:

 

1. We can directly construct a toolstripcontrolhost instance using a third-party control inherited from the control class as a parameter of the toolstripcontrolhost constructor, add this instance to the items set of toolstrip as the item of toolstrip.

Private void form2_load (Object sender, eventargs E)

{

Monthcalendar = new monthcalendar ();

Monthcalendar. firstdayofweek = day. Monday;

This. toolstrip1.items. Add (New toolstripcontrolhost (monthcalendar ));

}

 

 

2. Derived from toolstripcontrolhost, a third-party control is used as a parameter in the inherited default class parameter constructor to call the parameter constructor of the base class. this method can easily expose attributes, methods, and events in the inheritance class for easy access.

The implementation steps are as follows:

1. Extended toolstripcontrolhost. Implement a default constructor that calls the base class constructor that passes in the required control.

Public toolstripmonthcalendar (): Base (New monthcalendar ())

{

}

 

2. Declare an attribute of the same type as the packaging control and return control as the correct type in the property accessors.

Public monthcalendar monthcalendarcontrol

{

Get

{

Return base. Control as monthcalendar;

}

}

 

3. Use attributes and methods in the extension class to publish other common attributes and methods of the packaging control.

// Expose the monthcalendar. firstdayofweek as a property.

Public day firstdayofweek

{

Get

{

Return this. monthcalendarcontrol. firstdayofweek;

}

Set

{

Value = This. monthcalendarcontrol. firstdayofweek;

}

}

 

// Expose the addboldeddate method.

Public void addboldeddate (datetime datetobold)

{

This. monthcalendarcontrol. addboldeddate (datetobold );

}

 

4. You can also override the onsubscribecontrolevents and onunsubscribecontrolevents methods and add control events to be made public.

// Subscribe and Unsubscribe the control events you wish to expose.

Protected override void onsubscribecontrolevents (control C)

{

// Call the base so the base events are connected.

Base. onsubscribecontrolevents (C );

 

// Cast the control to a monthcalendar control.

Monthcalendar monthcalendarcontrol = (monthcalendar) C;

 

// Add the event.

Monthcalendarcontrol. datechanged + = new daterangeeventhandler (ondatechanged );

}

 

Protected override void onunsubscribecontrolevents (control C)

{

// Call the base method so the basic events are unsubscribed.

Base. onunsubscribecontrolevents (C );

 

// Cast the control to a monthcalendar control.

Monthcalendar monthcalendarcontrol = (monthcalendar) C;

 

// Remove the event.

Monthcalendarcontrol. datechanged-= new daterangeeventhandler (ondatechanged );

}

 

5. Provide necessary packaging for events to be made public.

// Declare the datechanged event.

Public event daterangeeventhandler datechanged;

// Raise the datechanged event.

Private void ondatechanged (Object sender, daterangeeventargs E)

{

If (datechanged! = NULL)

{

Datechanged (this, e );

}

}

 

For a complete example, see msdn (http://msdn2.microsoft.com/en-us/library/9k5etstz.aspx ).

 

 

How to provide design support

 

1. Add the toolstripitemdesigneravailabilityattribute label to the class inherited from toolstripcontrolhost. it enables toolstrip to find third-party controls inherited from toolstripcontrolhost in design-time. The direct effect is that third-party controls can be directly listed in the drop-down menu. in addition to embedding third-party controls in toolstrip, we can also directly embed third-party controls into contextmenustrip, menustrip, and statusstrip.

The toolstripitemdesigneravailability enumeration members are as follows:

 

Member name

Description

 

All

All the specified controls can be seen.

 

Contextmenustrip

Specify that contextmenustrip is visible.

 

Menustrip

Specify that menustrip is visible.

 

None

Specifies that no controls are visible.

 

Statusstrip

Specify that statusstrip is visible.

 

Toolstrip

Specify that toolstrip is visible.

 

2. Add the toolboxbitmapattribute label to the inherited control to change the default icon.

[Toolboxbitmap (typeof (monthcalendar)]

[Defaultproperty ("value")]

[Toolstripitemdesigneravailability (

Toolstripitemdesigneravailability. contextmenustrip |

Toolstripitemdesigneravailability. menustrip |

Toolstripitemdesigneravailability. statusstrip |

Toolstripitemdesigneravailability. toolstrip)]

Public class toolstripmonthcalendar: toolstripcontrolhost

{

 

-End-

-- Kevin Shan

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.