I need to write event handlers for tracking mouse movements, but the mouse-down and mouse-up events are not related to any Control object. What is the generic way to do this? On researching this question on StackOverflow, I found a lot of information about mouse events related to specific controls, such as the PictureBox, but I don't find any on C# generic mouse listeners. I did find a much older question, but it wasn't related to C#.
Related
This is for WinForms.
I am having a strange problem that is driving me absolutely batty. I have a tab control for which standard system events, and only standard system events, are not firing. The specific event I was trying to get to fire was the TabIndexChanged event. It doesn't matter whether I add this programmatically or with the designer.
Note: Mouse events will fire. Keypress events will fire. All other events that I have tried will fire.
System events on OTHER controls will fire.
It isn't a single tab control that is having a problem either. If I drag a new tab control onto the form it will also have this problem.
I do not really have any code to show here because it would just be the event as generated by the designer and a Console.WriteLine message to see if it is firing (this line outputs for other events). What I am hoping for is some insight as to what could cause this problem.
The entire program is quite large, so I cannot really clip the whole thing into this forum so that individuals can hunt for a specific problem. My hope is that somebody might be able to point me to what might cause this behavior. I am thinking that maybe something got screwed up when editing in design mode, but I just do not know what to look for. I am relatively new to C# and programming is a hobby for me.
Thank you for your time,
FC
You need to try the property SelectedIndexChanged; to expose that property, with the property window open, click once outside the tabbed container then click one of the small tabs once, don't click inside the tab area. Find the property, type in an event name and then double click it.
Context
In a C# Windows Forms application for Win7 we host an ActiveX control. So far, so good.
Need: prevent ActiveX to receive mouse events
Actually, the ActiveX has too much functionality. It is not very customizable to turn off features we don't need.
It would be nice if we just prevented it from receiving mouse events.
The ideal would be to just treat events as if the ActiveX was not there, that is the mouse clicks go right through to the underlying (or containing) Control.
But if we just prevent the ActiveX from getting events that would be okay with us.
Search before you ask
Some answers to previous questions mention to protected override void WndProc(ref Message m), e.g. c# - Pass-through mouse events to parent control - Stack Overflow
Other mentions to implement IMessageFilters, e.g.:
winforms - C# Application-Wide Left Mouse Click Event - Stack Overflow
winforms - Handling a click event anywhere inside a panel in C# - Stack Overflow
winforms - C# Application-Wide Left Mouse Click Event - Stack Overflow
Capturing Mouse Events from every component on C# WInForm - Stack Overflow
Experiment: ActiveX still gets events
I have fairly extensively tried both override WndProc and IMessageFilter ways, filtering (opt-in and opt-out) a number of events. In some cases I can prevent events to reach native C# controls, but the ActiveX still got its share.
Filtering too much, prevented controls and ActiveX to paint, or even application to draw properly or caused crash on exit. This can be averted by selecting opt-in or opt-out options carefully.
Is there another way?
Is there another way from C#/.NET to host an ActiveX Control while preventing it from getting mouse events? Perhaps at application start time?
Try to do a EnableWindow(hand,FALSE) on the uppermost hwnd of the ActiveX Control after has been created. See Using Window Handle to disable Mouse clicks using c# how to do this in c#.
I just faced the same issue (with a different ActiveX).
#GunnarRoth's answer and the page Using Window Handle to disable Mouse clicks using c# - Stack Overflow imply platform-specific magic with window handle, which is better avoided when possible.
A hint, still, is that it has Enabled in its name.
The ActiveX host object is a Windows Forms Control. I just set Enabled=false and the effect is exactly what I want : no mouse clicks or keyboard events are seen by the ActiveX, yet it still displays fine.
I don't even know if this would answer my initial question with the initial ActiveX Control, but this time it does! :-)
Currently I have to develop a very simple WPF user control that allows the user to select several points on a canvas. The difficulty that I encounter, is that uses with a touch screen should be able to so by triggering a TouchDown event whereas users that don't have touch screen should use the mouse and thus trigger a MouseLeftButtonDown event. Is there a simple way to handle these two cases without duplicating code? Also, I need to use Mvvm Light, so code-behind solutions like How to get Touchscreen to use Mouse Events instead of Touch Events in C# app won't do the trick.
Your linked question provides an answer for you, whether you are using MVVM or not. Using MVVM does not mean that you cannot handle UI control events. It just means that you should write an Attached Property to handle them for you. So, your answer is yes, you can handle the two events together and in almost the same way as your linked page suggests.
Your only difference is that the handler must be attached to the events in an Attached Property. Rather than go over the whole story once again here, I'll just briefly explain the procedure and request that you view my answer from the What's the best way to pass event to ViewModel? question for a code example.
First declare your Attached Property with its getter and setter and ensure that it has a PropertyChangedCallback handler attached. The PropertyChangedCallback handler is where you attach your single handler to the events (the code example just attaches a single event). This just means that it will only attach the handler to the events when your Attached Property is set. Finally, just add your single handler to handle the two events.
I am using the ScintillaNET component and I am attempting to capture clicks in the margin, as this will determine how I respond. Unfortunately, the margins capture the mouse events (and don't provide a way, from what I can see, to get the click information - number of clicks, mouse button clicked, etc).
If this is indeed the case (I am not able to get that info directly), what is another way of capturing what the mouse is doing before the MarginClick event is fired?
Thanks for any help!
I did not figure out a solution to my specific question. Instead, I changed my code to look at the modifier keys that are pressed when the MarginClick event is fired. This solution works well, and I am going to accept this answer. However, if someone can specifically answer my question, I will accept that answer.
So, here's what I'm trying to do:
I'm making a game for practice using the GameStateManagement example from creators.xna.com.
The whole of the system is too much to explain here, so experience with the sample is kind of necessary.
The gist of it is that there are multiple game screens that overlay one another. One contains a board, overlayed above it is a few card images. The end goal is to be able to drag-and-drop the card image onto the table (or click it, the method isn't really the problem).
The actual question, then, is how can I pass data from one screen (the overlayed cards) to the other (the table).
I considered that it might be possible to create an event that gets fired when I drag-and-drop a card or click it or what have you, and then register an event handler on the other screen to handle it.
I, however, have pretty much no experience with custom events in C#. I'm not sure if all fired events go into a big "event pool" somewhere and handlers can pick them up regardless of where they are or how that works. I ask because the table screen and the overlayed screen are separate classes that both deal with drawing and updating, not with sending information around to one another.
Hopefully this is enough information to outline the problem decently. Any tips/advice are welcomed.
Thanks!
After some more experimenting, it looks like events are the way to go.
Without knowing the details of how events work, it seems that a fired event can be picked up from just about anywhere, so long as you register to receive it.
So, the solution to my problem was to create an "OnDrag" event in my "Hand Screen." Then to register to handle this event in the "Table Screen."
This resource helped me get going with the basic custom event handling: Writing C# Custom Events