Building a Custom ToolStrip Control

G

Guest

Hi

I'm trying to create a composite control to be used on a ToolStrip,
consisting of a ToolStripComboBox and a ToolStripButton. It's going to be a
control used to search for text in a grid control on a WinForm.
I've got it working fine on a single form but now want to reuse this
functionality on other forms. Usually this would be a simple matter of
creating a UserControl, however a ToolStripComboBox cannot be placed on a
UserControl.

I've looked into the ToolStripControlHost however this only accepts a single
control in its constructor.

I need to make sure that I can handle the Enter and Leave events for the
composite control so that I can display / hide text "Type text to find".

What is the best way to create this control? Ideally I'd like to avoid
having to do my own Paint code etc.

thanks

Richard
 
L

Linda Liu [MSFT]

Hi Richard,

You're in the right direction to implement a derived ToolStripControlHost
to solve your problem.

You're right that a derived ToolStripControlHost only accepts a single
control. You may create a UserControl, which consists of a ComboBox and a
Button, and then add the user control to the derived ToolStripControlHost.

The following is a sample. It requires that you have created a UserControl
named UserControl1, which contains a ComboBox and a Button.

Public Class ToolStripComposite
Inherits ToolStripControlHost

Public Sub New()
MyBase.New(New UserControl1())
End Sub

Public ReadOnly Property InnerControl() As UserControl1
Get
Return CType(Control, UserControl1)
End Get
End Property

End Class


The ToolStripControlHost is derived from ToolStripItem, which provides the
MouseEnter and MouseLeave events, so the derived ToolStripControlHost has
the two events as well.

Hope this helps.
If you have any question, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Thanks Linda - that's perfect!

By setting the following properties on the UserControl, it sits just right
in the ToolStrip too:
AutoSize = true
AutoSizeMode = GrowAndShrink
BackColor=Transparent
Margin-All = 0 for combobox and button

How would I go about getting the ToolStripComposite control to be available
at design time and in the toolbox? Not sure whether this would be possible as
the ToolStripControlHost has no empty constructor so cannot be viewed in the
designer.

Out of interest, is there an (easy) way to get the ComboBox to render the
same way as the ToolStripComboBox?

thanks again for your quick reply

Richard
 
L

Linda Liu [MSFT]

Hi Richard,

Thank you for your feedback.

The ToolStripItem does not appear in the Toolbox by default. If you'd like
the derived ToolStripControlHost to appear in the Toolbox, you could add
ToolboxItemAttribute to it. The following is a sample.

Imports system.ComponentModel

<ToolboxItemAttribute(True)> _
Public Class ToolStripComposite
Inherits ToolStripControlHost
...
End Class

Note that even after you add the ToolboxItemAttribute and build the
project, the derived ToolStripControlHost won't appear automatically in the
Toolbox. You should add it to the Toolbox manually. To do this, right-click
on the Toolbox and select 'Choose Items..' and then browse to the assembly
that contains the derived ToolStripControlHost.

If you drag&drop the derived ToolStripControlHost onto a ToolStrip control
on a form, you will find that it could not be added to the ToolStrip
control at all. It is because the designer of ToolStrip does not support
such a design-time function.

In fact, a common usage is to add
ToolStripItemDesignerAvailabilityAttribute to the derived
ToolStripControlHost, which makes the custom component available on the Add
ToolStripButton drop down menu of a ToolStrip.

The following is a sample.

Imports System.Windows.Forms.Design

<ToolStripItemDesignerAvailabilityAttribute(ToolStripItemDesignerAvailabilit
y.All)> _
Public Class ToolStripComposite
Inherits ToolStripControlHost
...
End Class
is there an (easy) way to get the ComboBox to render the same way as the
ToolStripComboBox?

I think an easy way is to set the FlatStyle property of the ComboBox within
the UserControl to Flat. This will make the ComboBox appears almost the
same as the ToolStripComboBox.

Hope this helps.
If you have any question, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support
 
G

Guest

thanks Linda!

That answers all of my questions and I've now got a great control I can use
throughout my apps. Have also applied the ToolboxBitmapAttribute using an
image embedded in the control's assembly.

Very impressed! thanks again for your help

Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top