C# change event order - c#

Edit*
ok sorry for not giving the real scenario,
actually i have datagridview created programatically, i put 2 event, which is mouseclick and ColumnHeaderMouseClick.
currently whenever user click on column header it will trigger mouseclick first then followed by ColumnHeaderMouseClick
can i change the order of the trigger? or can i know when user click i can check whether he click on column header or other place in "mouseclick event"?

I don't know of any way to do that. The events occur according to the events that are actually taking place. In other words, that would be like saying can you walk through a door without opening it first. It doesn't really make sense in the context.
Could you possibly switch the code you are calling for down and click? Or just use click and execute the events in whatever order you like there?

You cannot do this, it does not make logical sense. The MouseDown event necessarily occurs prior to the MouseClick event because the mouse button had to go down in order to initiate a click. When the mouse button goes down, a MouseDown event is raised. The MouseClick event cannot be raised until some time after that.
The order of the mouse events is explicitly documented on MSDN and cannot be modified.
And you say that this was just an example, that you are working on other events. Unfortunately, the answer will be the same regardless. The order in which events are raised is something decided by the programmer who wrote the code that raises those events. There is no mechanism for you, the consumer of the library, to change that order.
Like the mouse events discussed above, the MSDN documentation lists the order of all significant events raised by the WinForms library.
Of course, if you are writing the code that raises the events, you can always modify it to raise them in whatever order you want. But I suspect that much is obvious and not why you are asking this question.

Most C# implementations will typically fire off the event handlers in the order that they are registered to each event. However this is not enforced!
It could change at any time and as such you should not create a dependency on the order of event handlers in your application.
I strongly suggest you take a step back and review your application design for ways to remove this dependency.

As many have suggested on their answers trying to change the event order is not recommended for multiple reasons.
In order to achieve what you want, you would have to re-implement the Windows Forms controls of the .NET Framework. You can also try to hijack the Windows message processing by overriding the WndProc procedure and process directly the messages provided by the OS, but you will learn that the event firing order follows the order in which the OS sends the input provided by the device (mouse) to the affected windows/controls.

i got the solution already, i get the index of the row, if -1 its mean it is a header so i can do the if else already, no need for different event, enough with cell-click event alone can handle both scenario.
sorry for asking in complicated way when the solution is so simple, thanks for helping.

Related

How To Keep Checking For Something Without a Timer?

I need to be able to check if the mouse is within a certain area on the form continuously. I want to be able to do this without the use of a timer, though. How would I go about doing this?
I'm using C# btw.
Have you tried attaching a handler to the MouseMove event, and checking on each movement?
If the area is a screen control, you can use MouseEnter, MouseLeave, MouseHover and MouseMove events.
I think the mouse events suggested by others is the best solution, but as another alternative to timers, you could write a small function to check the mouse and then keep invoking it on your main window dispatcher with an "application idle" priority. This will continuously run your check without freezing the UI.
Again, hooking into the mousemove event is still a cleaner solution, IMO.
If hooking the MouseMove event triggers too often - or if you want to avoid hooking that event on every form, consider hooking the Application.Idle event instead.
This event fires every time the application is about to go idle - all pending messages (including repaints) have been processed and there's nothing left to do. In most WinForms applications, this happens several times a second, providing a good way to do "just in time" processing.
You need to define events over that area.
Use OnMouseEnter and OnMouseLeave together to decide if mouse is in this area or not !
OnMouseEnter until OnMouseLeave means that mouse is still in that area.

Application.Idle only fires after I mouse over my tray icon

I want to show a BalloonTip in the Application.Idle event of my program, but for some reason the Application.Idle event only fires after I mouse over the NotifyIcon. What gives?
Are you sure that Application.Idle is not getting fired? Simple way would be to log into the file whenever code enters the event and see if this happening.
Also understand that this event may not be suitable for your needs - it happens when message pump becomes empty (typically no keyboard/mouse input) - so as such you would probably receive this event too frequently (see this SO thread to understand more). In this case, I suspect that windows is suppressing the balloon tip perhaps because it is being shown too frequently.
As such, you can code to show the tip only if it has not been shown say in last 2-3 seconds. You may want to look at different implementations of Idle detection to suit your requirements - have a look at:
http://ellisweb.net/2008/02/detecting-application-idle-state-in-windows-forms/
http://blog.opennetcf.com/ctacke/2009/05/19/DetectingApplicationIdle.aspx
http://www.codeproject.com/KB/miscctrl/Application_Idle.aspx

