VB and Radio Buttons

MTXR

[H]ard|Gawd
Joined
Nov 4, 2000
Messages
1,324
Alright here is my problem...

I can write a bunch of elseif statements to check if a radiobutton is checked etc....

But i just learned the Case statement and think this is a much more elegant way to do things.

I am checking to see which radiobutton is checked in my groupbox and then to check if the user input was over a certain number.

Basically checking to see which month it is and if the user entered an impossible number for the maximum number of hours in that month.

Here is some pseudocode: I just don't know how to get VB to check a certain list of radiobuttons in a groupbox... this is for a windows application btw..

Very new with this syntax stuff... i been searching for ever to find the solution, but if it comes down to it, i'll just use a bunch of if statements.

Help please or a point to a reference i can read? Thanks.

Code:
Select Case radiobuttongroup.checked

Case radBtn1, radBtn3, radBtn5, radBtn7, radBtn7, radBtn10, radBtn12

if Cint(userinput.text)  > 744 then 

messagebox.show("To many hours")

endif

Case radBtn2 

if Cint(userinput.text) > 672 then

messagebox.show("To many hours")

endif

Case radBtn4, radBtn6, radBtn9, radBtn11

if Cint(userinput.text) > 720 then

messagebox.show("To many Hours")

endif
 
Alright i guess i found out a way to do it without a crazy bunch of elseif statements.

I know there are some flaws... I havn't made a validation to check if the input is a decimal or not....

Code:
Dim intHours As Integer = CInt(txtUsageHours.Text)  'Text to integer for Usage Hours

Select Case intHours

            Case 673 To 720
                If radFebuary.Checked Then
                    MessageBox.Show("You Have Entered To Many Hours For the Month Selected")

                End If

            Case 721 To 744
                If radFebuary.Checked Or radApril.Checked Or radJune.Checked _
                Or radSeptember.Checked Or radNovember.Checked Then
                    MessageBox.Show("You Have Entered To Many Hours For the Month Selected")

                End If

            Case Is > 744
                MessageBox.Show("You Have Entered To Many Hours For the Month Selected")


        End Select
 
I think it would be better to use a control array for the radio buttons so you could access them with an index, like radMonth[4].Checked. This would allow you to use a loop to check all of them. Something like month zero would be January, and Month eleven would be December. Personally I never really liked select case. I haven't coded in VB since VB5 so my syntax will be off, but here's kind of what I'm talking about:

Code:
Dim intHours As Integer = CInt(txtUsageHours.Text)  'Text to integer for Usage Hours
Dim hoursInMonth = array(0=>744,1=>673,2=>744,blah,blah,blah)  // Array that stores the maximum hours for each month, starting with month zero, January.

For(i=0,i<12,i++)
{
   if(radMonth[i].Checked)
   {
      if(intHours > hoursInMonth[i])
      {
         MessageBox.Show("You have entered too many hours for the month selected.")
      }
   }
}

Also notice you used the wrong form of the word "to" in your message.
 
I think it would be better to use a control array for the radio buttons so you could access them with an index, like radMonth[4].Checked. This would allow you to use a loop to check all of them. Something like month zero would be January, and Month eleven would be December. Personally I never really liked select case. I haven't coded in VB since VB5 so my syntax will be off, but here's kind of what I'm talking about:

Code:
Dim intHours As Integer = CInt(txtUsageHours.Text)  'Text to integer for Usage Hours
Dim hoursInMonth = array(0=>744,1=>673,2=>744,blah,blah,blah)  // Array that stores the maximum hours for each month, starting with month zero, January.

For(i=0,i<12,i++)
{
   if(radMonth[i].Checked)
   {
      if(intHours > hoursInMonth[i])
      {
         MessageBox.Show("You have entered too many hours for the month selected.")
      }
   }
}

Also notice you used the wrong form of the word "to" in your message.

Wtbracketswagon? You have coded a C# for loop for him.
 
hmm, an array... i read something about that in my search for the radiobuttonlist.

I'll take a look at that...


which form of "to" do i need?

after testing my current code, it works... so i dunno...
 
which form of "to" do i need?
too

Arrays are very useful, even if it doesn't make a huge difference with the code you wrote, you will definitely want to learn how to use them for later.
 
oh wow. yeah i get it now... fixing the grammatical error now....
 
totally seeing your concept about arrays. I read up on them and used it. I made 2 arrays to handle the validation, its pretty sweet idea.

Thanks
 
totally seeing your concept about arrays. I read up on them and used it. I made 2 arrays to handle the validation, its pretty sweet idea.

Thanks

Hey no problem, glad you looked into it :D In the long run they are one of your best friends, I can't say the same for select case!
 
For Future Reference, is there a radiobuttonlist function or something if i have a bunch of radiobuttons in a group box?

Something like

radiobuttonlist1.checked to see which radiobutton in the group box was checked?
 
Back
Top