dbTalk Databases Forums  

Setfocus going to wrong control

comp.databases.ms-access comp.databases.ms-access


Discuss Setfocus going to wrong control in the comp.databases.ms-access forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
The Frog
 
Posts: n/a

Default Setfocus going to wrong control - 03-30-2010 , 06:09 AM






Hi Everyone,

I am stumped. I have a form with multiple listboxes (cascaded if you
will) that 'lead' to a final listbox and under this listbox is a
textbox. On the final listbox (lstProduct), when clicking with the
mouse to select a row, the subform shows the related records from a
table. So far so good. I have tried to implement a set of 'rapid'
keyboard controls to allow a user to jump around the form without
using the mouse. This is where it gets weird....

For the cascaded listboxes this works perfectly. For the lstProduct
control I am having a problem that when a row is selected, the related
data shows up in the subform, but instead of the focus being set to
the textbox (txtProduct) - as it does with the mouse - it is jumping
to the subform. I am using the KeyDown event on both the txtProduct
and lstProduct controls, and the keypress is being 'captured' in the
event (the keycodes are correct). The last lines of code in the Up
Cursor or Down Cursor keys (38 for up and 40 for down) are to set the
focus to the txtProduct control. It just isnt happening. When stepping
through the code the .SetFocus is being called but it just isnt going
to the correct control.

My code on one of the events is as follows:

If lstProduct.ListCount = 0 Then Exit Sub
If lstProduct.ListIndex <= 0 Then Exit Sub
lstProduct.Selected(lstProduct.ListIndex - 1) = True
txtProduct.SetFocus
txtProduct.Value = lstProduct.Column(0, lstProduct.ListIndex)

Nothing special there. I am guessing that there is a complication when
involving the subform that it takes focus when it refreshes /
requeries the underlying table for the associated data, and doesnt
return focus to the main form and hence the setfocus call I am making
doesnt achieve anything because the focus has changed after that is
set.

I need to keep the subform able to be tabbed to (ie/ tabstop still
works), but not to have focus when changing records / rows in the
lstProduct control. Cany anyone point me in the right direction on
this?

Any help appreciated.

Cheers

The Frog
(A2003)

Reply With Quote
  #2  
Old   
The Frog
 
Posts: n/a

Default Re: Setfocus going to wrong control - 03-30-2010 , 06:11 AM






Oh yeah,

If it makes any difference or not here is the code for the lstProduct
OnClick event:

If lstProduct.ListCount = 0 Then
lstProduct.Selected(-1) = True
Exit Sub
End If

txtProduct.Value = lstProduct.Column(0, lstProduct.ListIndex)

subVariety.Enabled = True

txtProduct.SetFocus

Cheers

The Frog

Reply With Quote
  #3  
Old   
Stuart McCall
 
Posts: n/a

Default Re: Setfocus going to wrong control - 03-30-2010 , 08:05 AM



"The Frog" <mr.frog.to.you (AT) googlemail (DOT) com> wrote

Quote:
Hi Everyone,

I am stumped. I have a form with multiple listboxes (cascaded if you
will) that 'lead' to a final listbox and under this listbox is a
textbox. On the final listbox (lstProduct), when clicking with the
mouse to select a row, the subform shows the related records from a
table. So far so good. I have tried to implement a set of 'rapid'
keyboard controls to allow a user to jump around the form without
using the mouse. This is where it gets weird....

For the cascaded listboxes this works perfectly. For the lstProduct
control I am having a problem that when a row is selected, the related
data shows up in the subform, but instead of the focus being set to
the textbox (txtProduct) - as it does with the mouse - it is jumping
to the subform. I am using the KeyDown event on both the txtProduct
and lstProduct controls, and the keypress is being 'captured' in the
event (the keycodes are correct). The last lines of code in the Up
Cursor or Down Cursor keys (38 for up and 40 for down) are to set the
focus to the txtProduct control. It just isnt happening. When stepping
through the code the .SetFocus is being called but it just isnt going
to the correct control.

My code on one of the events is as follows:

If lstProduct.ListCount = 0 Then Exit Sub
If lstProduct.ListIndex <= 0 Then Exit Sub
lstProduct.Selected(lstProduct.ListIndex - 1) = True
txtProduct.SetFocus
txtProduct.Value = lstProduct.Column(0, lstProduct.ListIndex)

Nothing special there. I am guessing that there is a complication when
involving the subform that it takes focus when it refreshes /
requeries the underlying table for the associated data, and doesnt
return focus to the main form and hence the setfocus call I am making
doesnt achieve anything because the focus has changed after that is
set.

