View Single Post
  #10   Report Post  
Posted to microsoft.public.excel.programming
okaizawa okaizawa is offline
external usenet poster
 
Posts: 129
Default Firing Click Events with Accelerators

Hi,
it seems that a control which has focus cannot be hidden in some
event procedures.
i don't have good things but this is a way that i use, as a last resort,

'standard module
Sub HideControl()
UserForm1.optYes.Visible = False
UserForm1.optNo.Visible = False
End Sub

'UserForm1
Private Sub optYes_Change()
If optYes.Value Then
cboVehReg.Enabled = True
lblYes.Visible = False
lblNo.Visible = False
'optYes.Visible = False
'optNo.Visible = False
'hide controls after this sequence ends
Application.OnTime Now(), "HideControl"
txt2.SetFocus
End If
End Sub

--
HTH
okaizawa

Geoff wrote:
Following on from an earlier thread which asked how to select an optbutton
and fire its click event from the keyboard, a new issue arises. No criticism
is intended on the suggested code and I hope the contributor is able to see
this new thread.

In contrast to the more conventional method of keying in the button's
accelerator then spacebar to fire the click event, I used the suggested code
which avoids the spacebar as reducing keyboard effort is a desirable aim.

For brevity 1 button is shown as the last 2 subs are repeated with relevant
name changes for each new button required.

Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As
Long) As Integer

Private Sub optYes_Enter()
If GetAsyncKeyState(vbKeyMenu) And &H8000 Then optYesValue =
True
End Sub

Private Sub optYes_KeyDown(ByVal KeyCode As
MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = Asc(UCase(lblYes.Accelerator)) And Shift = 4 Then
optYes.Value = True
End Sub

There are 2 scenarios:
1. 2 optbuttons, Yes and No, with appropriate labels. Function is to
provide choice to amend either a txtbox or combobox data. On selection,
focus is switched to the relevant control and the optbuttons hidden. An
accelerator key is attached to the label (not optbutton caption) and the tab
order is correctly sequenced to suit.
2. 1 optbutton which hides the frame in which it is located.

All 3 click events fire correctly using the mouse. Disarmingly, all 3 work
when using the conventional method of accelerator then spacebar:):) Using
the code above 1 event works but 2 fail at the point when trying to hide the
buttons.

Click events:
Private Sub optYes_Change() 'Works as expected
cboVehReg.Enabled = True
lblYes.Visible = False
lblNo.Visible = False
optYes.Visible = False
optNo.Visible = False
txt2.SetFocus
End Sub

Private Sub optNo_Change()
cboVehReg.Enabled = True
lblYes.Visible = False
lblNo.Visible = False
optYes.Visible = False
optNo.Visible = False 'Fails here
cboVehReg.Value = ""
cboVehReg.SetFocus
End Sub

The 3rd event in scenario 2 is similar.

I could understand if the cause of failure was in trying to hide the buttons
'recursively' ie before the control has lost focus but that works succesfully
in the other 2 handling methods. It also works with 1 button using this
method. Paradoxically it is the button that works which becomes the anomaly.

Does anyone knows what's happening here? I would appreciate your views.

T.I.A.

Geoff