Hi everyone

I am new to the world of VB
Actually I wanted to know if anyone knows that how to validate option button
For example I create 3-4 option button(radio) and would like to check if the user has clicked on any of the button and then it should proceed further to the next form

Pls help me out!!

Thanks

Dani AI

Generated

A few solid ideas already from , , and . A small, practical improvement is to centralize the “is anything selected?” check into one helper you call before moving on. That keeps each option's Click handler thin (or absent) and makes validation consistent. Below are two compact approaches — one for classic VB6 and one for WinForms (VB.NET) — plus a few short tips.

' VB6: return the Name of the checked OptionButton inside a Frame, or "" if none
Function GetCheckedOptionName(f As Frame) As String
    Dim ctl As Control
    For Each ctl In f.Controls
        If TypeName(ctl) = "OptionButton" Then
            If ctl.Value = True Then
                GetCheckedOptionName = ctl.Name
                Exit Function
            End If
        End If
    Next
    GetCheckedOptionName = ""
End Function

' usage
Dim sel As String
sel = GetCheckedOptionName(Frame1)
If Len(sel) = 0 Then
    MsgBox "Please select an option before continuing", vbExclamation
Else
    ' proceed using sel (e.g. map Name or Tag to a next step)
End If
' VB.NET (WinForms): LINQ makes this concise (requires Imports System.Linq)
Dim chosen = GroupBox1.Controls.OfType(Of RadioButton)().FirstOrDefault(Function(r) r.Checked)
If chosen Is Nothing Then
    MessageBox.Show("Please select an option", "Validation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Else
    HandleChoice(CStr(chosen.Tag))   ' use Tag/Text to decide next step
End If

Practical tips: put each option set in its own container (Frame in VB6, GroupBox in WinForms) so groups don't interfere; store a numeric or enum value in the control’s Tag to avoid string switches; consider disabling the Next/OK control until a choice exists (as suggested) and make one sensible default choice if your flow allows it. These patterns keep logic reusable and make validation simple and testable.

Recommended Answers

All 4 Replies

make an array of option buttons by copying / pasting to a form, you will be asked if you want to make a control array, say yes! then when the time comes to check, just use a loop to go through the array and see which one is true! :)

You name the radio buttons all the same thing... That creates a control Array...
then you could do something like this:

for I = 0 to optionbuttonname.count - 1
     if optionbuttonname(I).value = true then
          msgbox "option: " & I & " Is Selected!"
     end if
next I

Hope that helps a little.

Hi!

At first follow this steps:
1. Create a new Form as Form1.
2. Add two Option control on the form called Option1 and Option2.
3. Add a Command button called Command1
4. Add two forms called Form2 and Form3 from Project->Add Form.

It 'll look like this:

|-------------------------------------|
|Form 1 |
|-------------------------------------|
| |
|o Option1 |
| |
|o Option2 |
| |
| ___________ |
| | Command1 | |
| ------------ |
| |
|-------------------------------------|

5. Make a double click on the Command1 button
6. Inside there write the following:

If Option1.Value = True Then
//this first If shows you Form2
Form2.Show
ElseIf Option2.Value = True Then
//this second If shows you Form3
Form3.Show
End If

And that's all.

P.D. I sent you a picture "Project.jpg" which is the form.

Andrés Rodríguez

Ummm .... wouldn't it be much easier to have a Click event for each radio button so that if any button is clicked, then the next step is taken.

For example, you initialise the "OK" button to disabled. For each click event, you set the OK button to enabled.

And rather than the naming of the buttons themselves, I would assign each button a GroupName so that you can refer to the group more easily.

Alternatively, if button 1 clicked sends you down a particular path different from the path taken if the user clicks on button 2, then just use the Click even of each button to initiate the needed function.

Seems easier to me, anyway.

Regards,


Gravey

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.