views:

403

answers:

9

Rep steps:

  1. create example .NET form application
  2. put a TextBox on the form
  3. wire a function up to the TextBox's Enter event

When you run this application, the Control.Enter event fires when focus first goes to the TextBox. However, if you click away into another application and then click back into the test application, the event will not fire again.

So moving between applications does not trigger Enter/Leave.

Is there another alternative Control-level event that I can use, which will fire in this scenario?

Ordinarily, I would use Form.Activated. Unfortunately, that is troublesome here because my component is hosted by a docking system that can undock my component into a new Form without notifying me.

+3  A: 

What are you trying to do in the Enter event?

I can't find another control-level event that fires in your example program but when my test app does regain focus, the control that last had focus still has it.

Interesting question but it needs a little more context.

Austin Salonen
A: 

In your example, I think you need another control. The reason being is that the first control (tabIndex 0) is the one with focus. With no other control to switch focus to, this control will always be focused, and therefore can never be entered. Switching to another application or form will not change the focus or active control in this form so when you return you will still not get the event fired.

With added controls control.entered should work fine. If this is your only control, why not call the event on formLoad, or TextChanged, when the form gets focus?

Fry
A: 

Thanks, I'll give some background.

My control is a UserControl that contains a grid and a toolbar. A user will typically launch several of these controls to view different slices of the system's data.

There are several keyboards shortcuts that can launch actions from the selected row in the current grid. However, it is a requirement that these keyboard shortcuts should apply not only to the currently focused grid. If the user is currently focused on one of the many other areas of the application, then this keyboard shortcut should still work, and it should be routed to the last focused grid.

So I wired a function to the Control.Enter event of my UserControl to basically say LastFocusedGrid = this.

And it would work, except for the docking and undocking...

See, these controls are hosted inside an application with docking features, somewhat similar to visual studio.

By default, the control launches as a tab within the main working area of the application, similar to the way a source file opens in visual studio.

However, the user can "rip out" a tab by grabbing the tab header and dragging it out of the main application. At this point, the application creates a new "float form" to host the control. Switching between the main application and this float form is the same as switching between apps, for the purposes of the Control.Enter and Form.Activated events.

At that point we have the "one control within a form" scenario simulated with the example application described in the original post.

Now, there are some ways around this. I could leverage the Form.Activated event, which DOES fire when switching between forms. If you add an event in the test application to the Form's Activated event, you will see that it works great.

The problem is that my UserControl's relationship with its parent Form is fluid, making the solution somewhat complicated. I tried wiring up to "this.ParentForm.Activated" which worked okay. The problem is when do you call this? What happens when you are undocked/redocked? I ended up with a nasty bunch of code with things like "previousParentForm" so that I could unhook from the old form, and then I was still facing the problem that the docking system doesn't notify me when my parent Form is being changed, so I was going to have to make a bunch of changes there, too.

These problems are not unsolvable, but if there is a simpler control-level "parent form was activated" event, then that would be a lot more elegant.

That's rather long, but I hope it clarifies the situation.

rice
A: 

So when creating your grid, can you not set the KeyPressed, or KeyUp, etc. event? If so, all the grids can make use of the same event handler. Just make sure that when you get into the event handler to do something like:

Grid currentGrid = (Grid)sender;

Then you should be able to apply that block of code to any grid that gets sent in without having to worry about keeping track.

Since all the event handler really is, it's location is a mute point really as long as everything you need to execute it is accessible.

Fry
"moot" not "mute" ;)
Jason Dagit
A: 

Frye, the problem is that the keyboard shortcuts should work no matter where the user is in the application. They are gloabl commands, handled at the top level, and then routed to the "last focused grid."

So handling the keystrokes at the grid level will not help.

To be more specific, assume user launches grids A, B, and C. But he also launches other controls X, Y, and Z that have nothing to do with my code.

User clicks on A, then on C. Then he clicks on Y, then on Z. With focus on Z, he hits my keyboard shortcut. In this case, grid C should respond since it was the last grid the user was focused in.

rice
Ok... I'm going to say I'm a little confused here, specifically with the "other controls X, Y, and Z that have nothing to do with my code". Is this an external program? or code outside the scope of the code you're writing?
Fry
A: 

It sounds like the issue that you're having is not directly related to the Enter event and more to the point, if you have controls "that have nothing to do with your code" then you really aren't looking at a control level event.

Fry
A: 

Guess I wasn't clear.

My control lives in a container application. So do other unrelated controls by other teams. Think of it like visual studio -- my control is the code editing tab, but there is also the pending changes list and the properties window, which cohabitate with the source files but aren't directly related.

The keyboard shortcut is handled by the container application. Then it should be routed to the last one of my controls that the user was focused on.

Maintaing this "LastFocusedGrid" reference is what I do in the Enter event.

If you want to see similar functionality at work in visual studio, try this:

  1. open a few source files
  2. navigate to the "Start Page" tab.
  3. Hit Ctrl-F and search "current document" for some string
  4. Notice that the serach feature auto-navigates to the LAST FOCUSED source file to perform the search.

So even though you weren't focused in the source file, the ctrl-F command was processed by visual studio and routed to the last focused source file tab.

Now try the same thing with Ctrl-G. It doesn't work unless you are focused directly in the source file.

My keyboard commands need to work like Ctrl-F here, not like Ctrl-G. That is why I don't just capture the keyboard events directly in my control.

Does that clarify or make things worse?

rice
A: 

Have you tried just a simple Control.GotFocus?

Fry
Yes, I have. As far as I can tell, that event never fires. I tried it in both my test app and my actual app, it never once fired.
rice
+1  A: 

If I try your example and click outside the control on another window, desktop, etc, I can get the Got and Lost Focus events to fire, but if you're only trying to click within a form or a control with only 1 control, these event will never be fired because it is the only thing to focus on. Neither will Entered or left, unless you change the dynamics or overload the controls, you cannot get this to happen

Fry
Interesting. I verified this and you're right.Enter will never fire in my scenario.GotFocus will never fire if you have exactly 1 control.GotFocus WILL fire on task-switch if you have > 1 control.
rice