473,396 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 6314
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.Controls
{
/// <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 StandardTextBoxContextMenu : 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 StandardTextBoxContextMenu() : 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="additionalMenuItems">Menu 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.MenuItemCollection.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.MenuItemCollection.AddRange"/> will
rearrange
/// the standard menu items to follow the new ones you add.</remarks>
public StandardTextBoxContextMenu(MenuItem[] additionalMenuItems)
{
this.MenuItems.AddRange(additionalMenuItems);

InitializeComponent();
}

#region Static constructor

static StandardTextBoxContextMenu()
{
Agama.AgamaRegistry.Reg.AddControlSerialization(ty peof(StandardTextBoxContextMenu),
null, null, false);
}

#endregion

private void InitializeComponent()
{
this.miUndo = new System.Windows.Forms.MenuItem();
this.miSeparator = 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.miSelectAll = new System.Windows.Forms.MenuItem();
//
// miUndo
//
this.miUndo.Text = "&Undo";
this.miUndo.Click += new System.EventHandler(this.miUndo_Click);
//
// miSeparator
//
this.miSeparator.Text = "-";
//
// miCut
//
this.miCut.Text = "Cu&t";
this.miCut.Click += new System.EventHandler(this.miCut_Click);
//
// miCopy
//
this.miCopy.Text = "&Copy";
this.miCopy.Click += new System.EventHandler(this.miCopy_Click);
//
// miPaste
//
this.miPaste.Text = "&Paste";
this.miPaste.Click += new System.EventHandler(this.miPaste_Click);
//
// miDelete
//
this.miDelete.Text = "&Delete";
this.miDelete.Click += new System.EventHandler(this.miDelete_Click);
//
// miSelectAll
//
this.miSelectAll.Text = "Select &All";
this.miSelectAll.Click += new
System.EventHandler(this.miSelectAll_Click);

this.MenuItems.AddRange(
new System.Windows.Forms.MenuItem[] {
this.miUndo,
this.miSeparator,
this.miCut,
this.miCopy,
this.miPaste,
this.miDelete,
this.miSelectAll
});
this.Popup += new EventHandler(StandardTextBoxContextMenu_Popup);
}

private void miUndo_Click(object sender, System.EventArgs e)
{
// Get the text box that the context menu was popped on
if (this.SourceControl is TextBox)
{
TextBox clickedBox = (TextBox)this.SourceControl;

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

private void miCut_Click(object sender, System.EventArgs e)
{
// Get the text box that the context menu was popped on
if (this.SourceControl is TextBox)
{
TextBox clickedBox = (TextBox)this.SourceControl;

if (clickedBox.SelectionLength > 0)
{
clickedBox.Cut();
}
}
}

private void miCopy_Click(object sender, System.EventArgs e)
{
// Get the text box that the context menu was popped on
if (this.SourceControl is TextBox)
{
TextBox clickedBox = (TextBox)this.SourceControl;

if (clickedBox.SelectionLength > 0)
{
clickedBox.Copy();
}
}
}

private void miPaste_Click(object sender, System.EventArgs e)
{
// Get the text box that the context menu was popped on
if (this.SourceControl is TextBox)
{
TextBox clickedBox = (TextBox)this.SourceControl;

if (clickedBox.SelectionLength > 0)
{
clickedBox.Paste();
}
}
}

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

private void miDelete_Click(object sender, System.EventArgs e)
{
// Get the text box that the context menu was popped on
if (this.SourceControl is TextBox)
{
TextBox clickedBox = (TextBox)this.SourceControl;

if (clickedBox.SelectionLength > 0)
{
SendMessage(clickedBox.Handle, WM_CLEAR, 0, 0);
}
}
}

private void miSelectAll_Click(object sender, System.EventArgs e)
{
// Get the text box that the context menu was popped on
if (this.SourceControl is TextBox)
{
TextBox clickedBox = (TextBox)this.SourceControl;
clickedBox.SelectAll();
}
}

private void StandardTextBoxContextMenu_Popup(object sender,
EventArgs e)
{
// Get the text box that the context menu was popped on
if (this.SourceControl is TextBox)
{
TextBox clickedBox = (TextBox)this.SourceControl;

// Enable and disable standard menu items as necessary
bool isSelection = clickedBox.SelectionLength > 0;
IDataObject clipObject = Clipboard.GetDataObject();
bool textOnClipboard = clipObject.GetDataPresent(DataFormats.Text);

this.miUndo.Enabled = clickedBox.CanUndo;
this.miCut.Enabled = isSelection;
this.miCopy.Enabled = isSelection;
this.miPaste.Enabled = textOnClipboard;
this.miDelete.Enabled = isSelection;
}
}
}
}

Jan 16 '06 #2

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

Similar topics

4
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...
1
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. ...
2
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...
5
by: Dean L. Howen | last post by:
Dear friends, Could we determine when context menu should appear?
8
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...
2
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,...
2
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...
0
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...
0
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....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.