Solved

Default Button Behaviour

Posted on 2004-04-29
10
554 Views
Last Modified: 2013-12-25

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
Comment
Question by:DJWalker
  • 4
  • 4
  • 2
10 Comments
 
LVL 7

Expert Comment

by:OHDev2004
ID: 10948400
in the gotfocus event of your button type the following:

Private Sub MyCommand_GotFocus()
Command1.Default = False
End Sub
0
 
LVL 7

Expert Comment

by:OHDev2004
ID: 10948416
where command1 is the default button in the form :)
0
 

Author Comment

by:DJWalker
ID: 10948585

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
 
LVL 11

Expert Comment

by:dbrckovi
ID: 10948641
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
 

Author Comment

by:DJWalker
ID: 10948720

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 11

Expert Comment

by:dbrckovi
ID: 10948898
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
 
LVL 7

Expert Comment

by:OHDev2004
ID: 10952050
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
 
LVL 7

Expert Comment

by:OHDev2004
ID: 10952082
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
 
LVL 11

Accepted Solution

by:
dbrckovi earned 250 total points
ID: 10956864
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
 
LVL 11

Expert Comment

by:dbrckovi
ID: 10956882
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now