Quick Question...

Hi all

I have a progrma that i am writing where the input is based on radio buttons.

The radiobuttons are spereated into 3 groupboxes, only the first groupbox has its visible property set to true. Once 1 groupbox has had a radiobutton selected i wish for the next groupbox to become visible and so on.

Now i have written a private sub to do this, but i am unsure of where is the best place to call it?


Comments

  • seancampbellseancampbell Pennsylvania, USA
    There are so many different ways to accomplish, no one of them is really the "best" but some better then others for sure.

    Personally, I would write a routine like this:
    [code]
    Public Sub RadioEntryValidator()
    'This Routine shows or hides the group boxes based on radio buttons
    'GroupBox1 - has RadioButton1, has RadioButton2
    'GroupBox2 - has RadioButton3, has RadioButton4
    'GroupBox3 - has RadioButton5, has RadioButton6

    If RadioButton1.Checked = True Or RadioButton2.Checked = True Then
    GroupBox2.Visible = True
    else
    GroupBox2.Visible = False
    End If

    If RadioButton3.Checked = True Or RadioButton4.Checked = True Then
    GroupBox3.Visible = True
    Else
    GroupBox3.Visible = False
    End If

    End Sub
    [/code]

    On Form_Load I would set all of my RadioButton.Checked values to false

    Then on all of my RadioButton.Click events, I would call the RadioEntryValidator routine.

    That is how I would handle it for something simple, like I said there are several ways to achieve this, so you may have found another way to do it that works for you. The important part is to do what works for you, since you will likely be the only one working on the code.

    hope this helps!
    Sean cAmpbell
  • Yeah that does help as it was what i first thought of using..

    Here is what i eventually decided upon..

    I call this routine on the checkedChanged event of each radiobutton
    [code]
    Public Sub CheckSizeSelected()
    Dim Ctrl As Control
    Dim MyRDB As RadioButton

    For Each Ctrl In gpb_BaseSize.Controls
    Select Case TypeName(Ctrl)
    Case "RadioButton"
    MyRDB = CType(Ctrl, RadioButton)
    If MyRDB.Checked = True Then
    Me.gpb_BaseType.Visible = True
    End If
    End Select
    Next

    End Sub[/code]
  • seancampbellseancampbell Pennsylvania, USA
    Awesome, you got yourself a function that doesn't care about how many radio buttons are in a groupbox

    Personally I would exit the For Loop after you find a checked radio button, but thats just me

    good work ^_^
    sean c - firesickle.com
  • Yeah the code still needs refining, i prefer to just write it all out as it comes to me and then refine bits later..

    i'm having an issue with this section of code

    [code] Private Sub btn_Confirm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Confirm.Click

    Dim ChkBox As CheckBox

    For Each ChkBox In Me.gpb_Topping.Controls
    If ChkBox.Checked = True Then
    ToppCount = ToppCount + 1
    End If

    Next

    If ToppCount = 2 Then 'Or ToppCount = 5 Then
    MsgBox("Yay")

    Else
    MsgBox("No")
    End If
    End Sub
    [/code]

    I have 9 checkboxes in a groupbox, i wish for only between 2 and 5 to be selected. I know the code at present is only to allow two, i am just getting it workingwith this for now.

    The idea behind the code was that i want to cycle through all the checkboxes in the specified groupbox, and add 1 to an integer holding the amount of checkboxes ticked (toppcount)

    I'm sure it's a simple mistake but i've been looking at it for ages now...
  • seancampbellseancampbell Pennsylvania, USA
    [code]

    Private Sub btn_Confirm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Confirm.Click

    Dim ToppCount As Integer = 0
    Dim ChkBox As CheckBox

    For Each ChkBox In Me.gpb_Topping.Controls
    If ChkBox.Checked = True Then
    ToppCount = ToppCount + 1
    End If
    Next

    If ToppCount >= 2 And ToppCount <= 5 Then
    MsgBox("Yay")
    Else
    MsgBox("No")
    End If
    End Sub
    [/code]

    Is that what you mean? or are you saying that you want to check all checkboxes across multiple GroupBoxes?
    In that case:
    [code]

    Dim ToppCount as Integer

    Sub countCheckedBoxes(ByRef GB as GroupBox)
    Dim ChkBox As CheckBox

    For Each ChkBox In Me.GB.Controls
    If ChkBox.Checked = True Then
    ToppCount = ToppCount + 1
    End If
    Next
    End Sub

    Sub chkGroupBoxes()
    'Must set ToppCount to 0 before we check
    ToppCount = 0

    countCheckedBoxes(gpb_Topping)
    countCheckedboxes(gpb_Topping2)
    countCheckedBoxes(gpb_Topping3)

    If ToppCount >= 2 and ToppCount <= 5 Then
    MsgBox("Yea")
    Else
    MsgBox("Nay")
    End If

    End Sub

    [/code]

    The second set of code isn't the best way to accomplish this, leaves some room for errors in the future, if that is close to what you wanted, we can expand it and get it better for you
  • Yeah thats pretty much it

    In this instance, i have a groupbox name gpb_topping which contains 9 Checkboxes..

    There is also a confirm button, and in the click of this event i want to make sure that atleast two checkboxes have been ticked, but no more than 5.

    I then need to find out if a certain checkbox has been ticked as calculations are slightly different if certain checkbox has been selected
  • seancampbellseancampbell Pennsylvania, USA
    I cannot really know exactly what you need to do with the infroamtion you have provided me, but here is a sub routine that will return an integer of how many checkboxes are checked in a GroupBox

    [code]
    Sub Button_click_code()
    Dim I as integer = evaluateChkBxsInGrpBox(gpb_topping)

    If I >= 2 And I <= 5 Then
    MsgBox("Between 2 and 5")
    Else
    MsgBox("Not Between 2 and 5")
    End If
    End Sub

    Public Function evaluateChkBxsInGrpBox(ByRef GB as GroupBox) as integer
    Dim Counter as Integer = 0
    Dim ChkBox As CheckBox

    For Each ChkBox In GB.Controls
    If ChkBox.Checked = True Then
    Counter = Counter + 1
    End If
    Next
    Return Counter
    End Function

    [/code]

    heres an example of using different formulas based on which check boxes are checked:
    [code]

    Sub Test()
    'An example of checking if a specific checkbox is checked:
    If CheckBox1.Checked = True Then
    'Formula
    ElseIf CheckBox2.Checked = True Then
    'Different Formula
    ElseIf CheckBox3.Checked = True Then
    'Different Formula
    End If

    End Sub

    [/code]

    Hope this helps,
    S_campbell
  • Cheers for your reply but I've sorted it now.. i can psot the code if you'd like to see but essentially the results are the same..thanks for all your help though
  • seancampbellseancampbell Pennsylvania, USA
    No problem, no need to post the code, I was only interested in helping you get yourself in order.

    Happy coding!!
    Sean C
    firesickle.com
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion