Explicitly Prevent ContextMenuStrip from Loading in C# - c#

I have a requirement to hide the contextmenustrip when a particular flag is not set. As i don't think we can explicitly control the show/hide of the context menu strip, i decided to trap the right mouse button click on the control with which the contextmenustrip is associated. It is a UserControl, so i tried handling it's MouseClick event inside which i check if the flag is set and if the button is a right button. However to my amazement, the event doesn't get fired upon Mouse Right Click, but fires only for Left Click.
Is there any thing wrong with me or is there any workaround?
RIGHT CLICK IS GETTING DETECTED, Question TITLE and Description Changed
After Doing some more research, i got the rightclick to fire, when i Handled the Mouse_Down Event on the Control. However Am still clueless, as how to explicitly prevent the ContextMenuStrip From Loading. Another question being, why MouseClick didn't detect Right Button Click?
Current WorkAround
Registering the event handler
userControl1.Control1.MouseDown += new MouseEventHandler(Control1_MouseDown);
void Control1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right && flag == false)
{
userControl1.Control1.ContextMenuStrip = null;
}
else
{
userControl1.Control1.ContextMenuStrip = contextMenuStrip1;
}
}
this is the current workaround i'm doing.
But how can i change it in the Opening Event of the ContextMenuStrip

Your solution will fail anyway when the context menu is invoked with the context menu key (or what it's called) on the keyboard. You can use the Opening event to cancel the opening of a context menu.

There is a work around.
Lets say Menu Item A sets the flag that controls the context menu on control B.
In the click event for A, you set b.ContextMenu = nothing to switch it off, and set b.ContextMenu back to the context menu control to switch it back on.

In WinForms there's also the Click event - which does get fired for a right mouse click.
If you are using WPF you should have MouseRightButtonDown and MouseRightButtonUp events.
Check out the table on this MSDN page which lists which controls raise which click events. Significantly, Button, CheckBox, RichTextBox and RadioButton (and a few others) don't raise and event on right click.

Related

How to know which Image the MouseClick event occured?

I have 2 images for 2 players on my windows form. I have added a MouseClick event for both of them. Now when I mouse click, it activates both the events for both the players. I wanted to know only one player where I am clicking.
How do I do that?
this.MouseClick += new System.Windows.Forms.MouseEventHandler(this._player1_MouseClick);
this.MouseClick += new System.Windows.Forms.MouseEventHandler(this._player2_MouseClick);
Assume you have two PictureBox controls with images. Thus you have posted code from designer, I also assume that you are subscribing to events via designer.
Select one of PictureBox controls
In Events tab find Click property, type-in Player_Click as handler name and hit Enter. You have subscribed to Click event of first control.
Select second PictureBox
In Events tab find Click property, click on drop-down and select Player_Click as well. Hit Enter. You have used same event handler to subscribe to Click event of second control.
Now go to code view (you can double click any of those controls for that) and find Player_Click method
In order to find which control was clicked you need to cast sender argument to PictureBox type:
private void Player_Click(object sender, EventArgs e)
{
var pictureBox = (PictureBox)sender;
// use control which was clicked. e.g. get it's name
var name = pictureBox.Name;
}

Prevent Click events on child view while swiping(flicking) parent. WinPhone8.1

I have canvas with listbox inside it.
each child element of listbox sets eventhandler for Click event.
On canvas I set eventhandlers for
ManipulationStarted="canvas_ManipulationStarted"
ManipulationDelta="canvas_ManipulationDelta"
ManipulationCompleted="canvas_ManipulationCompleted"
My code for swiping works perfect accept one thing, it fires Click eventhandler before ManipulationCompleted eventhandler.
But for example listbox in the same time scrolls perfectly and do not fire Click event.
So basically what I need is to handle manipulation events in same way listbox do.
If this condition is true:
private void canvas_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
e.DeltaManipulation.Translation.X > [some value]
....
}
I need to disable firing Click event on any child element of canvas, doesn't matter if it is inside listbox or not.
Why are you setting click handlers if you don't want them to fire?
Click fires on pointer pressed, so there's no way to tell if the user wanted to Click or to start a manipulation. You'll need to either decide based on the location clicked or a later event if you want to differentiate between "clicks" and swipes.
Instead of Click you can handle the Tap gesture along with the manipulation events. Since Tap fires on the pointer released the manipulation system will fire it if the user tap and releases in one spot and it will trigger the manipulations if the user presses and moves the pointer.
See How to handle manipulation events for Windows Phone 8 for more details.

