Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Default Button Behaviour

Posted on 2004-04-29
10
Medium Priority
?
575 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
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 1000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

721 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