PC Review


Reply
Thread Tools Rate Thread

Building a Custom ToolStrip Control

 
 
=?Utf-8?B?UmljaGFyZCBCeXNvdXRo?=
Guest
Posts: n/a
 
      13th Feb 2007
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

-----------
Please provide code examples in VB if possible...thanks!
-----------
 
Reply With Quote
 
 
 
 
Linda Liu [MSFT]
Guest
Posts: n/a
 
      13th Feb 2007
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/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

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

 
Reply With Quote
 
=?Utf-8?B?UmljaGFyZCBCeXNvdXRo?=
Guest
Posts: n/a
 
      14th Feb 2007
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

 
Reply With Quote
 
Linda Liu [MSFT]
Guest
Posts: n/a
 
      14th Feb 2007
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


 
Reply With Quote
 
=?Utf-8?B?UmljaGFyZCBCeXNvdXRo?=
Guest
Posts: n/a
 
      15th Feb 2007
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
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
building a custom calender control Manoli67 Microsoft Access Form Coding 4 29th Jan 2010 03:01 PM
Custom Toolstrip =?Utf-8?B?SmVmZiBTdWRkZXRo?= Microsoft Dot NET Framework Forms 1 25th Jul 2007 03:36 PM
Custom ToolStrip item in Designer MLM450@hotmail.com Microsoft C# .NET 0 20th Sep 2006 12:21 PM
Problem with Custom Control in Building it when the same is usedin a new windows project solution Kiran Microsoft Dot NET Framework Forms 1 11th Feb 2006 12:46 PM
error CS0006-while building design-time version of custom control =?Utf-8?B?SGFyaQ==?= Microsoft Dot NET Compact Framework 1 4th Dec 2004 01:27 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:47 AM.