I have a context menu for notifyIcon, in which for one of the menu items I want to provide a submenu with three options that can be checked/unchecked. But I want this submenu to appear only when the user hovers/clicks over the arrow in the parent menu item. I.e., the user should be able to select the parent menu item without having to open its submenu, and the default values of the options in the submenu will be used in the parent menu's handler. Is this possible?
I am using Winforms with .Net framework 4.8.1 -
MenuItem[] submenu = new MenuItem[]
{
new MenuItem("Italic",OpenItalic){ DefaultItem = true},
new MenuItem("Bold",OpenBold),
new MenuItem("Underline", OpenUnderline)
};
MenuItem[] menuItems = {
new MenuItem("Show text", submenu)
};
icon = new NotifyIcon()
{
ContextMenu = new ContextMenu(menuItems),
Icon = new System.Drawing.Icon("instance.ico"),
Visible = true,
Text = "test"
};
Here user has to select from the submenu items bold/italic/underline each time to open a form. Can we keep 'italic' as default option and allow the user to open the form by clicking 'Show text' option itself, and only open the submenu when there is a need to change the option?
Related
I need to show a menu on an NSTableView when user right clicks anywhere on the table row. Just like when we right click anywhere in the browser and menu shows up.
NSTableView has Menu property that can be assigned to your NSMenu.
Of course all this can be done in Xcode's IB, but if you are doing this programmatically you need to construct a NSMenu, add the NSMenuItem(s) required and assign it to the NSTableView.
var menu = new NSMenu("A context menu")
{
Delegate = this
};
menu.AddItem(new NSMenuItem { Title = "StackOverflow" Action = SomeMenuAction });
aTableView.Menu = menu;
As i see in janus there is UIButton that has an arrow on it , so when you click on it , it can open a menu.
I dont know how to add items so when you click on that Arrow, that is on UIButton, the menu will be open.
Can someone explain me please?
You will need to set the ButtonStyle to DropDown. It will now accept a new property called DropDownContextMenu, which will allow you to attach a context menu to the UIButton dropdown. Here's a quick example setting it in code:
public Form1()
{
InitializeComponent();
uiButton1.ButtonStyle = Janus.Windows.EditControls.ButtonStyle.DropDown;
var menu = new Janus.Windows.UI.CommandBars.UIContextMenu();
menu.Commands.Add(new Janus.Windows.UI.CommandBars.UICommand("1", "Item1"));
menu.Commands.Add(new Janus.Windows.UI.CommandBars.UICommand("2", "Item2"));
uiButton1.DropDownContextMenu = menu;
}
I'm unable to get the keyboard shortcuts working on context menu items belonging to specific controls and I was wondering if anyone could help me.
Use case: I'm trying to write an attached behavior that can be applied to various elements in order to provide undo/redo context menu functionalists to those controls. The context menu part is working but the key bindings don't seem to work.
What I've tried so far:
Attempt 1:
var menu = new ContextMenu();
var undoCommand = new DelegateCommand(Undo);
var undoMenuItem = new MenuItem
{
Header = "Undo",
Command = undoCommand,
InputGestureText = "Ctrl+Z",
};
menu.Items.Add(undoMenuItem);
AssociatedObject.ContextMenu = menu; // AssociatedObject is a textbox as an example
Attempt 2:
var menu = new ContextMenu();
var undoCommand = new DelegateCommand(Undo);
var undoMenuItem = new MenuItem
{
Header = "Undo",
Command = undoCommand,
};
undoMenuItem.InputBindings.Add(new InputBinding(undoCommand,
new KeyGesture(Key.Z, ModifierKeys.Control)))
menu.Items.Add(undoMenuItem);
AssociatedObject.ContextMenu = menu; // AssociatedObject is a textbox as an example
Neither of these seem to be working. The context menu is selectible from the UI (right click on a text box for instance and select "Undo") but the keyboard shortcut is not fired.
Is there a way to do this in an attached behavior? I would like to avoid having access to the underlying Window element inside my attached behavior if possible and would like the keys to work within their respective bounds (for instance, only if the text box is focused should Ctrl+Z cause an Undo on that text box).
Many thanks
I have a ContextMenuStrip in my c# win application that include some submenu.
How i can display a existing submenu(from above ContextMenuStrip) on a button when this clicked?
Takle munu first and add menu items into it e.g delete or resize and after that take e.g take resize menu item and add items into it like submenu1 and submenu2
menu = new ContextMenu();
mnudel = new MenuItem("Delete");
mnuresize = new MenuItem("Resize");
menu.MenuItems.AddRange(new MenuItem[] { mnudel, mnuresize });
submenu1 = new MenuItem("Sub1");
submenu2 = new MenuItem("Sub2");
mnuresize.MenuItems.AddRange(new MenuItem[] { submenu1, submenu2 })
I am using a context menu for my gridcontrol.
theGrid.ContextMenuStrip = contextMenuStrip1;
This has add, delete, edit as the choices.
How can I create a menu class and use for the choices in the contextmenustrip.
Can anyone show me some sample code to do this.
Thanks
Sun
MSDN has information on how to add items to a contextmenustrip
Sample Code:
// Declare the ContextMenuStrip control.
private ContextMenuStrip fruitContextMenuStrip;
public Form3()
{
// Create a new ContextMenuStrip control.
fruitContextMenuStrip = new ContextMenuStrip();
// Attach an event handler for the
// ContextMenuStrip control's Opening event.
fruitContextMenuStrip.Opening += new System.ComponentModel.CancelEventHandler(cms_Opening);
// Create a new ToolStrip control.
ToolStrip ts = new ToolStrip();
// Create a ToolStripDropDownButton control and add it
// to the ToolStrip control's Items collections.
ToolStripDropDownButton fruitToolStripDropDownButton = new ToolStripDropDownButton("Fruit", null, null, "Fruit");
ts.Items.Add(fruitToolStripDropDownButton);
// Dock the ToolStrip control to the top of the form.
ts.Dock = DockStyle.Top;
// Assign the ContextMenuStrip control as the
// ToolStripDropDownButton control's DropDown menu.
fruitToolStripDropDownButton.DropDown = fruitContextMenuStrip;
// Create a new MenuStrip control and add a ToolStripMenuItem.
MenuStrip ms = new MenuStrip();
ToolStripMenuItem fruitToolStripMenuItem = new ToolStripMenuItem("Fruit", null, null, "Fruit");
ms.Items.Add(fruitToolStripMenuItem);
// Dock the MenuStrip control to the top of the form.
ms.Dock = DockStyle.Top;
// Assign the MenuStrip control as the
// ToolStripMenuItem's DropDown menu.
fruitToolStripMenuItem.DropDown = fruitContextMenuStrip;
// Assign the ContextMenuStrip to the form's
// ContextMenuStrip property.
this.ContextMenuStrip = fruitContextMenuStrip;
// Add the ToolStrip control to the Controls collection.
this.Controls.Add(ts);
//Add a button to the form and assign its ContextMenuStrip.
Button b = new Button();
b.Location = new System.Drawing.Point(60, 60);
this.Controls.Add(b);
b.ContextMenuStrip = fruitContextMenuStrip;
// Add the MenuStrip control last.
// This is important for correct placement in the z-order.
this.Controls.Add(ms);
}
Or another link to ContextMenuStrip in C#