Prevent mouse events during wpf Window.OnActivate

Is it possible to prevent mouse events when a window is activated?
For example, I have a C# window and I change the focus to something else such as a browser. When I reactivate the c# window by clicking on it, I don't want any mouse events to be executed. The first click on the window should just activate it. Mouse Events are only fired if the window is already activated.
An event will be fired then a user clicks on your wpf controll but why don't you just handle the event?
Subscribe to the event and prevent it to bubble up the visual tree:
private void Panel_KeyDown(object sender, KeyEventArgs e)
{
e.Handled = true;
}
Have a look at this link it describes the event bubble and tunneling in WPF.

How do I make somthing visible when the mouse moves across a button?

How do I show a PropertyGrid instead of a context menu, so when the user clicks somewhere else, it will hide (like a context menu)?
You could wire the loss of focus to this action. It's not perfect (not all clicks outside the control will be captured), but it will work every time another control grabs focus.
propertyGrid1.Leave += (object sender, EventArgs e) => { propertyGrid1.Hide(); };
If this is not good enough, you could try wiring the action to other events, such as the form's Click event.

How to change Event properties & create new events in C# .NET

I'm using NotifyIcon (System tray icon) in my WinForms application. I also have a ContextMenuStrip assigned to it. When user right clicks on NotifyIcon this ContextMenuStrip pops up.
These are the only events that NotifyIcon has.
Click
DoubleClick
MouseClick
MouseDoubleClick
MouseDown
MouseMove
MouseUp
This contextMenuStrip items (ToolStripMenuItem) are dynamically generated. I mean there are few default items like 'About','Exit','Help' etc.. but other items are dynamically generated and inserted into this menu when user right clicks on it. I'm generating items and inserting into contextMenuStrip in Click event handler
Now, I've two problems:
Problem is for an instant its showing the default menustrip and then my Click event handler executes and new update menu pops up. How can I avoid this? I don't want to see the default menu at all. In other words I need to override the default behavior.
Other problem is since I'm handling the Click event (because I didn't find RightClick event) the left button click also is handled by the same handler. I want to do different things (like show application windows) on left click and show dynamically generated contextMenuStrip on right click. How to acheive this?
Why are there two different events like Click & MouseClick? What else would we click with? Aren't these two interdependent. I mean when ever there is a MouseClick there is also a Click.
If you can point me to some examples. That would be great!
Item #1) The ContextMenuStrip has events that allows you to handle any dynamic creation of menu items before the menu is displayed. See the Opening and Opened events.
Item #2) Use the MouseEventArgs parameter to inspect the mouse-state when event was raised.
Item #3) Depending on the control, Click and MouseClick can be different. Take buttons for instance. When a button has focus, the "Click" event is raised when the user presses the Space or Enter key. In most cases, a MouseClick generates a Click event.
I'm the OP. I guess I've achieved it.
notifyIcon.MouseDown += new MouseEventHandler(notifyIcon_MouseDown);
and
static void notifyIcon_MouseDown(object sender, MouseEventArgs e)
{
NotifyIcon notifyIcon = sender as NotifyIcon;
if (e.Button == MouseButtons.Left)
{
MessageBox.Show("Left Button Clicked"); // & do what ever you want
}
else
{
updateMenuItems(notifyIcon.ContextMenuStrip);
}
}

Categories

Resources