Results 1 to 6 of 6

Thread: make sure each grp box has a rad or chk selected

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2018
    Posts
    7

    make sure each grp box has a rad or chk selected

    Hi I am new so I hope I am posting this in the right area.
    I am in school and taking a Visual Basic class with visual studio 2012. I am working on a project for school and have been doing more then needed for this just because I like to learn. I have been able to figure out everything from the book and reading the notes on the program but I am stumped here. I have a forum with group box like( grp.sat, grpGeo, grpAlumni and a few more. I want to put in with datamissing that it checks the group box for a selection if not then the message is shown

    This is the function I have made and it works so far

    Function DataMissing() As Boolean
    If (txtName.Text = "") And (Not IsNumeric(txtGPAin.Text) And
    Return False
    Else
    Return True
    End If
    End Function


    This is the response if Ture or False

    If Not DataMissing() Then
    lstScore.Items.Clear()
    MessageBox.Show("There is Information missing please check over the application", "Enter Info")
    txtGPAin.Focus()

    Else
    totaltop = Math_Top(totaltop)
    totalbottom = Math_Bottom(totalbottom)
    admition = admition_status(total, totaltop, totalbottom, gpa)
    Application(name, total, admition, totaltop, totalbottom, gpa)
    End If


    I have a feeling this will be simple to someone and I am just over thinking it or missing a word when I do the grp. in the datamissing.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: make sure each grp box has a rad or chk selected

    I don't see anything at all to do with GroupBoxes there. Is there supposed to be something or have you not done anything at all in relation to this?

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2018
    Posts
    7

    Re: make sure each grp box has a rad or chk selected

    Quote Originally Posted by jmcilhinney View Post
    I don't see anything at all to do with GroupBoxes there. Is there supposed to be something or have you not done anything at all in relation to this?
    Sorry I was doing this at work.

    Code:
     ' This is where the top half is calculated for the application Tag is the holder for the number that is assigned to each radial button 
        Function Math_Top(totaltop As Double) As Double
            Dim rad As RadioButton
            For Each rad In grpSat.Controls.OfType(Of RadioButton)()
                If rad.Checked Then
                    totaltop += rad.Tag
                End If
            Next
            For Each rad In grpSchool.Controls.OfType(Of RadioButton)()
                If rad.Checked Then
                    totaltop += rad.Tag
                End If
            Next
            For Each rad In grpDifficulty.Controls.OfType(Of RadioButton)()
                If rad.Checked Then
                    totaltop += rad.Tag
                End If
            Next
            Return totaltop
        End Function
    
    
    
        ' Bottom Half of application 
        Function Math_Bottom(totalbottom As Decimal) As Decimal
            Dim rad As RadioButton
            Dim chk As CheckBox
            For Each chk In grpGeo.Controls.OfType(Of CheckBox)()
                If chk.Checked Then
                    totalbottom += chk.Tag
                End If
            Next
            For Each chk In grpAlumni.Controls.OfType(Of CheckBox)()
                If chk.Checked Then
                    totalbottom += chk.Tag
                End If
            Next
            For Each chk In grpLeadership.Controls.OfType(Of CheckBox)()
                If chk.Checked Then
                    totalbottom += chk.Tag
                End If
            Next
            For Each rad In grpEssay.Controls.OfType(Of RadioButton)()
                If rad.Checked Then
                    totalbottom += rad.Tag
                End If
            Next
            For Each rad In grpMisc.Controls.OfType(Of RadioButton)()
                If rad.Checked Then
                    totalbottom += rad.Tag
                End If
            Next
            ' This keeps the bottom half at 40 even if the person picks more then 40 points.
            If totalbottom > 40 Then
                totalbottom = 40
            End If
            Return totalbottom
        End Function

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: make sure each grp box has a rad or chk selected

    So, you already have the code to check each group, because you are looping through the controls in the code you show. If you want to check each group, you could have a method that takes a GroupBox as an argument, loops through all the RadioButtons in the group, and return whether any is True. Looks like you've already written all pieces of that, so there isn't any point in me writing it again.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2018
    Posts
    7

    Re: make sure each grp box has a rad or chk selected

    Quote Originally Posted by Shaggy Hiker View Post
    So, you already have the code to check each group, because you are looping through the controls in the code you show. If you want to check each group, you could have a method that takes a GroupBox as an argument, loops through all the RadioButtons in the group, and return whether any is True. Looks like you've already written all pieces of that, so there isn't any point in me writing it again.

    OK so I think I am looking at this in the wrong way. When I created a true/false to this if one it checked or nothing I get a error box for how ever many radio buttons in the group.


    Code:
    For Each rad In grpSat.Controls.OfType(Of RadioButton)()
                If rad.Checked = True Then
                    totaltop += rad.Tag
                Else
                    MessageBox.Show("ERROR")
                End If
            Next
    This is my biggest down fall with this call. When we have a project I want to make the program the way I think it should work and read info on other options like the For Each and learn how they work. But then I get to points like this and I looking at it that I miss the simple things.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: make sure each grp box has a rad or chk selected

    You can't display the error message until after the loop is complete because you can't know until then that no RadioButton is checked. Declare a Boolean variable and initialise it to False. Loop through the RadioButtons and when you encounter one that is checked, set your 'totaltop' variable as you are and also set the aforementioned Boolean variable to True and then EXIT THE LOOP. The reason you exit the loop is that you can't learn anything else useful once you have found a checked RadioButton. You know for a fact that all the rest must be unchecked so what's the point in testing them? At the end of the loop, you test your Boolean variable and if it's still False then you know that no checked RadioButton was found so you know that you need to display an error message.

    Another option is to use more LINQ to make your code more succinct again:
    vb.net Code:
    1. Dim option = grpSat.Controls.OfType(Of RadioButton)().SingleOrDefault(Function(cb) cb.Checked)
    2.  
    3. If option Is Nothing Then
    4.     'No RadioButton is checked.
    5. Else
    6.     'option is the RadioButton that is checked.
    7. End If
    SingleOrDefault will return the first item that matches the specified condition or Nothing if there are no matches, or throw an exception if there are multiple matches. The methods First, Single, FirstOrDefault and SingleOrdefault are all similar but slightly different.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width