I want to open a windows form and set focus to the first field. There are many panels, group boxes, controls and event handlers in this form and they are apparently interfering with setting focus, which leads to three questions.
1) Is there a sure fire way to set focus to a particular control on load. This particular control is a form being loaded as a showdialog from another control. I've tried calling [control].focus() as the last item in the form_Load event and it does not work.
2) The tab order appears to want to cycle through all controls in a container, like a panel before going to the next panel. I do not want this order. Is there any way to overcome this besides hardcoding the next focus in the leave event.
2) In trying to step through in the debug mode while the control is loading, I get thrown in what seems like an endless series of paint events getting fired. Is there a setting or attribute that can be set on these events to skip these events in the debug process.
Thanks,
Rick

Focus() problem
Kris_H
You must be setting a break point, or the IDE would not be stopping, and you wouldn't be 'stepping through' anything. You cannot stop the paint event from firing, excepting in the manner I described.
Prakashalladi
Mahasweta Das
Hi Rick,
Thanks, that is an interesting thought and I'll try it to see if it helps. Still would like a way to tell the debugger to automatically skip over the event or to prevent the event from firing, except once.
you may try to right click on the breakpoint, and select Hit Count.
In the following dialog, choose "break when the hit count is equal to 1", so that it only breaks in the first time.
As I observer it, in the IDE of a form with containers, it assigns a mult-part number to each control, the tab order you set only affects the last digit in the number, the higher order numbers represent the container, it then seems to sort by the total number, hence cycling through the container before going to the next container. It seems I can only override this behaviour with directing flow in code.
Also you may try to set the TabStop property to false if you do not want certain control to get focused when Tab
For whatever reason, it doesn't work that way in my form, hence the questions.
As a followup question, what is the very last event to fire when loading the form (ShowDialogue), I'm hoping if I can set Focus in the very last event, I can get it to stick.
I have tried to set breakpoint on all the form event, it seems that the Activate will be the last event. You may have a try.
BTW: based on my test, the focus approach works at my scenario, can you build a simple reproduce sampel for us to do further troubleshooting, you can reach me via removing "online" from my email address.
If you still have any concern, please feel free to post here.
Best regards,
Peter Huang
tzmedia
Set the tabindex property of the control to any negative value. Make sure that the tabIndex value being set is the lowest amongst all the controls in the forms. You can do this programatically or at design time.
JJSarte
Peter:
Using the break point features won't work because I'm not setting a break point. The paint event is getting fired over and over agin which takes me through a lot of lengthy code that I'd prefer not to have to step through. If I can't find another way, I may change the event handler so that instead of containing code, it calls another method with the code, then I can at least F10 past the method.
Setting tabstop doesn't work because I do want to stop, though it may be useful as part of a manually coded process.
My control is a user control and it doesn't have an activate event. I suppose I could wire up a delegate to the form event, but I'd like to find a simpler solution. For a temporary work around I set up a timer in the constructor to fire once after a short delay. It calls a method to set focus. It's pretty sloppy but it works. It would be nice if there was some 'final' start up event, comprable to the 'finally' in try/catch.
I don't 'see' your email address otherwise I'd send you a sample app demonstrating the tab problem. You could recreate it by creating a windows form app, adding two panels with two text boxes each. Interestingly, when I created the sample app and used the GUI interface to set the tab order from panel1text1 to panel2text1 to panel1text2 to panel2text2, it worked the first time I ran the program. After changing the order around, I couldn't get it to work again. Setting the tab order in the properties to the above order also will not work. The tab order wants to go panel1text1 to panel1text2 to panel2text1 to panel2text2, despite the property tab order.
Rick
TBone
Hi Rick,
I tried out and it worked the way I mentioned earlier. I added couple of textboxes and then set the tab index property of each one of them with unique +ve integer value. Then I set the tab index value of textbox for which I want to set the focus on Form Load to 0. I had no issues with it.
spirit_of_pk
If you place the IDE and the form being loaded so they do not overlap ( having two monitors is best ), then your breakpoint won't invalidate the form ( through the IDE popping up ) and thus causing the paint event.
You can set the tab order in the properties for each control in the IDE
I thought the item with the first tab index would get focus by default.
ShrinanD Vyas
If you place the IDE and the form being loaded so they do not overlap ( having two monitors is best ), then your breakpoint won't invalidate the form ( through the IDE popping up ) and thus causing the paint event.
Thanks, that is an interesting thought and I'll try it to see if it helps. Still would like a way to tell the debugger to automatically skip over the event or to prevent the event from firing, except once.
You can set the tab order in the properties for each control in the IDE
As I observer it, in the IDE of a form with containers, it assigns a mult-part number to each control, the tab order you set only affects the last digit in the number, the higher order numbers represent the container, it then seems to sort by the total number, hence cycling through the container before going to the next container. It seems I can only override this behaviour with directing flow in code.
I thought the item with the first tab index would get focus by default.
For whatever reason, it doesn't work that way in my form, hence the questions.
As a followup question, what is the very last event to fire when loading the form (ShowDialogue), I'm hoping if I can set Focus in the very last event, I can get it to stick.
Thanks,
Rick
Kalpesh Paradava
I set the break point in the constructor and try to step thru till the control is loaded. I do not set a break point in the paint event handler. As I step thru, it takes me into the paint event code over and over and over again. So, I do not want to skip the break point, I want to be able to tell the debug process to skip the paint event handler code where there is no breakpoint set.
Rick
chilling
I suspect you are just guessing and haven't tried this yourself, since the tabindex will throw an error if it isn't greater then or equal to zero.
Rick
Eric Cooper
What 'should' happen and what actually happens are two different things. What you are describing is what 'should' happen. The point of the thread is dealing with what 'actually' happens. In other words, while you would expect it to, in actuality, it does not follow the numbered order of the tabindex property.
Rick