Results 1 to 10 of 10

Thread: Switching optionbuttons – how to make it a correct

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2005
    Location
    in Poland
    Posts
    390

    Switching optionbuttons – how to make it a correct

    Hi All

    I write small app in a VBA. I have on Form a four an OptionButton. I want now to make that after switched they changed BackColor and ForeColor.

    in this way I do it but this looks on a bad.
    VB Code:
    1. Private Sub optCztery_Click()
    2. If optCztery.Value = True Then
    3. optCztery.BackColor = &HC0FFFF
    4. optCztery.ForeColor = &HFF0000
    5. optTrzy.BackColor = 10931133
    6. optTrzy.ForeColor = &H4080&
    7. optTrap.BackColor = 10931133
    8. optTrap.ForeColor = &H4080&
    9. optProst.BackColor = 10931133
    10. optProst.ForeColor = &H4080&
    11. CommandButton4_Click
    12. End If
    13. End Sub
    14.  
    15. Private Sub optProst_Click()
    16. If optProst.Value = True Then
    17. optProst.BackColor = &HC0FFFF
    18. optProst.ForeColor = &HFF0000
    19. optTrzy.BackColor = 10931133
    20. optTrzy.ForeColor = &H4080&
    21. optTrap.BackColor = 10931133
    22. optTrap.ForeColor = &H4080&
    23. optCztery.BackColor = 10931133
    24. optCztery.ForeColor = &H4080&
    25. CommandButton4_Click
    26. End If
    27. End Sub
    28.  
    29. Private Sub optTrap_Click()
    30. If optTrap.Value = True Then
    31. optTrap.BackColor = &HC0FFFF
    32. optTrap.ForeColor = &HFF0000
    33. optTrzy.BackColor = 10931133
    34. optTrzy.ForeColor = &H4080&
    35. optProst.BackColor = 10931133
    36. optProst.ForeColor = &H4080&
    37. optCztery.BackColor = 10931133
    38. optCztery.ForeColor = &H4080&
    39. CommandButton4_Click
    40. End If
    41. End Sub
    42.  
    43. Private Sub optTrzy_Click()
    44. If optTrzy.Value = True Then
    45. optTrzy.BackColor = &HC0FFFF
    46. optTrzy.ForeColor = &HFF0000
    47. optProst.BackColor = 10931133
    48. optProst.ForeColor = &H4080&
    49. optTrap.BackColor = 10931133
    50. optTrap.ForeColor = &H4080&
    51. optCztery.BackColor = 10931133
    52. optCztery.ForeColor = &H4080&
    53. CommandButton4_Click
    54. End If
    55. End Sub
    how to make this better, maybe in this some a loop?

    Thanks in advance
    Tamgovb
    I know, I know, my English is bad, sorry .....

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: Switching optionbuttons – how to make it a correct

    Well, since the code in the If statements is the same, put it in a separate sub and call it from each option button's click event.
    Tengo mas preguntas que contestas

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2005
    Location
    in Poland
    Posts
    390

    Re: Switching optionbuttons – how to make it a correct

    thanks you for reply.

    Note, that I can't make so. Because those OptionButtons each time are there other (have a look on their name) the array of controls would be the best solution, but there is no something like this in a VBA.

    I have Win98 and Office2000
    thanks
    tamgovb
    I know, I know, my English is bad, sorry .....

  4. #4
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: Switching optionbuttons – how to make it a correct

    VB Code:
    1. Private Sub optCztery_Click()
    2. If optCztery.Value = True Then
    3.    SwitchColors
    4. End If
    5. End Sub
    6.  
    7. Private Sub optProst_Click()
    8. If optProst.Value = True Then
    9.    SwitchColors
    10. End If
    11. End Sub
    12.  
    13. Private Sub optTrap_Click()
    14. If optTrap.Value = True Then
    15.    SwitchColors
    16. End If
    17. End Sub
    18.  
    19. Private Sub optTrzy_Click()
    20. If optTrzy.Value = True Then
    21.    SwitchColors
    22. End If
    23. End Sub
    24.  
    25. Private Sub SwitchColors()
    26. optTrap.BackColor = &HC0FFFF
    27. optTrap.ForeColor = &HFF0000
    28. optTrzy.BackColor = 10931133
    29. optTrzy.ForeColor = &H4080&
    30. optProst.BackColor = 10931133
    31. optProst.ForeColor = &H4080&
    32. optCztery.BackColor = 10931133
    33. optCztery.ForeColor = &H4080&
    34. CommandButton4_Click
    35. End Sub
    Maybe I'm missing something? What's wrong with the above?
    You could loop through controls, checking for the ones you want as well.
    Tengo mas preguntas que contestas

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2005
    Location
    in Poland
    Posts
    390

    Re: Switching optionbuttons – how to make it a correct

    Thanks, sorry but.....you didn't understand me probably. I attached file and you have a look. Your solution can't work correctly because always it a refer to a different of group of objects.

    So therefore please you again, you have a look on the names of objects - always are other

    thanks
    Attached Files Attached Files
    I know, I know, my English is bad, sorry .....

  6. #6
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: Switching optionbuttons – how to make it a correct

    Ok, I didn't realize this was Excel. I don't know as much about Excel, so take this for what it's worth.
    You may need to put Me. in front of your statements:
    VB Code:
    1. Me.optTrap.BackColor = &HC0FFFF
    For some things you may need to SetFocus before setting properties:
    VB Code:
    1. optTrap.SetFocus
    2. optTrap.BackColor = &HC0FFFF
    I'm not sure when you need to do one or the other. I think it depends on what property you're setting.
    Anyway, I can't test it on the file you sent because security settings won't let me change the code.
    Tengo mas preguntas que contestas

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2005
    Location
    in Poland
    Posts
    390

    Re: Switching optionbuttons – how to make it a correct

    Hi All

    Okay, I found here any code and I modified it
    An works it fine for me, but I have other problem now.

    I have a three of sections (frame) with the optionbutton controls. If I use this code
    then this code to make that only one option will be active and all other will be not active. It will be not correctly.
    My question is:
    how to make that those all of section would be it works completely of irrespective

    here's this code, it work with the objects (optionbutton) only in a one Frame.

    VB Code:
    1. Private Sub Highlight_Control(Optional MarkControl As Control)
    2. Dim cMyControl As Control
    3.  
    4.     For Each cMyControl In Me.Controls
    5.         If Left(cMyControl.Name, 3) = "Opt" Then
    6.  
    7.             With cMyControl
    8.  
    9.             .BackColor = 10931133
    10.             .ForeColor = &H4080&
    11.  
    12.             End With
    13.  
    14.         End If
    15.     Next cMyControl
    16.    
    17.     If Not MarkControl Is Nothing Then
    18.         With MarkControl
    19.           .BackColor = &HC0FFFF
    20.           .ForeColor = &HFF0000
    21.         End With
    22.     End If
    23. End Sub
    24.  
    25.  
    26.  
    27. '>>> for a optionbutton:
    28.  
    29. Private Sub optTrap_Click()
    30.     Highlight_Control optTrap
    31. End Sub
    32.  
    33. '>>>. . . . . .  etc, etc for each the optionbuttons in a this section

    thanks in advance
    Last edited by Tamgovb; Jun 20th, 2006 at 05:11 AM.
    I know, I know, my English is bad, sorry .....

  8. #8
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Switching optionbuttons – how to make it a correct

    Tamgovb
    You will need to change this lie
    VB Code:
    1. For Each cMyControl In Me.Controls
    to
    VB Code:
    1. For Each cMyControl In Me.Frame1.Controls
    or whatever the name of the frame is. This change means that the code now only loops throught the controls in frame1 and not all the controls on the form
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2005
    Location
    in Poland
    Posts
    390

    Re: Switching optionbuttons – how to make it a correct

    Hi All

    Thanks DKenny, but I about this knew and made so

    If you have three section (a three frames and in each it's after two, three or four optionbuttons)

    it will not be well to work, because active only one option will be. These are three independent section

    Look on attached images:

    Thanks
    Attached Images Attached Images  
    Last edited by Tamgovb; Jun 21st, 2006 at 04:19 AM.
    I know, I know, my English is bad, sorry .....

  10. #10
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Switching optionbuttons – how to make it a correct

    Try the following version of the Highlight_Control procedure. When calling it, you will need to include both the OptionButton and the Frame as parameters.

    e.g.
    VB Code:
    1. Private Sub OptionButton1_Click()
    2.     Highlight_Control Me.OptionButton1, Me.Frame1
    3. End Sub

    Here's the revised procedure.
    VB Code:
    1. Private Sub Highlight_Control(ByRef MarkControl As Control, ByRef OptFrame As Control)
    2. Dim cMyControl As Control
    3.    
    4.     If TypeName(OptFrame) <> "Frame" _
    5.     Or TypeName(MarkControl) <> "OptionButton" Then
    6.         Exit Sub
    7.     End If
    8.    
    9.     For Each cMyControl In OptFrame.Controls
    10.         If TypeName(cMyControl) = "OptionButton" Then
    11.  
    12.             With cMyControl
    13.                 .BackColor = 10931133
    14.                 .ForeColor = &H4080&
    15.             End With
    16.  
    17.         End If
    18.     Next cMyControl
    19.  
    20.     With MarkControl
    21.       .BackColor = &HC0FFFF
    22.       .ForeColor = &HFF0000
    23.     End With
    24.  
    25. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width