Windows Develop Bookmark and Share   
 index > Windows Forms Designer > is there any way to force a control to receive focus?
 

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?
MigrationUser 1  Friday, February 21, 2003 9:15 AM
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.
MigrationUser 1  Friday, February 21, 2003 3:09 PM
I'm confused. The Panel control supplies Enter and Leave events--what are you looking for that isn't there?
MigrationUser 1  Monday, February 24, 2003 11:42 AM
might wanna look into it.  Panel doesn't fire Enter or Leave...at least not for me.
MigrationUser 1  Monday, February 24, 2003 4:59 PM
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!  ;) 
MigrationUser 1  Monday, February 24, 2003 5:11 PM
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
MigrationUser 1  Monday, February 24, 2003 5:45 PM
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?
MigrationUser 1  Monday, February 24, 2003 5:48 PM
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?
MigrationUser 1  Monday, February 24, 2003 5:55 PM
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
MigrationUser 1  Tuesday, February 25, 2003 2:01 AM
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.
MigrationUser 1  Tuesday, February 25, 2003 6:13 AM
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.
MigrationUser 1  Tuesday, February 25, 2003 8:54 AM
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
MigrationUser 1  Wednesday, February 26, 2003 5:05 PM
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
MigrationUser 1  Wednesday, February 26, 2003 5:22 PM
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..)
MigrationUser 1  Wednesday, February 26, 2003 8:54 PM
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
MigrationUser 1  Thursday, February 27, 2003 4:14 AM
the control is actually already written..i've worked through most of the issues and i don't think there's any real problems i havent figured out. i'll have to wrap up the zip and post it up on the controls gallery...
MigrationUser 1  Thursday, February 27, 2003 9:28 AM
Just to note - the panel only fires the Enter the first time focus is either on the Panel, or on a control in the panel, and fires Leave when focus leaves the Panel (or any control contained in the Panel). So, you wouldn't get an Enter everytime a users clicks, unless after the click you set the focus back to something else. Basically it works like it normally would. Note also that my little test Panel code also supports tab-stop to the Panel whether it is empty or contains controls. This helps for keyboard accessibility.

-Mark
MigrationUser 1  Thursday, February 27, 2003 11:04 AM

You can use google to search for other answers

Custom Search

More Threads

• Where does Windows.Forms implement the control property editor?
• Design mode repaint problem
• Databinding faults due to Generic class
• Changed event
• Help Need Power Point Rect Tracker Source Code in VC6 or VC7
• Adding EDIT function to DATAGRID Rows
• VS 2008 error HRESULT 0x80042929
• Designer: Value does not fall within expected range
• Creating Tree View in Datagrid in VB.Net 2005?
• Can't get inheritance or association to work in class designer (Visual Studio 2005 Standard Academic)