[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 582
  • Last Modified:

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?
0
DJWalker
Asked:
DJWalker
  • 4
  • 4
  • 2
1 Solution
 
OHDev2004Commented:
in the gotfocus event of your button type the following:

Private Sub MyCommand_GotFocus()
Command1.Default = False
End Sub
0
 
OHDev2004Commented:
where command1 is the default button in the form :)
0
 
DJWalkerAuthor Commented:

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.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
dbrckoviCommented:
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.Controls                        '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.Controls
        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.
0
 
DJWalkerAuthor Commented:

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

0
 
dbrckoviCommented:
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)
0
 
OHDev2004Commented:
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 ,,
0
 
OHDev2004Commented:
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 ,,
0
 
dbrckoviCommented:
The problem is that DJWalker's control contains more than one sub control. Among them it has one or more command buttons.
When one of those command buttons recieves focus, default button on main form should loose it, but if some other sub-control (like textbox, combobox) recieves focus, default button should remain default
         ( it should have that black rectangle around it )

At least this is what I think the problem is.

In my last comment I have said that LostFocus of internal button is called immediately after GotFocus. It was some bug in my code so here goes what I think is the sollution:

This is the code for ActiveX control which has at least 3 command buttons. To implement the same behaviour on other buttons inside it, just do the obvious:
     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.

--------------------------------------------------------
Dim HadFocus As Control                               'to remember which external button was default

Private Sub Command1_GotFocus()
    HadFocus.Default = False                          'when internal button recieves focus, remove black rectangle from external Default button
End Sub

Private Sub Command1_LostFocus()
    HadFocus.Default = True                           'When focus is lost, return Default flag to external button
End Sub

Private Sub Command2_GotFocus()
    HadFocus.Default = False
End Sub

Private Sub Command2_LostFocus()
    HadFocus.Default = True
End Sub

Private Sub Command3_GotFocus()
    HadFocus.Default = False
End Sub

Private Sub Command3_LostFocus()
     HadFocus.Default = True
End Sub

Private Sub UserControl_EnterFocus()                                 'Get the default button
    For Each ctl In UserControl.Parent.Controls
        If TypeOf ctl Is CommandButton Then
            If ctl.Default = True Then
                Set HadFocus = ctl
            End If
        End If
    Next ctl
End Sub
----------------------------------------------------------------
0
 
dbrckoviCommented:
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.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 4
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now