Link to home
Start Free TrialLog in
Avatar of mcanepa
mcanepa

asked on

EASY Question about OptionButton

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)?
Avatar of GolfNut
GolfNut

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
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.
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.
ASKER CERTIFIED SOLUTION
Avatar of Netminder
Netminder

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial