Erratic behavior from ContextMenuStrip - c#

I am getting some erratic behavior from a ContextMenuStip:
private void lstModules_MouseMove(object sender , MouseEventArgs e)
{ mouse = e.Location; }
private void lstModules_MouseDown(object sender , MouseEventArgs e)
{
ListViewItem item = null;
if((hitTest = lstModules.HitTest(mouse)) != null)
item = hitTest.Item;
switch (e.Button)
{
case MouseButtons.Right:
if (item != null)
{
// valid item selection
ShowModuleDetails(item.Name);
lstModules.ContextMenuStrip = mnuContext_Module;
}
else
{
// right-click - no item selection
lblModuleDetails.Text = string.Empty;
lstModules.ContextMenuStrip = mnuContext_Desktop;
}
lstModules.ContextMenuStrip.Show(lstModules , mouse);
break;
case MouseButtons.Left:
if (item != null)
{ ShowModuleDetails(item.Name); }
break;
}
}
private void ShowModuleDetails(string modName)
{
// get module details from dictionary
lblModuleDetails.Text = Modules[modName].Details;
}
The item in the list view is not properly selected when the context menu is showing. In other words, when the item is selected, a detail string value is displayed in a label control.
If a context menu is visible, and an item is selected, the item details do not change.
Context menu location briefly appears at the old mouse location then moves to the new mouse location.
Is there something I'm doing wrong with the context menus?

I tried to reproduce your problem as far as I could. I think I can help you out with at least two of the three issues you've listed.
1. The item in the list view is not always properly selected. In other words, when the item is selected, a detail string value is displayed in a label control.
You can be notified when an item has been selected via the ListView.ItemSelectionChanged event:
//
// this handler's only responsibility is updating the item info label:
//
void lstModules_ItemSelectionChanged(object sender,
ListViewItemSelectionChangedEventArgs e)
{
if (e.IsSelected)
{
// an item has been selected; update the label, e.g.:
lblModuleDetails.Text = e.Item.Text;
}
else
{
// some item has been de-selected; clear the label:
lblModuleDetails.Text = string.Empty;
}
}
3. Context menu location briefly appears at the old mouse location then moves to the new mouse location.
I believe you try to do too much. Let the framework handle the displaying of the context menu which you have specified via the ListView.ContextMenuStrip property. The effect you experience is caused by your manually calling ContextMenuStrip.Show(...), which results in the displaying of the context menu by the framework, and then you doing the same thing a second time, at another location.
Therefore, try not to call this function; the context menu should still appear.
//
// this handler's only responsibility is setting the correct context menu:
//
void lstModules_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
var hitTest = lstModules.HitTest(e.Location);
if (hitTest != null && hitTest.Item != null)
{
lstModules.ContextMenuStrip = mnuContext_Module;
}
else
{
lstModules.ContextMenuStrip = mnuContext_Desktop;
}
}
}
Btw., if that works, you can also get rid of your lstModules_MouseMove event handler and the mouse location object.

Related

How to get what form control a context menu was over when clicked one of its items

I‘ve one ContextMenuStrip attached to two controls (DataGridView).
In the ToolStripMenuItem click event, currently I’ve used:this.ActiveControl.Name to get the active GridView control name;
This is fine if I first select the GridView cell and than Rt. click on it to invoke the ContextMenu
Case: sometime if GridView control is not a active control and cell is pre-selected, than context menu Item click not worked accordingly.
Is there any way to get the owner name that initiate the context menu Item click event?
Currently, In the ToolStripMenuItem click event, I've manage to get the original caller (i.e. DataGridView) with this code:
private void CopytoolStripMenuItem1_Click(object sender, EventArgs e)
{
var grid = new DataGridView();
switch (this.ActiveControl.Name)
{
case "dGVEL1":
{
grid=dGVEL1;
break;
}
case "dGVEL2":
{
grid=dGVEL2;
break;
}
}
if (grid == null) return;
DataObject data = grid.GetClipboardContent();
Clipboard.SetDataObject(data);
}
Finally I've Resolved the issue..
The complete solution is
private void CopytoolStripMenuItem1_Click(object sender, EventArgs e)
{
ToolStripDropDownItem item = sender as ToolStripDropDownItem;
if (item == null) // Error
return;
ContextMenuStrip strip = item.Owner as ContextMenuStrip;
var grid = strip.SourceControl as DataGridView;
if (grid == null) // Control wasn't a DGV
return;
switch (grid.Name)
{
case "dGVEL1":
{
grid=dGVEL1;
break;
}
case "dGVEL2":
{
grid=dGVEL2;
break;
}
}
if (grid == null) return;
DataObject data = grid.GetClipboardContent();
Clipboard.SetDataObject(data);
}
Is there any way to get the owner name that initiate the context menu Item click event?
I worked off of the solution you found and simplified it down. Hopefully, this can help some future readers who are looking for a more general solution.
It looks like you go through quite a bit to get ahold of the ContextMenuStrip which is already being passed as sender (granted you will still need to cast it). The following is a more general solution to this issue and it's a bit more simple.
private void contextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e) {
//Cast the parent as a context menu strip so we can access 'sourceControl' attribute
ContextMenuStrip strip = (ContextMenuStrip) sender;
var stripParent = strip.SourceControl;
//print the name of the 'parent' (Control that called the context menu)
System.Diagnostics.Debug.WriteLine("Called From: " + stripParent.Name);
}

Right click selection in DataGrid

I have a DataGrid created in XAML in a C# project. I've added a context menu to the rows. Basically when the user clicks directly on the cell it should open the relevant item in the current window, which is implemented on the SelectionChanged event.
However if the user right clicks a row it should show the the context menu without selecting the row, so that the user can select an item in the context menu to open the relevant item in a new window. So they can look at both the already selected item and the new item at once, but as the right click selects the row, the user see the newly selected item in the current window and the new window.
How can I stop the right click action to show the context menu from selecting the cell?
For my solution, I have to overwrite the following two event handlers (i.e., PreviewMouseRightButtonDown and PreviewMouseRightButtonUp). Plus, not sure why data-binding for the ItemsSource does not work, so that I have to bind it manually.
private void ResultDataGrid_PreviewMouseRightButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
if (sender is DataGrid dg)
{
if (this.DataContext is PipelineStepResultViewModel dataContext
&& dataContext.DatagridMenuItems != null)
{
dg.ContextMenu.ItemsSource = dataContext.DatagridMenuItems;
}
}
e.Handled = true;
}
private void ResultDataGrid_PreviewMouseRightButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
if (sender is DataGrid dg && dg.ContextMenu.ItemsSource != null)
{
ResultDataGrid.ContextMenu.IsOpen = true;
}
e.Handled = true;
}

Check which submenu item was clicked in context menu strip

There is a ContextMenuStrip in a grid control.
I have named it as GridContextMenu.
The GridContextMenu is populated with 4 - 5 items using the following code :
gridcontextMenu.Items.Add(new ToolStripMenuItem
{
Name = Plants,
Text = Plants,
Tag = Plants,
Width = 100,
Image = <image source is put here>
});
gridcontextMenu.Items.Add(new ToolStripMenuItem
{
Name = Animals,
Text = Animals,
Tag = Animals,
Width = 100,
Image = <image source is put here>
});
For the animal menu in tool strip, i added submenu in the following way
(gridcontextMenu.Items[1] as ToolStripMenuItem).DropDownItems.Add("Tiger", image_source, new EventHandler(SubmenuItem_Click));
(gridcontextMenu.Items[1] as ToolStripMenuItem).DropDownItems.Add("Lion", image_source, new EventHandler(SubmenuItem_Click));
(gridcontextMenu.Items[1] as ToolStripMenuItem).DropDownItems.Add("Elephant", image_source, new EventHandler(SubmenuItem_Click));
In the SubmenuItem_Click event handler i need to know which animal submenu was clicked.
How to achieve this ?
currently i have the code for event handler in the following way :
private void SubmenuItem_Click(object sender, EventArgs e)
{
}
How to check condition in this event that which animal submenu was selected ?
Kindly share the answer.
You can do something like this:
private void SubmenuItem_Click(object sender, EventArgs e)
{
var clickedMenuItem = sender as MenuItem;
var menuText = clickedMenuItem.Text;
switch(menuText) {
case "Tiger":
break;
case "Lion":
break;
. ...
}
}
As I found that none of the other answers worked here, I went digging and found the proper solution. This may have been applicable only in .NET Framework 4+ but here is what I found to work.
Essentially, the ItemClicked event in the ContextMenuStrip control passes itself as the sender and a ToolStripItemClickedEventArgs object when the event is raised. As you can't obtain the clicked item from the ContextMenuStrip itself, the only way to obtain this information is to interrogate the ToolStripItemClickedEventArgs object and the clicked item resides in there as a ToolStripItem object. This can then be used to extract the name of the option to use in an if/switch statement as appropriate. See below:
To configure the EventHandler:
...
contextMenuStrip1.ItemClicked += OnContextMenuItem_Clicked;
...
To handle the event and retrieve the text of the clicked item:
private void OnContextMenuItem_Clicked(object sender, ToolStripMenuItemClickedEventArgs e)
{
ToolStripItem clickedItem = e.ClickedItem;
string itemName = clickedItem.Text;
...
}
Hopefully this helps someone looking for this answer in future :)
You can use Tag for this purpose in case when your should localize your application.
Moreover Tag is an object so you can put any tapy of data there. For example Enum type.
private void SubmenuItem_Click(object sender, EventArgs e)
{
var clickedMenuItem = sender as MenuItem;
EnumType item = (EnumType)clickedMenuItem.Tag;
switch(item) {
case TigeItem:
break;
case LionItem:
break;
...
}
}
This is a way to retrieve the ToolStripMenuItem's index if you have created the ContextMenuStrip Dynamically. It is really helpful with getting Enum values. My Context menu is dynamically created and filled with the Enum Names. I hope it helps someone. Sorry for the formatting still new to posting.
`private void DynamiallyCreatedContextMenu_Click(object sender, EventArgs e)
{
ToolStripMenuItem item = sender as ToolStripMenuItem;
var parent = (item.Owner as ContextMenuStrip);
for (int i = 0; i < parent.Items.Count; i++)
{
if (item == parent.Items[i])
{
index = i;
break;
}
}
}`
private void SubmenuItem_Click(object sender, EventArgs e)
{
string clickedItemName=e.ClickedItem.Text;
}

