Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

textbox focus problem

Status
Not open for further replies.

jrl237

IS-IT--Management
Jan 29, 2002
61
US
I'm having a strange little problem I can't figure out.

I have a Windows form with several controls on it. One control is a textbox where the user can enter a 10-digit code. This box may also be filled in programmatically in certain situations.

The problem is, if I fill the box in from code, then try to set the focus to the next control, the focus doesn't change. Here's basically what I'm trying to do:

Code:
  textbox.text="1234567890"
  cbType.focus()

Seems simple enough. But the focus remains on the textbox.

There's an event handler for textbox.lostfocus which does some validation on the input. I've commented this code out, though, and still get the same results.

I've tried changing the focus to several different controls, just to see if I can get it to move. I've tried using SendKeys.send to simulate a tab key. No matter what I do, the focus won't leave the textbox.

If the user enters the code and hits tab, the focus changes just fine.

Anyone have any idea what could be wrong, or what more I could try? This is really driving me mad.

Any help would be greatly appreciated.

JRL
 
Firstly, make sure cbtype is enabled and able to recieve focus.

Secondly, do a search for 'textbox.focus' to make sure your not setting the focus back to the textbox in a long-forgotten event.

If that doesn't work, are there panels or group boxes involved? (shouldn't really make any difference but just in case...)
 
Jubble,

Thanks for the response. Yes, cbType is enabled and able to receive focus.

There are no panels or group boxes on the form.

I do set the focus back to the textbox in some events. But I've commented all of these out. Still no go.

It does work when the user enters info and tabs out. It doesn't work when the textbox is filled from code then the focus is shifted programmatically.

Weird, huh?
 
All I can say is Im glad someone else has noticed this odd behaviour. I have found that sometimes .Focus works, and othertimes .Select works. There appears to be no pattern to it as such, and the problem is mainly in cases like this where you are trying to force focus to the next control, usually from the Lost focus, validated or Leave event of the previous control in the Index.

So in short
1) Try .Select (seems more reliable than .Focus)
2) Try moving the change focus code to another event.
3) Try setting the forms.ActiveControl to Nothing before changing focus



Sweep
...if it works dont mess with it
 
could you send some of your code? in particular the method that populates the textbox and sets focus to the combo + the method that calls it.
 
The 'oddity' of .focus deals with the Order of Opperations. There was a great post and link to article on it a few months ago. This gets expecially hair when say the user hit's tab.

Tab is pressed
Form Key down preview?
Control Key down event
Form Key Up preview?
Control Key up event
Form Key Pressed preview?
Control key pressed event
Control validate events
Control lost focus events
[Code to set focus]
target got focus events
next control got focus events

There may be more, and I'm not even sure if that is the reight order. The form registers tab to move to the next control which will eventually fire the lost focus event for the control that currently has focus. But if you set the focus in the lost focus event of the object, there is still the stack that is planning on setting the original focus.

I'm not sure how all of the layers of the stack interact at that point, setting the focus may clear the stack inorder to avoid the issue, but who's to say it's consistant accross all control types?

so it's a big hair mess, but if you can track down the order of events on the lost focus, it may help you find the best place to put your code.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Thanks for all the repsonses. I tried everything.

I found a solution that isn't great, but it works.

Instead of updating the textbox and switching the focus programmatically, I use SendKeys.sendWait(value & vbTab) followed by a SendKeys.Flush(). That solves my problem, but I don't understand why it works when cbType.focus() didn't.

Oh well. Chalk this up as one more thing I don't understand.

Like killer coding ninja monkeys.

Thanks again.
 
It's all about the order of events. By using sendkeys it is going to fire the form's key preview events, the controls key events, the control's lost focus and the next control's got focus. Most likely the delay of going through the stack from the keypress is enough to allow the rest of the previous stack to clear.

-Rick

PS: don't worry about the killer coding ninja monkies, it's the drunken looting code stealing pirates ya have to watch out for ;)

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Specifically, you can use the Control.Focus method in the Load event of the form to set the focus on a control only after the Visible property of the form is set to True.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top