Detecting mousewheel over non-focused window?

My goal is to make a floating toolbar (as its own C# application), and when the user uses the scrollwheel over me I want to change the buttons that are visible. Sounds easy enough, should just be a matter of this one-liner:
MouseWheel += new MouseEventHandler(Form1_MouseWheel);
The problem I am having is that the mouse wheel handler is only invoked when my application has focus. That means the user has to first click, and then mousewheel. That won't do for what I'm trying to do.
I can hook the MouseHover event handler and call form.Activate() then, to get focus. That's suboptimal because if the user uses the scrollwheel immediately after mousing over my application (instead of waiting a little), the focus will still be on the previous app and it'll get the mousewheel event.
A natural thing to do would be to hook the MouseEnter event and call Activate() there, but then instead of my application coming to the front, its icon starts to blink on the task bar. I'm using Win7, but this problem is probably older than this.
Ideally, what I'd like to do would be to detect the mousewheel events without having to worry about whether my application has focus. It would really be better for the previous application to keep input focus, so for example if the user's in Notepad they can type, mouse over to my app, use the scroll wheel, look at what they see and decide to just resume typing in Notepad. Ideally I don't want them to have to click even once in this scenario.
I'll settle for a solution that switches focus to my application, though, if there's no other way.
What I have so far uses C# and Windows Forms, but I'd be open to using something different if that can solve my problems.
So: how can I see those mousewheel events without the user having to click to focus my application first?
If you need to catch mouse events outside your application, you can use a global system hook. There's a good .NET implementation here

Which windows message triggers a form's load event?

I posted this answer (more of an idea really) but haven't been able to find out for sure which message triggers a WinForms Form.Load event. From Spy++ and some reading I suggested it might be WM_SHOWWINDOW, but I'd like to be sure.
Also, other than Spy++ is there a better way to see exactly which windows message triggered a .Net event? Even after switching off some event types in Spy++ the log window fills up very quickly.
Thanks.
There isn't a specific windows message which corresponds to the Load event on the Form class. The event is simply fired the before first time the window is made visible after the creation of the window handle of the Form.
It's used for initialization which requires the window handle of the Form to be created but before the Form is shown for the first time.
I'm guessing here, but it might be instructive to configure your dev environment to use Microsoft's source server, then have a look through the System.Windows.Forms code. See Shawn Burke's blog entry Configuring Visual Studio to Debug .NET Framework Source Code.

Check if a drag&drop is in progress

Is there any way to check if a drag and drop is in progress? Some method or win32 api which can be checked? I know I can set AllowDrop and use events but it doesn't work in this case. Basically i want to check, with code, if any drag&drop is in progress.
I had a similar question which I answered myself (after some hours messing about) See - How do I tell if a Drag Drop has ended in Winforms?.
Basically if you do as earwicker suggests you need to set the flag when the drag drop begins near the DoDragDrop call. You will need to unset the flag in both the DragDrop event and in the QueryContinueDrag if the QueryContinueDragEventArgs indicate a drop or a cancel.
The GetCapture API function might be a good start. Basically, when a drag operation starts, the source window "captures" the mouse, which means that it will still receive all mouse events even if the mouse leaves the window.
However, applications can also capture the mouse for other reasons, so this is not 100% reliable. You can try it and see how well it works for you. And with applications doing their own drag&drop handling, there's no way to be sure what is going on anyway.
What about QueryContinueDrag event handler http://msdn.microsoft.com/en-us/library/system.windows.forms.control.querycontinuedrag.aspx ?
You can hook a handler to any control and check if there is a ongoing drag&drop operation and then cancel it if you want to.
Ooops, sorry, I just saw that the guy before me already mentioned that. Me bad.
Assuming it's in the context of just your own code, you could identify all the places in your code where a drag/drop happens, and set a global boolean flag to true for the duration of the operation, then back to false after it finishes.
So the next question is, how are drag/drop operations being started in your application?

Categories