• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 246
  • Last Modified:

set focus to a command button in a group

I am working on a form to enable registration of my app.
I like the WinZip (and others)? idea of randomly altering the button captions, so users can't just click on the left hand button, say,  each time they want to ignore the Register Now message and open the programme.
I have got the idea working using a group of command buttons and  the button caption determines the result of a button click, and not the index.

My question:
Is it possible to set the focus always to the register now button. At present the focuse goes to the (0) element in the group, because the setfocusapparently can't see indivisual buttomns within the group.


Here is the simple code that does the job



' *********************************************************
' **  open this form                                  
' *********************************************************
Private Sub Form_Load()
   
    ' random order for button captions on buttons
    ShuffleButtons
   
    ' all buttons visible for start
    cmdRegister(0).Visible = True
    cmdRegister(1).Visible = True
    cmdRegister(2).Visible = True
   
End Sub


' =========================================================
'  winzip trick with register form button shuffling
' =========================================================
Private Sub ShuffleButtons()

Dim iValue      As Integer

' Seed Rnd
Randomize
   
' Generate random value from 0 to 5 inclusive
iValue = Int((6 * Rnd))
   
Select Case iValue
   
    Case 0
        cmdRegister(0).Caption = "Register now"
        cmdRegister(1).Caption = "Register later"
        cmdRegister(2).Caption = "Exit"
   
    Case 1
        cmdRegister(0).Caption = "Register now"
        cmdRegister(1).Caption = "Exit"
        cmdRegister(2).Caption = "Register later"
   
    Case 2
        cmdRegister(0).Caption = "Exit"
        cmdRegister(1).Caption = "Register later"
        cmdRegister(2).Caption = "Register now"

    Case 3
        cmdRegister(0).Caption = "Exit"
        cmdRegister(1).Caption = "Register now"
        cmdRegister(2).Caption = "Register later"
       
    Case 4
        cmdRegister(0).Caption = "Register later"
        cmdRegister(1).Caption = "Exit"
        cmdRegister(2).Caption = "Register now"

    Case 5
        cmdRegister(0).Caption = "Register later"
        cmdRegister(1).Caption = "Register now"
        cmdRegister(2).Caption = "Exit"

    Case Else
        MsgBox "error"
       
End Select

End Sub


' ************************************************************
' **  choose action based on button caption
' ************************************************************
Private Sub cmdRegister_Click(Index As Integer)

Dim iCount As Integer, iSave As Integer

    Select Case cmdRegister(Index).Caption
   
        Case "Register later"
            bEndProgramme = False
            Unload frmRegister
            Set frmRegister = Nothing
       
        Case "Register now"
       
            lblUser.Visible = True
            txtUser.Visible = True                ' enter user name here
           
            ' make exit button accept name button
            For iCount = 0 To 2
                If cmdRegister(iCount).Caption = "Exit" Then
                    iSave = iCount
                End If
            Next iCount
            cmdRegister(iSave).Caption = "Accept name"
                       
            ' make register now button exit button
            For iCount = 0 To 2
                If cmdRegister(iCount).Caption = "Register now" Then
                    iSave = iCount
                End If
            Next iCount
            cmdRegister(iSave).Caption = "Exit"
           
            ' make register later button disappear
            For iCount = 0 To 2
                If cmdRegister(iCount).Caption = "Register later" Then
                    cmdRegister(iCount).Visible = False
                End If
            Next iCount

        Case "Accept name"
            If SendRegRequest Then                ' send email request for registration
                bEndProgramme = True
                Unload frmRegister
                Set frmRegister = Nothing
            End If
           
        Case "Exit"
            bEndProgramme = True
            Unload frmRegister
            Set frmRegister = Nothing
       
        Case Else
            MsgBox "Error"
       
    End Select
   
 
End Sub
0
bantamb20
Asked:
bantamb20
  • 14
  • 9
1 Solution
 
Harisha M GCommented:
Hi bantamb20,
    You can check for the caption of the button and if it is not, then goto the next button.

Bye
---
Harish
0
 
Harisha M GCommented:
bantamb20,
   
Private Sub Form_Load()
   
    ' random order for button captions on buttons
    ShuffleButtons
   
    ' all buttons visible for start
    cmdRegister(0).Visible = True
    cmdRegister(1).Visible = True
    cmdRegister(2).Visible = True
   
    If(cmdRegister(0).Caption = "Register Now") Then
        cmdRegister(0).SetFocus
    ElseIf(cmdRegister(1).Caption = "Register Now") Then
        cmdRegister(1).SetFocus
    Else
        cmdRegister(2).SetFocus
    End If