Prevent user from deselecting an item in a ListBox?

I've got a ListBox with a bunch of items in it. The user can click an item to edit its contents. How do I prevent the user from deselecting all items? i.e., the user shouldn't be able to have nothing selected.
There is a case missing in your situation, which is when the list is cleared you will reselect an item there is no longer on the list. I solve this by adding an extra check.
var listbox = ((ListBox)sender);
if (listbox.SelectedItem == null)
{
if (e.RemovedItems.Count > 0)
{
object itemToReselect = e.RemovedItems[0];
if (listbox.Items.Contains(itemToReselect))
{
listbox.SelectedItem = itemToReselect;
}
}
}
I then put this inside a behaviour.
I'm not sure if there is a direct way to disable deselecting an Item, but one way which would be transparent to the user is to keep track of the last selected Item, and whenever the SelectionChanged event is raised and the selected index is -1, then reselect the last value.
This Works for Sure to Prevent User from Deselect... Add those 2 Events to your checkedListBox1 and set the Property CheckOnClick to "True" in Design Mode. (MSVS2015)
private void checkedListBox1_SelectedValueChanged(object sender, EventArgs e)
{
checkedListBox1.SetItemChecked(checkedListBox1.SelectedIndex, true);
}
private void checkedListBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
checkedListBox1.SetItemChecked(checkedListBox1.SelectedIndex, true);
}
To disable on or more options in your listbox/dropdown, you can add the "disabled" attribute as shown below. This prevent the user from selection this option, and it gets a gray overlay.
ListItem item = new ListItem(yourvalue, yourkey);
item.Attributes.Add("disabled","disabled");
lb1.Items.Add(item);
One solution, as suggested by amccormack:
private void hostsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if(lstHosts.SelectedItem == null)
{
if(e.RemovedItems.Count > 0)
{
lstHosts.SelectedItem = e.RemovedItems[0];
}

C#: How to detect who is the caller of a context menu's menu item when linked to two different objects?

C#: How to detect who is the caller of a context menu's menu item when linked to two different objects?
I have two labels, lblOn and lblOff. I am linking 'one' contextmenu to both labels to discard having to make two of the same.
How would I go upon finding out which label object called the contextmenu.menuitem? That way the clicked on menuitem knows if it was it it's contextmenu was called by the lblOn label or lblOffline?
Check the SourceControl property of the ContextMenuStrip.
Disregard. After googling a bit more, I found a solution + code example.
private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
{
//Make sure the sender is a ToolStripMenuItem
ToolStripMenuItem myItem = sender as ToolStripMenuItem;
if (myItem != null)
{
//Get the ContextMenuString (owner of the ToolsStripMenuItem)
ContextMenuStrip theStrip = myItem.Owner as ContextMenuStrip;
if (theStrip != null)
{
//The SourceControl is the control that opened the contextmenustrip.
//In my case it could be a linkLabel
LinkLabel linkLabel = theStrip.SourceControl as LinkLabel;
if (linkLabel == null)
MessageBox.Show("Invalid item selected.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
else
{
if (MessageBox.Show(string.Format("Are you sure you want to remove BOL {0} from this Job?", linkLabel.Text), "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
linkLabel.Text = Program.NullValue(linkLabel);
}
}
}
}
}
Source:
http://www.tek-tips.com/viewthread.cfm?qid=1441041&page=8
I know this is a question from many moons ago but I havent really been able to find
a simple answer with code...
I know SLaks kind of pointed it out, but I think others out there need a code sample...
I wanted to know who the called the context menu between either a rich text box or a label.
The reason is I wanted only one context menu and wanted the copy button within it to be
disabled if the caller was the rich text box with nothing selected.
Heres my code:
private void contextMenuStrip1_Opened(object sender, EventArgs e)
{
//get the context menu (it holds the caller)
ContextMenuStrip contextMenu = sender as ContextMenuStrip;
//get the callers name for testing
string controlName = contextMenu.SourceControl.Name;
//test if it is infact me rich text editor making the call.
if (controlName == "text_rchtxt")
{
//if I have nothing selected... I should not be able to copy
if (text_rchtxt.SelectedText == "")
copy_shrtct.Enabled = false;
}
else
{
//if I do have something selected or if its another control making the call, enable copying
copy_shrtct.Enabled = true;
}
}
Use this:
contextMenuStrip1.SourceControl;

Categories

Resources