EASY Question about OptionButton

mcanepa
mcanepa used Ask the Experts™
on
I have a form with 3 option buttons: (ie.)
1 = Day Shift
2= Night Shift
3= Swing Shift

How do I get the shift that was chosen to appear in a ListBox (after the click event)?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Are you wanting to add the shift that was chosen to a listbox or are you wanting to highlight in the listbox the selected item?  
In either case, I'd have to ask why you are doing this?
In the Option1_click (same for Option2, Option 3, etc.

    If Option1.Value = True Then
       
        List1.AddItem Option1.Caption
       
    End If
However, this isn't the right thing to do.  You will ALWAYS add the caption to the listbox whether or not it is there.   Also, consider making the option buttons a control array (make the first one, then copy/paste it and when it asks you if you wnat to create a control array, say yes).  That way, the code changes like (one place handles all the code):

Private Sub Option1_Click(Index As Integer)
    If Option1(Index).Value = True Then
       
        List1.AddItem Option1(Value).Caption
       
    End If

End Sub


first of all you need not have list box.
you can work with text box.
Make array of option button. then in the click event write code
text1.text=option1(index).caption

if you want to work with list box
in the form load event write code
list1.addItem("Day shift")
list1.addItem("Night shift")
list1.addItem("Swing shift")

and in the click event write code
list1.text=option1(index).caption
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

This is the same code I wrote except it uses a textbox.  Plus, it doesn't take into account multi-line in the textbox (I don't see vbcrlf on the end of each string, so each caption will be smashed up against the previous one).
the difference is i have made all entries on form load
in your code everytime the option_click event occurs a new strign will be added to the list box
I see we are interpreting the question differently.  I took it to mean "When I click on an option button, add that item to a listbox".  I think you took it to mean "When I click on an option button select the preloaded item in the listbox that corresponds to the option selected".  I can see both applications of the question to be correct.

Commented:
Here's an interpretation of the suggestions already offered.  Hope this helps.

Form Layout:
     3 Options buttons
           OptionShift(0)'Caption=Day Shift,
           OptionShift(1)'Caption=Night Shift           OptionShift(2)'Caption=Swing Shift     1      List Box
           listShift

Code:

Private Sub Form_Load()
    'Form load initializes the option box using captions
    'from the option buttons.
    'Loading the list box values at run time allows for
    'less maintenance if the option button captions are
    'changed in the future
   
    'Declare Local Variables
        Dim integerIndex As Integer
        Dim integerEndCount As Integer
   
    'Initialize Variables
        integerEndCount = optionShift.Count - 1
   
    'Code
        'Load the list index with captions corresponding
        'to the captions for the option buttons
        For intIndex = 0 To integerEndCount Step 1
            listShift.AddItem optionShift(intIndex).Caption
        Next intIndex

End Sub

Private Sub listShift_Click()
    'Sync option button selection with list box selection
    'if user clicks in listbox
    optionShift(listShift.ListIndex).Value = True
End Sub

Private Sub optionShift_Click(Index As Integer)
    'Sync list selection with option button selection
    listShift.ListIndex = Index
   
End Sub

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in Community Support that this question is:
- refund and close
Please leave any comments here within the
next seven days.
Per recommendation, points refunded and question closed.

Netminder
EE Admin

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial