Visual Basic 多个 if 函数一次 [英] Visual Basic multiple if functions at once

查看:25
本文介绍了Visual Basic 多个 if 函数一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 10 个分组框,每个分组框有 5 个单选按钮.所有 10 个分组框都有相同的选项(是、否、可能、肯定和从不).

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).

我想检查所选单选按钮的所有组框.所以我正在运行一个 For 循环来进行检查.

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.

我的问题是,可以做一个 If 函数来检查是否在所有 10 个分组框中选择了是"单选按钮?

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

我的 For 循环肯定会遍历所有 10 个分组框.我对此很满意.

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

但是我有以下问题:

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?

理想情况下,我希望运行 5 个 If 函数,而不是 50 个.

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

推荐答案

我的解决方案可能听起来与@user2721815 发布的非常相似,但实际上非常不同.我认为创建继承树然后循环遍历控件是没有意义的,因为您之前已经可以这样做了.因此,例如,您可以遍历单选按钮并通过单选按钮文本获取结果.

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.

更好的方法是使用继承将自定义面板类上的 {NotSelected, Yes, No, Maybe, absolute, and Never} 枚举作为属性公开.创建一个单选按钮后代并通过属性公开其枚举值.这样,如果选中了单选按钮,您就可以确切地知道它对应于哪个枚举值.我将上面的 NotSelected 选项保留为默认值.请确保每个自定义单选按钮都在 UI 设计器中填充了一个值.

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.

下一步 - 在自定义面板类的单个处理程序中处理所有特殊复选框的已选中事件,并设置其选定的枚举值(让属性名称为 .SelectedOption).到这里你已经基本完成了布线.

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.

现在在您的主程序中,在任何给定的时间点,查看您的自定义面板并确定哪些面板具有 .SelectedOption = SelectedOptions.Yes.这种方法非常灵活,因为它不会将您限制在表单上的多个面板中.您还可以灵活选择选项 - 某些面板可能没有所有选项,但一切仍然正常.

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.

这篇关于Visual Basic 多个 if 函数一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