I need to keep the subform able to be tabbed to (ie/ tabstop still
works), but not to have focus when changing records / rows in the
lstProduct control. Cany anyone point me in the right direction on
this?

Any help appreciated.

Cheers

The Frog
(A2003)
This is a guess. When you process the KeyCode in your KeyDown event, are you
'throwing away' the keypress? (KeyCode = 0). If not then you should,
otherwise the keypress is left in the windows message queue and thus will
affect the UI.

Reply With Quote
  #4  
Old   
The Frog
 
Posts: n/a

Default Re: Setfocus going to wrong control - 03-31-2010 , 02:23 AM



I did not know this! I will give this a crack and see how it goes.
This would make a lot of sense given the behaviour. Thankyou
Stuart :-)

Cheers

The Frog

Reply With Quote
  #5  
Old   
The Frog
 
Posts: n/a

Default Re: Setfocus going to wrong control - 03-31-2010 , 03:09 AM



OK, I gave this a shot and have not had any success. I decided to test
what is going on a little further, and placed a stop call in each of
the keydown, keyup and keypress events. This is what I found: the
first time the down cursor key is pressed the control works as
expected, and (i dont know why) but the controls click event is also
fired. The second time the cursor key is pressed there is no
recognition of the stop call at all - it seems that none of the events
are firing! The focus is then set to the sub form.

I am going to hazard a guess here and assume that somewhere, somehow
the form is fundamentally broken. I will scrap it and replace it with
a new one with the same setup. Wont take too long. Extremely weird
thing to watch in the debugger / code window.......

If anyone else has seen this then please let me know!

Cheers

The Frog

Reply With Quote
  #6  
Old   
The Frog
 
Posts: n/a

Default Re: Setfocus going to wrong control - 03-31-2010 , 04:31 AM



Alrighty then.........

Form is rebuilt. Events are working. Setting the keycode = 0 at the
end if each desired keydown event option stops the subform getting the
focus. Final code as an example for the cursor down key on the KeyDown
event is as follows:

If lstProduct.ListCount = 0 Then Exit Sub
If lstProduct.ListIndex >= lstProduct.ListCount - 1 Then Exit
Sub
lstProduct.Selected(lstProduct.ListIndex + 1) = True
lstProduct.Value = lstProduct.Column(lstProduct.BoundColumn -
1, lstProduct.ListIndex)
txtProduct.SetFocus
txtProduct.Value = lstProduct.Column(0, lstProduct.ListIndex)
KeyCode = 0

Works perfectly

Thankyou for your help. I would never have guessed the keycode = 0
thing. It was also necessary to set the value property of the listbox
in the code so that the subform updated automatically (if anyone is
interested). If I didnt set this the subform didnt show the related
records.

Thankyou for all your help, I really appreciate it.

Cheers

The Frog

Reply With Quote
  #7  
Old   
Stuart McCall
 
Posts: n/a

Default Re: Setfocus going to wrong control - 03-31-2010 , 06:15 AM



"The Frog" <mr.frog.to.you (AT) googlemail (DOT) com> wrote

Quote:
Alrighty then.........

Form is rebuilt. Events are working. Setting the keycode = 0 at the
end if each desired keydown event option stops the subform getting the
focus. Final code as an example for the cursor down key on the KeyDown
event is as follows:

If lstProduct.ListCount = 0 Then Exit Sub
If lstProduct.ListIndex >= lstProduct.ListCount - 1 Then Exit
Sub
lstProduct.Selected(lstProduct.ListIndex + 1) = True
lstProduct.Value = lstProduct.Column(lstProduct.BoundColumn -
1, lstProduct.ListIndex)
txtProduct.SetFocus
txtProduct.Value = lstProduct.Column(0, lstProduct.ListIndex)
KeyCode = 0

Works perfectly

Thankyou for your help. I would never have guessed the keycode = 0
thing. It was also necessary to set the value property of the listbox
in the code so that the subform updated automatically (if anyone is
interested). If I didnt set this the subform didnt show the related
records.

Thankyou for all your help, I really appreciate it.

Cheers

The Frog
You're very welcome.

Ah. a corrupt form. That certainly wouldn't help!

The reason I was able to guess re the KeyCode thing is because I can say
'been there, done that' (and now so can you). :-)

I suspect you've done a lot of work/re-work on that form. That's when Access
mucks it up, usually. So I routinely do:

Application.SaveAsText
Delete the form
Application.LoadFromText

every now and then, whether the form is acting strangely or not. Works for
me..

Glad you're up & running.

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.