简体   繁体   中英

Visual Basic multiple if functions at once

I have 10 Group boxes with 5 Radio Buttons in each of them. All 10 Group Boxes have the same options (Yes, No, Maybe, Definitely, and Never).

I'd like to check all the group boxes for the Radio button selected. So I'm running a For loop to do the check.

My question is, can one do one If function to check if the "Yes" Radio button is selected in all 10 Group Boxes?

My For loop definitely loops through all 10 Group Boxes. I'm happy with that.

I however have an issue with the following:

If (Yes.Checked) And (Yes1.checked) Then
'Do something here
End If

Unfortunately this code only works if Both Group Boxes has the "Yes" Radio Button selected.

What can I do to check every group Box, an add to a variable if it is selected?

Ideally I'd like to run 5 If unctions, and not 50.

I had a similar issue and this is how I solved it:

Create a specific type for each one of your responses and then assign that type to each option. For example you could create the group that holds the radio buttons like this:

Public Class groupPanel
  Inherits Panel
  Public Sub New()
  End Sub
End Class

Then you would create the variable like this:

Private group as new groupPanel()

Then, create a specific type for the "yes" response like this:

Public Class yesRadioButton
  Inherits System.Windows.Forms.RadioButton
  Public Sub New()
  End Sub
End Class

Then you continue to do the same thing for each on (one for "no", one for "maybe", and so on like this:

Public Class noRadioButton
  Inherits System.Windows.Forms.RadioButton
  Public Sub New()
  End Sub
End Class

Then when you are creating the group, you assign the specific type to the option they belong to. For example:

Private yes_button as new yesRadioButton()
yes_button.parent = groupPanel
Private no_button as new noRadioButton()
no_button.parent = groupPanel

and so on

Then you can simply iterate through each one in the group and count how many there are.

For each g as control in Main

   If TypeOf g Is groupPanel Then

      For each rb as control in g

         If TypeOf rb is yesRadioButton then

           ' add one to the "yes" counter

         End if

         ElseIf TypeOf rb is noRadioButton then

           ' add one the "no" counter

         End if

         ' and so on until you check all options

      Next

   End if

Next 

Then you have everything contained in a single loop

My solution may sound very similar to what @user2721815 posted, but it is actually very different. I think there is no point in creating an inheritance tree and then loop through controls, because you could already do this before. So you could, for example, iterate through your radio buttons and get results by radio button text.

A better approach would be to use inheritance to expose a {NotSelected, Yes, No, Maybe, Definitely, and Never} enum on the custom panel class, as a property. Create a radio button descendant and expose its enum value through a property as well. This way if a radio button is checked, you know exactly which enum value it corresponds to. I left the NotSelected option above to be default value. Please make sure every custom radio button has a value populated in UI designer.

Next step - handle all of your special checkboxes' checked event in a single handler in your custom panel class and set its selected enum value (let the property name be .SelectedOption ). You are pretty much done with the wiring at this point.

Now in your main program, at any given point in time, go through your custom panels and determine which ones have .SelectedOption = SelectedOptions.Yes . This approach is very flexible, because it does not restrict you to a number of panels on your form. You are also flexible with the choice of options - some panels may not have all the options and everything will still work fine.

If you need some code for better understanding of this approach, please feel free to comment underneath.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM