is there any way to force a control to receive focus?
i have a custom control inherited from panel and i need to be able to handle Enter/Leave events for it..any idears?
I've never tried anything like this before and don't even know if it's possible, but i don't have time to try it right now and i thought i'd throw it out just in case it might actually work.
the Panel class doesn't seem to have a TabStop Property, so I was thinking, again, don't laugh if this isn't possibly, because it definitely might not be, but Panel inherits from the ScrollableControl which does have a TabStop property, maybe something like MyBase.MyBase.TabStop = True
I'm guess that won't work, which means I'm guess that it's not possible, but I'm sure there's somebody out there that knows more than me that could tell you how to do it.
I'm confused. The Panel control supplies Enter and Leave events--what are you looking for that isn't there?
might wanna look into it. Panel doesn't fire Enter or Leave...at least not for me.
Yah, that's why I mentioned the TabStop Property and noticed that the Panel doesn't have one. I'm not sure if the Panel can actually get focus itself. I believe only it's children can. But hey, I'm quite often wrong! ;)
Randy, can you be a little more crisp in defining your scenario? What exactly are you trying to do? Set focus to a control inside a Panel? Get a notification when a control inside a Panel recieves focus?
thanks
-mike
While Randy comes up with his post, I just thought of some neato focusing eye candy, that that would be great for...what's the event to use (or however we would need to do it) to be notified when any control inside a Panel gets focus or is that something you'd have to setup yourself by adding an event handler for each control as it's added to the panel?
well, let's say i have a custom control derived from Panel that works just like the collapsing panels in winXP explorer..and let's say i have two of them on a form, when i switch from one to another i need to be able to handle the Leave() [for example] event and see if anything inside the control has changed, and if it has, write out changes to a file or database or whatever..the real scenario is the same idea, but with about 10 or 15 of these controls, and I'd write a generic handler that tested the sender...this make sense?
I tried the following and saw consistent behavior. I have a Form with two Panels and two TextBoxes on each Panel. In the Panel's leave events I have:
private void panel1_Leave(object sender, System.EventArgs e)
{
Debug.WriteLine("Panel1 Leave");
}
private void panel2_Leave(object sender, System.EventArgs e)
{
Debug.WriteLine("Panel2 Leave");
}
When run, and I tab through the 4 TextBoxes, I get the following output:
Panel1 Leave
Panel2 Leave
Panel1 Leave
Panel2 Leave
Panel1 Leave
.....
Is this not the behavior you are looking for? Are you seeing a bug in this area? If you can supply a simple repro, I'll look into it.
thanks
- mike
this is sort of the behavior im looking for..and this is how im doing it right now..but if they click in any area IN the panel im looking for it to raise the leave and enter's instead of them having to tab to or click into another control that is IN the panel...that make sense?
ie: if i had 3 panels with nothing in them, and they clicked back and forth between those panels i'd want the events raised.
Ah. The problem is that the Panel doesn't raise Enter/Leave events unless some control within the panel can get the focus, and there's somewhere else on the form to put the focus. The Panel control acts as an "aggregator" for Enter and Leave events of all the controls within it--the event bubbles up to the parent level, as if in DHTML.
It's not clear to me how any other behavior would be of any use, but FWIW, that's how it works by default.
Randy, from your problem description a few posts up, you said that you were looking for notification in order to do some validation on things that may have changed inside the Panel.
If this is the case, shouldn't you have at least one control in there that the user can interact with, and therefore, have focus? If you don't have a control that can take focus in there, what could change that you'd need to do work based on?
thanks
- mike
FWIW - you might want to also look at the Validating / Validated events. But I think they are closely tied to Enter and Leave for when they are raised
I did hack up my own version of the Panel class that allows it to get focus when there are no other controls in it. Maybe it is a start to what you want? Since Panel hides the Enter\Leave events from showing in the designer, they still are hidden so you will have to do your event hookup manually (or if you are using VB .NET, you can use the WithEvents and Handles keywords.)
code:
[ Designer("System.Windows.Forms.Design.PanelDesigner, System.Windows.Forms.Design.DLL" ) ]
public class MyPanel : System.Windows.Forms.Panel
{
public MyPanel()
{
SetStyle(ControlStyles.Selectable, true);
this.TabStop = true;
}
protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e) {
this.Focus();
base.OnMouseDown(e);
}
}
thanks,
Mark Rideout
thanks for the help mark, but here's a thought..is that going to force the control to focus and throw an Enter event everytime a user clicks? or just once? ...im not at home so i dont have vs.net to test that on..
Mike -- actually yes, originally when i posted that your solution would work..but heres another one (again im not near vs.net so i cant test this) but if you have two panels inside that panel, with controls on those panels and let's say a splitter in between them..would the Enter circulate all the way up to the top? but once i started thinking about it, in the XP style collapsing panel for example..the collapse/expand is drawn simply using DrawIcon() and checking the MouseDown to see if its "hot" ..there are situations where i might have an empty windowpane (eventually the class may support dragging and dropping items between panels, and they could possibly get all of the items off of a particular panel..)
First off, yes, nested controls should raise Enter and Leave events all the way up the parent hierarchy. If this isn't working for a given scenario, it is a bug.
Secondly, in your drag scenario, you would want to hook the DragEnter event.
Randy, at this point, there's just too much hype. You need to write this control and share it with the community. :) I promise we'll help you work through any eventing issues.
- mike