DJWalker
asked on
Default Button Behaviour
I have written and ActiveX control. One of its constituent controls is a command button. When this button has the focus, I would like the default button on the containing form to lose its highlighting, just as it would if the button controlled in my control was placed on the form rather than being contained in the control.
How can I do this?
where command1 is the default button in the form :)
ASKER
There are a couple of problems with this approach:
1) "MyButton" is in a control. It's not the control's job to be setting properties on the parent form.
2) I'd have to recover the default property at some other point.
Currently, the DefaultCancel property is set to true on the user control so the highlighting is lost when any of my constituent controls get focus. This kind of achieves what I want - I don't have to worry about changing the default property at all. The downside is that I would really like it to change only when the button has focus, not the other controls.
Try this:
- under Control's general section declare this: Dim hadfocus As Control
- paste this code inside this control:
-------------------------- ---------- ---------- ---------- ---------
Private Sub UserControl_EnterFocus() 'When control recieves focus
For Each ctl In UserControl.Parent.Control s 'Loop through all "neighbour" controls
If TypeOf ctl Is CommandButton Then 'If neighbour is Command Button
If ctl.Default = True Then 'and if it is Default
ctl.Default = False 'then remove it's "Default behaviour"
Set hadfocus = ctl 'Remember which button it was so we can return Default flag to it later
End If
End If
Next ctl
End Sub
Private Sub UserControl_ExitFocus()
For Each ctl In UserControl.Parent.Control s
If TypeOf ctl Is CommandButton Then
If ctl = hadfocus Then ctl.Default = True 'loop through controls and when correct button is found, return "Default" to ut
End If
Next ctl
End Sub
-------------------------- ---------- ---------- ---------- ----------
This should be it.
- under Control's general section declare this: Dim hadfocus As Control
- paste this code inside this control:
--------------------------
Private Sub UserControl_EnterFocus() 'When control recieves focus
For Each ctl In UserControl.Parent.Control
If TypeOf ctl Is CommandButton Then 'If neighbour is Command Button
If ctl.Default = True Then 'and if it is Default
ctl.Default = False 'then remove it's "Default behaviour"
Set hadfocus = ctl 'Remember which button it was so we can return Default flag to it later
End If
End If
Next ctl
End Sub
Private Sub UserControl_ExitFocus()
For Each ctl In UserControl.Parent.Control
If TypeOf ctl Is CommandButton Then
If ctl = hadfocus Then ctl.Default = True 'loop through controls and when correct button is found, return "Default" to ut
End If
Next ctl
End Sub
--------------------------
This should be it.
ASKER
I'm feeling like I'm being picky, but these aren't the solutions I'm looking for, I don't think it should be the control's job to be setting properties on the parent form and the code above achieves exactly the same as setting the DefaultCancel property, which requires no code at all.
One other point - it's not necessary to loop through controls in the UserControl_ExitFocus above, the following would do it:
Private Sub UserControl_ExitFocus()
hadfocus.Default = true
End Sub
I didn't see your previous comments when I was posting mine.
I didn't have time to test this, but it should work:
- Copy the code from UserControl_EnterFocus to GotFocus event of every button inside your control.
- After this remove ctl.Default = False line from UserControl_EnterFocus event
- To GotFocus event of every other control place hadfocus.Default = true
So now the theory is:
- when your main control recieves focus nothing appearant will happen
- when any of your internal buttons recieves focus, the external button with Default flag will lose it
- when any other internal contol which is not a command button recieves focus, Default flag is returned back to external button which previously had it
- when your entire control loses focus, Default flag is returned back where it was
Instead of using GetFocus event of non-button controls maybe this can be done by returning the Default flag in "InternalButton"_LostFocus () but in my case LostFocus() is called riht after GotFocus()
and I didn't find out why yet.
It's possible that couse to this behaviour is the rest of the code in my control. (I'm testing this on one of my previous ActiveX projects)
I didn't have time to test this, but it should work:
- Copy the code from UserControl_EnterFocus to GotFocus event of every button inside your control.
- After this remove ctl.Default = False line from UserControl_EnterFocus event
- To GotFocus event of every other control place hadfocus.Default = true
So now the theory is:
- when your main control recieves focus nothing appearant will happen
- when any of your internal buttons recieves focus, the external button with Default flag will lose it
- when any other internal contol which is not a command button recieves focus, Default flag is returned back to external button which previously had it
- when your entire control loses focus, Default flag is returned back where it was
Instead of using GetFocus event of non-button controls maybe this can be done by returning the Default flag in "InternalButton"_LostFocus
and I didn't find out why yet.
It's possible that couse to this behaviour is the rest of the code in my control. (I'm testing this on one of my previous ActiveX projects)
if you only want to cancel the highlight of a commandbutton in the mainform when your button gets the focus and recover the default action again to the control which had it you can use the following :
Dim DefaultCmd As Object
Private Sub MyCommand_GotFocus()
Dim x As Integer
For x = 0 To Me.Controls.Count - 1
If TypeOf Me.Controls(x) Is CommandButton Then
If Me.Controls(x).Default = True Then
Set DefaultCmd = Me.Controls(x)
DefaultCmd.Default = False
End If
End If
Next x
End Sub
Private Sub MyCommand_LostFocus()
DefaultCmd.Default = True
Set DefaultCmd = Nothing
End Sub
this routine will check for the control which is set as the default and removes the default from it ,, and when your command looses focus the Default returns again to the appropiate control which had the default ,,
Dim DefaultCmd As Object
Private Sub MyCommand_GotFocus()
Dim x As Integer
For x = 0 To Me.Controls.Count - 1
If TypeOf Me.Controls(x) Is CommandButton Then
If Me.Controls(x).Default = True Then
Set DefaultCmd = Me.Controls(x)
DefaultCmd.Default = False
End If
End If
Next x
End Sub
Private Sub MyCommand_LostFocus()
DefaultCmd.Default = True
Set DefaultCmd = Nothing
End Sub
this routine will check for the control which is set as the default and removes the default from it ,, and when your command looses focus the Default returns again to the appropiate control which had the default ,,
sorry it seems that my code is the same as the code of dbrckovi
i just reialized that now ,, lol ,, i don't know what the problem is now ,,
i just reialized that now ,, lol ,, i don't know what the problem is now ,,
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Correction !
In my previous comment:
WAS:
copy and paste "HadFocus.Default = True" and "HadFocus.Default = False" to "GotFocus()" and "LostFocus()" events respectively to all buttons which you have in your control.
SHOULD BE:
copy and paste "HadFocus.Default = False" and "HadFocus.Default = True" to "GotFocus()" and "LostFocus()" events respectively to all buttons which you have in your control.
In my previous comment:
WAS:
copy and paste "HadFocus.Default = True" and "HadFocus.Default = False" to "GotFocus()" and "LostFocus()" events respectively to all buttons which you have in your control.
SHOULD BE:
copy and paste "HadFocus.Default = False" and "HadFocus.Default = True" to "GotFocus()" and "LostFocus()" events respectively to all buttons which you have in your control.
Private Sub MyCommand_GotFocus()
Command1.Default = False
End Sub