End Sub

0
 
Harisha M GCommented:
bantamb20,
    You can also do this...

    For Each cmdBtn in cmdRegister
        If(cmdBtn.Caption = "Register Now") Then cmdBtn.SetFocus
    Next
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
bantamb20Author Commented:
when I use
cmdRegister(0).SetFocus or
cmdRegister(1).SetFocus or
cmdRegister(2).SetFocus

I get an error 5 'Invalid procedure call or argument'

This is the problem. I can't setfocus to a selected button within that group
0
 
Harisha M GCommented:
When you press "." after cmdRegister(0)

a list will dropdown. Does it contain SetFocus or not?
0
 
Harisha M GCommented:
Ohhh.. GOT IT

You can't set focus when the form is not loaded at all....
i.e, you can't use that in Form_Load

Use in Form_Paint instead
0
 
bantamb20Author Commented:
Yes, which has got me puzzled.
If it is apparently a valid (listed) method why does it error?
0
 
bantamb20Author Commented:
OK I have your last comment. Will try it.
0
 
Harisha M GCommented:
Because, the controls will be placed on the form only after the form is loaded completely.
During the Form_Load, the form will not be loaded completely. It will be evolving yet...
0
 
Harisha M GCommented:
Did that work ??
0
 
bantamb20Author Commented:
I can't get that to work. I think i'll try the old mouseover trick so when user first moves cursor the focus shifts to the correct button, since setfocus won't work in the Form_Load
thanks for your input
0
 
Harisha M GCommented:
Did you use that in Form_Paint ?? Like this ...
_____________________
Private Sub Form_Load()
   
    ' random order for button captions on buttons
    ShuffleButtons
   
    ' all buttons visible for start
    cmdRegister(0).Visible = True
    cmdRegister(1).Visible = True
    cmdRegister(2).Visible = True
   
End Sub

Private Sub Form_Paint()
    If(cmdRegister(0).Caption = "Register Now") Then
        cmdRegister(0).SetFocus
    ElseIf(cmdRegister(1).Caption = "Register Now") Then
        cmdRegister(1).SetFocus
    Else
        cmdRegister(2).SetFocus
    End If

End Sub
____________________
0
 
Harisha M GCommented:
Please tell me if it works.. I don't think you looked at Form_Paint at all...

If so, please change the grade !!
0
 
Harisha M GCommented:
I told to use Form_Paint...

> Ohhh.. GOT IT

> You can't set focus when the form is not loaded at all....
> i.e, you can't use that in Form_Load

> Use in Form_Paint instead
 
  ^^^^^^^^^^^^^^^^^^
          Here
0
 
bantamb20Author Commented:

Thanks for the info about the method not being usable in the Form_Load sub.
I'd forgotten about that completely
So this works as soon as the cursor is moved . . .it's better than not workign at all. . .

' *********************************************************
' ** mousemove on form changes focus on command button
' *********************************************************
Private Sub Form_MouseMove(Button As Integer, _
                      Shift As Integer, X As Single, Y As Single)
                     
Dim cmdButt As CommandButton

    For Each cmdButt In cmdRegister()
        If (cmdButt.Caption = "Register now") Then cmdButt.SetFocus
    Next
     
End Sub
0
 
Harisha M GCommented:
Private Sub Form_Paint()
    Dim cmdButt As CommandButton
    For Each cmdButt In cmdRegister()
        If (cmdButt.Caption = "Register now") Then cmdButt.SetFocus
    Next
End Sub
0
 
bantamb20Author Commented:
OK . . . . I have finally understood the Form_Paint significance.

Thank you

I can get rid of the mouseover

 
0
 
Harisha M GCommented:
Can you change the grade ??!
0
 
bantamb20Author Commented:
Sure I goofed, but how to change it?
0
 
Harisha M GCommented:
0
 
bantamb20Author Commented:

Make that an A

Certainly worth it.
0
 
bantamb20Author Commented:

Yes indeed, an A is the call . . .
0
 
Harisha M GCommented:
Thanks RomMod

Thank you bantamb20
0

Featured Post

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.

  • 14
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now