Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Option Button Index

Status
Not open for further replies.

davman2002

Programmer
Joined
Nov 4, 2002
Messages
75
Location
US
How can I get the index of an option button control array, if no option has not been selected. Currently I know how to get the index of the control array based on what option was selected. but my current way means that the user must click on an option. This poses a probelm for form validation.

my code is

Private Sub optPhy_Click(Index As Integer)
'
'Get Index value of the option Selected
'
intPhyIndex = Index

End Sub
 
Hi there,

what is it you are trying to do? If no option has been selected what do you want to happen? I usually set up my otion groups so that one is always selected by default. When you load your form up, set one to be selected.

Transcend
[gorgeous]
 
It sounds like you want to get the Index value of a button in a control array when no button has been pressed. If that is the case, what would be the determining factor of which button is the correct button whose index value you seek. Perhaps you could search thru the control array looking for a certain Caption value to find the button, or do you have something else in mind to choose a specific button? Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
My goal is to validate that the user has selected an option. Currently my company does not want to have a default value for these option buttons. They want to make sure that the user has selected an option
 
Ok so you do something like ...

If (optOption(0).Value = false) and (optDrawingDate(1).Value = false) Then
'no options selected
else
'do something else
end if

Transcend
[gorgeous]
 
hmm make that

If (optDrawingDate(0).Value = false) and (optDrawingDate(1).Value = false) Then
'no options selected
else
'do something else
end if


so they are both actually the same :D

Transcend
[gorgeous]
 
or in your case optPhy_Click(0) etc etc

another way would be (haven't tested this so sorry if its not quite right) also this will only work if you only have the one group of controls on the form. Otherwise you'd need to add a frame or something...

dim cntl as control
dim blnSeleced as boolean

blnselected = false

For Each cntl In me.Controls
if typeof cntl is OptionButton
If cntl.Value = true Then
blnSelected = true
End If
end if
next cntl

if blnselected = true then 'one has been picked

Transcend
[gorgeous]
 
Geez, and I was think it was an array of command buttons - think its time to call it a night.

I think Transcend has a good approach, but I would add a check on the Name property to insure that you're only looking at the buttons in the right group.
Code:
For Each cntl In me.Controls
   If (TypeOf cntl is OptionButton) Then
      If (cntl.Name = "optPhy") Then
         If (If cntl.Value = True) Then
            blnSelected = true
         End If
      End If
   End If
Next
Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
I agree with CajunCenturion
if you just go by the fact that it is a OptionButtons then you'll be introuble if you have more than 1 group of option buttons.
make sure you set blnSelected to False before entering the For loop
 
I did already mention this fact
"also this will only work if you only have the one group of controls on the form" otherwise you need to alter it

Transcend
[gorgeous]
 
oh .. my hurting head
i meant one group of OptionButtons

sorreh! :)
Transcend
[gorgeous]
 
I think the most easy way would be (a variation of) this:

Dim bSelected As Boolean
Dim n As Integer

For n = 0 To optXX.Count - 1
If optXX(n).Value Then Exit For
Next n

bSelected = n < optXX.Count


Greetings,
Rick
 
LaxyMe - That approach will only work if the OptionButton are contiguous, and begin at 0, which is not required.

Its perfectly legal to have three Option Buttons in a group, with Indexes 10, 20, and 30. Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Yes, that's true. But since you as a developer are responsible for creating them, it's something you could keep in mind when doing so.
Greetings,
Rick
 
Yeah but if you have heaps and a couple get deleted do you always go through them and renumber the indexes ?

I like to but admit I don't always get to do this.

Transcend
[gorgeous]
 
Well, usually I have those indexes numbered with constants and they can be renumbered quite easily. An renumbering them on the form is quite easy too. Although I have to admit that mostly it's not that some get deleted, usually it's because more get added...
Greetings,
Rick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top