473,595 Members | 2,462 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

customize the context menu

Hi,
I'm looking of a way (preferred - a ready class or dll) to customize
the context menu.
many application has more controls inside the context menu (like
textbox, sliders, checkbox, panel etc').
is there a way making this without writing my own context menu
(meaning, popup form or something) ?

Please don't post commercial products to this topic
thanks.

Jan 15 '06 #1
1 6328
Not in VS2003. You have to make your own context menu, which isn't as
easy as it sounds.

Here is my version:

using System;
using System.Runtime. InteropServices ;
using System.Windows. Forms;

namespace MyCompany.Contr ols
{
/// <summary>
/// A home-grown version of the standard right-click context menu,
/// so that applications can extend this menu and add their own
/// context items.
/// </summary>
public class StandardTextBox ContextMenu : ContextMenu
{
private System.Windows. Forms.MenuItem miUndo;
private System.Windows. Forms.MenuItem miCut;
private System.Windows. Forms.MenuItem miCopy;
private System.Windows. Forms.MenuItem miPaste;
private System.Windows. Forms.MenuItem miDelete;
private System.Windows. Forms.MenuItem miSelectAll;
private System.Windows. Forms.MenuItem miSeparator;

/// <summary>
/// Creates a standard context menu for a text box, containing
/// Undo, Cut, Copy, Paste... all of the usual context menu
/// items.
/// </summary>
public StandardTextBox ContextMenu() : this(new MenuItem[0])
{ }

/// <summary>
/// Creates a standard context menu for a text box, containing
/// Undo, Cut, Copy, Paste... all of the usual context menu
/// items, with additional menu items supplied by the caller
/// that will precede the standard items in the context menu.
/// </summary>
/// <param name="additiona lMenuItems">Men u items that should
/// appear above the standard menu items.</param>
/// <remarks>You can get the same effect as calling this
/// constructor by calling the no parameter constructor
/// and then using <see cref="Menu.Menu ItemCollection. AddRange"/>
/// to add menu items later. Just set the <see
cref="MenuItem. Index"/>
/// property of the menu items to start numbering from
/// 0, and <see cref="Menu.Menu ItemCollection. AddRange"/> will
rearrange
/// the standard menu items to follow the new ones you add.</remarks>
public StandardTextBox ContextMenu(Men uItem[] additionalMenuI tems)
{
this.MenuItems. AddRange(additi onalMenuItems);

InitializeCompo nent();
}

#region Static constructor

static StandardTextBox ContextMenu()
{
Agama.AgamaRegi stry.Reg.AddCon trolSerializati on(typeof(Stand ardTextBoxConte xtMenu),
null, null, false);
}

#endregion

private void InitializeCompo nent()
{
this.miUndo = new System.Windows. Forms.MenuItem( );
this.miSeparato r = new System.Windows. Forms.MenuItem( );
this.miCut = new System.Windows. Forms.MenuItem( );
this.miCopy = new System.Windows. Forms.MenuItem( );
this.miPaste = new System.Windows. Forms.MenuItem( );
this.miDelete = new System.Windows. Forms.MenuItem( );
this.miSelectAl l = new System.Windows. Forms.MenuItem( );
//
// miUndo
//
this.miUndo.Tex t = "&Undo";
this.miUndo.Cli ck += new System.EventHan dler(this.miUnd o_Click);
//
// miSeparator
//
this.miSeparato r.Text = "-";
//
// miCut
//
this.miCut.Text = "Cu&t";
this.miCut.Clic k += new System.EventHan dler(this.miCut _Click);
//
// miCopy
//
this.miCopy.Tex t = "&Copy";
this.miCopy.Cli ck += new System.EventHan dler(this.miCop y_Click);
//
// miPaste
//
this.miPaste.Te xt = "&Paste";
this.miPaste.Cl ick += new System.EventHan dler(this.miPas te_Click);
//
// miDelete
//
this.miDelete.T ext = "&Delete";
this.miDelete.C lick += new System.EventHan dler(this.miDel ete_Click);
//
// miSelectAll
//
this.miSelectAl l.Text = "Select &All";
this.miSelectAl l.Click += new
System.EventHan dler(this.miSel ectAll_Click);

this.MenuItems. AddRange(
new System.Windows. Forms.MenuItem[] {
this.miUndo,
this.miSeparato r,
this.miCut,
this.miCopy,
this.miPaste,
this.miDelete,
this.miSelectAl l
});
this.Popup += new EventHandler(St andardTextBoxCo ntextMenu_Popup );
}

private void miUndo_Click(ob ject sender, System.EventArg s e)
{
// Get the text box that the context menu was popped on
if (this.SourceCon trol is TextBox)
{
TextBox clickedBox = (TextBox)this.S ourceControl;

if (clickedBox.Can Undo)
{
clickedBox.Undo ();
}
}
}

private void miCut_Click(obj ect sender, System.EventArg s e)
{
// Get the text box that the context menu was popped on
if (this.SourceCon trol is TextBox)
{
TextBox clickedBox = (TextBox)this.S ourceControl;

if (clickedBox.Sel ectionLength > 0)
{
clickedBox.Cut( );
}
}
}

private void miCopy_Click(ob ject sender, System.EventArg s e)
{
// Get the text box that the context menu was popped on
if (this.SourceCon trol is TextBox)
{
TextBox clickedBox = (TextBox)this.S ourceControl;

if (clickedBox.Sel ectionLength > 0)
{
clickedBox.Copy ();
}
}
}

private void miPaste_Click(o bject sender, System.EventArg s e)
{
// Get the text box that the context menu was popped on
if (this.SourceCon trol is TextBox)
{
TextBox clickedBox = (TextBox)this.S ourceControl;

if (clickedBox.Sel ectionLength > 0)
{
clickedBox.Past e();
}
}
}

[DllImport("user 32.dll", CharSet = CharSet.Auto)]
private static extern int SendMessage(Sys tem.IntPtr hWnd, int msg,
int lParam, int wParam);
private const int WM_CLEAR = 0x0303;

private void miDelete_Click( object sender, System.EventArg s e)
{
// Get the text box that the context menu was popped on
if (this.SourceCon trol is TextBox)
{
TextBox clickedBox = (TextBox)this.S ourceControl;

if (clickedBox.Sel ectionLength > 0)
{
SendMessage(cli ckedBox.Handle, WM_CLEAR, 0, 0);
}
}
}

private void miSelectAll_Cli ck(object sender, System.EventArg s e)
{
// Get the text box that the context menu was popped on
if (this.SourceCon trol is TextBox)
{
TextBox clickedBox = (TextBox)this.S ourceControl;
clickedBox.Sele ctAll();
}
}

private void StandardTextBox ContextMenu_Pop up(object sender,
EventArgs e)
{
// Get the text box that the context menu was popped on
if (this.SourceCon trol is TextBox)
{
TextBox clickedBox = (TextBox)this.S ourceControl;

// Enable and disable standard menu items as necessary
bool isSelection = clickedBox.Sele ctionLength > 0;
IDataObject clipObject = Clipboard.GetDa taObject();
bool textOnClipboard = clipObject.GetD ataPresent(Data Formats.Text);

this.miUndo.Ena bled = clickedBox.CanU ndo;
this.miCut.Enab led = isSelection;
this.miCopy.Ena bled = isSelection;
this.miPaste.En abled = textOnClipboard ;
this.miDelete.E nabled = isSelection;
}
}
}
}

Jan 16 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3034
by: Mohit Gupta | last post by:
Hi all, Lately I have been working on an application in VB .net CF for Pocket PC device. I have a small question about Context Menu. When I try to close the window after context menu is poped up, the window does not closes until I click on the window. Below is the code. Hopefully, you can help me out.
1
5892
by: deko | last post by:
I've found that the "Remove Filter/Sort" selection in the Shortcut menu (displayed on right click) produces ugly, untrappable errors, even though "Allow Filters" is set to No in the subform. However, the "Sort Ascending" and "Sort Descending" selections provide a helpful feature. Is there a way to get that "Remove Filter/Sort" out of the Shortcut menu? I've used this in some cases, but then I lose the Sort option:
2
3790
by: JMCN | last post by:
hi i'm having a problem with the customize a shortcut menu. i read the ms office assistance: customize a shortcut menu/delete a command or add to a shortcut menu and followed the simple directions. i tried both but for some reason i cannot delete the categories called "filter", "form", etc. eventually i would like to add commands to the "custom" category, ie commands "filter by form, "filter for:" and "filter by excluding" options and...
5
5482
by: Dean L. Howen | last post by:
Dear friends, Could we determine when context menu should appear?
8
10083
by: Dennis C. Drumm | last post by:
Is there a way to modify the standard context menu shown when someone right clicks in a windows text box and that would work for all open windows applications? The standard context menu for a text box has 6 items, undo, cut, copy, paste, delete and select all. I would like to add one additional paste menu that opens a new sub menu with several optional text items that could be pasted. The items would be populated by my program but...
2
4262
by: | last post by:
Hello, I'm trying to add a "Send to Notification Area" item to the context menu for my application's taskbar button. (For example, right-click a CHM file (i.e., SQL Server BOL) in the taskbar, and it has three extra menu items...Separater, Jump to URL, and Version.) The only thing I found that might be what I'm looking for is in C# (below)...having a hard time translating. How do I do this in VB.NET?
2
5898
by: MCM | last post by:
I'm working on a plotting control. The plotting control will have a context menu with basic commands for "scaling", "zooming", etc. Is there a way that, from the parent form, I can add more commands to the control's context menu? I'm envisioning a case where the control has a set of context menu items, and the parent form also has a set of context menu items. Thanks.
0
1513
by: temp304 | last post by:
Customize Dropdown Menu is designed to display some choices, as: languages, countries, themes,... but it's differrent from original version in browsers. This tool combine CSS and JavaScript to create a dropdown you can add image to tilte, every choice. Version 2: check if JavaScript is disabled, then add normal dropdown menu Let's try dropdown below and feel differences. Tested: IE 7, Opera 9, Firefox 2, Netscape 8. Demo:...
0
2206
by: Sky | last post by:
I have an Access 2003 front-end database with custom toolbars. The toolbars work fine. One annoying feature is that at the far right edge of each custom toolbar there a small dropdown arrow. When you hover over it with the mouse, a ControlTip Text of "Toolbar Options" is displayed. If you then click on the dropdrown arrow, it displays a popup menu of "Add or Remove Buttons", with a fly-out menu with the toolbar name at the top and...
0
7957
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7883
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8262
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
6675
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5839
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5421
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3875
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3915
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1491
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.