enable option buttons at runtime

N

natanz

i have a userform with two listboxes, and six optionbuttions. the
options buttons are split into 3 pairs of two. at the outset only the
first pair of option buttons is enabled. the following code tests to
see if more than one selection is made in a listbox, and if it is, then
enables a relevant pair of option buttons.

Private Sub Listbox1_change()
'check to see if there is a multiple selection
Dim i As Integer
Dim Acount As Integer
Acount = 0

With Userform1.Listbox1
For i = 0 To .ListCount - 1
If .Selected(i) Then
Acount = Acount + 1
End If
Next i
End With
If Acount > 1 Then
Userform1.OptionButton5.Enabled = True
Userform1.OptionButton6.Enabled = True
End If
Userform1.Repaint
End Sub

this code works fine, when i run the code, by stepping through it. In
fact i don't need the Userform1.Repaint line, when i run it this way.
But the appearance of the userform doesn't change when i run the code
normally. Is there some other trick to getting this change to happen?

thanks for your help.
 
C

Cindy M -WordMVP-

Hi Natanz,
this code works fine, when i run the code, by stepping through it. In
fact i don't need the Userform1.Repaint line, when i run it this way.
But the appearance of the userform doesn't change when i run the code
normally. Is there some other trick to getting this change to happen?
Tried sticking a DoEvents line in, after the Repaint line?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)
 
N

natanz

actually the answer is quite tricky. the problem with the code is that
it references the name of the userform, despite the fact that it is
within the userforms code window. here's what it should look like
(thanks to doug robbins)

Private Sub Listbox1_change()
'check to see if there is a multiple selection
Dim i As Integer
Dim Acount As Integer
Acount = 0


With Listbox1
For i = 0 To .ListCount - 1
If .Selected(i) Then
Acount = Acount + 1
End If
Next i
End With
If Acount > 1 Then
OptionButton5.Enabled = True
OptionButton6.Enabled = True
End If
End Sub
 
Top