1 Attachment(s)
[RESOLVED] Problem with Option button
Hi,
I attached my form as attachment herewith. It has one option button. In run time I load another 3 option button.
In run time please click command1 which will create three more option buttons.
My problem is,
run the form;
select the option1
now click on command1 which will create 3 more option buttons
but it removes the selection in option1(0). I mean the first option button is unchecked.
how to fix this problem?
Re: Problem with Option button
Putas the last command in your button click event.
Re: Problem with Option button
Maybe you want it to work with the first option selected or not, if so you can save its state before in a boolean variable and then update its state at the end, using that variable.
VB Code:
Private Sub Command1_Click()
Dim I As Integer, lFirstChecked As Boolean
lFirstChecked = Me.Option1.Item(0).Value
For I = 1 To 3
Load Option1(I)
With Option1(I)
.Left = Option1(I - 1).Left
.Top = Option1(I - 1).Top + 357
.Value = False
.Visible = True
End With
Next I
Me.Option1.Item(0).Value = lFirstChecked
End Sub
Re: Problem with Option button
hmmm. I think I haven't stated my problem here properly. The code I posted is a sample. But in my project I will create n number of controls at any time. and I do not know which control is selected.
Any way I got an idea. so, I will be going through all controls to make sure which one is selected and then load additional control and check the selected control again.
Thanks for your inputs
Re: Problem with Option button
You can use an Integer and Option1_Click event. Since Option1 is a control array, you will get "Index" when one of theme is clicked. Here's what i'm talking about:
VB Code:
Option Explicit
[B]Private selected As Integer[/B]
Private Sub Command1_Click()
Dim I As Integer
For I = 1 To 3
Load Option1(I)
With Option1(I)
.Left = Option1(I - 1).Left
.Top = Option1(I - 1).Top + 357
.Value = False
.Visible = True
End With
Next I
Option1([B]selected[/B]).Value = True
End Sub
Private Sub Option1_Click([B]Index[/B] As Integer)
[B]selected = Index[/B]
End Sub
1 Attachment(s)
Re: Problem with Option button
The problem is the first created instance inherits the Value property of index 0 instance, which is True, so it becomes the selected option until .Value = False line in with structure is reached. At which point both option buttons have value = false. 2nd and 3rd option buttons then inherit instance index 0 value property of false.
You don't have to iterate through the entire control array... just save the value property of instance at index 0 and reset it after loading new instances like what jcis did