I am sure you know how to add the predefined TextBox or ComboBox controls to a Windows Forms context Menu:

Just wanted to quickly show you how you can add your own custom controls to that context menu by using the ToolStripControlHost class, like this (the yellow panel is a user control):

Here is a short video:

And here is the code:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            //instance my control
            UserControl1 c = new UserControl1();

            //pass as parameter to ToolStripControlHost instance
            ToolStripControlHost host = new ToolStripControlHost(c);

            //add ToolStripControlHost object to context menu. simple!
            contextMenuStrip1.Items.Add(host);
        }
    }
}


 

kick it on DotNetKicks.com AddThis Social Bookmark Button

Monday, January 19, 2009 5:41:23 PM UTC
Heh :D I have searched something like that few days ago but I didn't find by googling and had to "waste time" ;) for my own experiments and tries. I think this post will be useful for many people, thanks anyway :)

I like your youtube presentations :) Your nice blog is already in my feeds :)

Greetz!
Marek
Monday, January 19, 2009 5:42:41 PM UTC
eh, btw: to other readers ;) Same method you can use for popular StatusStrip component.
Monday, January 19, 2009 8:15:28 PM UTC
Thanks for your comments Marek!
I think I have to lower down the screen resolution before. It looks kind of blurry. I will try that in the next post :)
Friday, May 15, 2009 8:29:07 PM UTC
cool ! thnx for the code
Jarno Burger
Monday, February 08, 2010 7:46:22 PM UTC
Very Very good and usefull code
Your are great, man !
A..
All comments require the approval of the site owner before being displayed.
Name
E-mail
(will show your gravatar icon)
Home page

Comment (Some html is allowed: a@href@title, b, em, i, strike, strong) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview

Yeap, the sponsors! :-)

On this page...

Tags

Calendar

<March 2010>
SunMonTueWedThuFriSat
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910

Navigation

Archives

Recommended .NET blogs

The author

Iván Loire
Iván Loire (Zaragoza, Spain)

View Ivan Loire's profile on LinkedIn
Microsoft Certified Trainer
Send mail to the author(s) E-mail

Syndication

Feed Icon