Show Control in DropDown of ToolStripMenuItem

G

Guest

I need to show a custom control in the DropDown of a
Windows.Forms.ToolStripMenuItem (e.g., similar to the Font Color menu item in
Word except that the control is specific to my application). I tried adding
the control to Windows.Forms.ToolStripMenuItem.DropDown.Controls but doing so
raises a NotSupported exception. Are there any standard ToolStripItems
and/or techniques for implementing this type of functionality?

Thanks for any help!
Lance
 
J

Jeffrey Tan[MSFT]

Hi Lance,

Based on my understand, you want to showing a customized control in the
ToolStripMenuItem DropDown property.

To get this done, you should not use ToolStripMenuItem.DropDown.Controls
property. The correct solution is using ToolStripControlHost to host your
customize control and add ToolStripControlHost to ToolStripDropDown.Items
collection. I created a little sample application demonstrating the usage:

private void Form1_Load(object sender, EventArgs e)
{
TreeView treeview = new TreeView();
TreeNode treeNode1=new TreeNode();
treeview.Name = "treeview";
treeNode1.Name = "Node1";
treeNode1.Text = "Node1";
treeview.Nodes.AddRange(new System.Windows.Forms.TreeNode[] {treeNode1
});

treeview.BorderStyle = BorderStyle.None;
ToolStripControlHost treeViewHost = new ToolStripControlHost(treeview);
ToolStripDropDown dropDown = new ToolStripDropDown();
dropDown.Items.Add(treeViewHost);

this.toolStripMenuItem1.DropDown = dropDown;
}
Note: I created a TreeView control and hosted it in ToolStripControlHost,
finally, I added it into ToolStripDropDown.Items collection.

The articles below contains more information:
"Writing custom popup controls"
http://blogs.msdn.com/jfoscoding/archive/2005/09/18/471048.aspx
"Adding a custom control to a ToolStripDropDownButton"
http://www.codeproject.com/cs/menu/ToolStripDropDown.asp

Hope this helps.

Best regards,
Jeffrey Tan
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.
 
J

Jeffrey Tan[MSFT]

Hi Lance,

Oh, sorry, it seems that I pasted the C# code by mistake. Below is the
VB.net version:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim treeview As New TreeView()
Dim treeNode1 As New TreeNode()
treeview.Name = "treeview"
treeNode1.Name = "Node1"
treeNode1.Text = "Node1"
treeview.Nodes.AddRange(New System.Windows.Forms.TreeNode()
{treeNode1})

treeview.BorderStyle = BorderStyle.None
Dim treeViewHost As New ToolStripControlHost(treeview)
Dim dropDown As New ToolStripDropDown()
dropDown.Items.Add(treeViewHost)

Me.ToolStripMenuItem1.DropDown = dropDown
End Sub

Thanks.

Best regards,
Jeffrey Tan
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

Hi Jeffrey,

That's great. Thanks a lot! I misunderstood the purpose of the
ToolStripControlHost so your sample was very helpful.

Thanks again,
Lance
 

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