在组框中选择的 VB.NET 默认单选按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2030974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB.NET default radio button selected inside a group box
提问by BP.
I have a WinForms application (VS 2008, .NET 3.5) that has a form with two different group boxes, and inside of each group box are different sets of radio buttons. When I run the application, the first group box automatically has the first radio button in it already selected, and the second group box does not have a radio button selected by default.
我有一个 WinForms 应用程序(VS 2008,.NET 3.5),它有一个带有两个不同组框的表单,每个组框内部是不同的单选按钮组。当我运行应用程序时,第一个组框会自动选择第一个单选按钮,而第二个组框默认没有选择单选按钮。
I have looked through all the properties of the radio buttons and the group boxes, and cannot figure out what the difference is between the two. I would like both group boxes to have all radio buttons unselected when the form is first opened.
我查看了单选按钮和组框的所有属性,但无法弄清楚两者之间的区别。我希望第一次打开表单时,两个组框都取消选择所有单选按钮。
Also, I looked through the Designer.vb file for the form, and could not find anything unusual going on in there either.
此外,我查看了表单的 Designer.vb 文件,也找不到任何异常之处。
回答by Hans Passant
Set all the buttons' AutoCheck property to False. You'll now have to write a Click handler for them to set their Checked property. A sample handler that takes care of two of them:
将所有按钮的 AutoCheck 属性设置为 False。您现在必须为他们编写一个 Click 处理程序来设置他们的 Checked 属性。处理其中两个的示例处理程序:
Private Sub RadioButton_Click(ByVal sender As Object, ByVal e As EventArgs) _
Handles RadioButton1.Click, RadioButton2.Click
Dim button As RadioButton = DirectCast(sender, RadioButton)
RadioButton1.Checked = button is RadioButton1
RadioButton2.Checked = button Is RadioButton2
End Sub
回答by HardCode
I've had this issue, too. I just manually set all RadioButton objects to .Checked = False in the Form_Shown event. Note that it has to be afterthe Form_Load event or it won't work, and the RadioButton will be set with a default.
我也遇到过这个问题 我只是在 Form_Shown 事件中手动将所有 RadioButton 对象设置为 .Checked = False 。请注意,它必须在Form_Load 事件之后,否则它将不起作用,并且 RadioButton 将设置为默认值。
Why? I don't know. Perhaps a bug in VB.NET.
为什么?我不知道。也许是 VB.NET 中的一个错误。
回答by Majavis
If your lowest Tab Index is a Radio Button, and you have AutoCheck set to True, then when the form loads and sets the active element to the lowest Tab Index, it acts as if you had clicked on the Radio Button, tripping the AutoCheck and therefore checking the Radio Button. Simply give another control on the form the Tab Index of 0.
如果您的最低标签索引是单选按钮,并且您将 AutoCheck 设置为 True,那么当表单加载并将活动元素设置为最低标签索引时,它的行为就像您单击了单选按钮,触发了 AutoCheck 并因此检查单选按钮。只需在表单上提供另一个控件 Tab Index 为 0。
回答by BP.
You need to give both radio groups different group names. That may not be your issue, but it's a possible reason.
您需要为两个无线电组指定不同的组名。这可能不是您的问题,但这是一个可能的原因。
I'm curious as to why you would want radios to default to having no value at all. Radios represent boolean values - True or False - there is no other valid state.
我很好奇你为什么希望收音机默认没有任何价值。Radios 代表布尔值 - True 或 False - 没有其他有效状态。
回答by rsolo__
RadioButton1.checked = False under Form_Activated works.
RadioButton1.checked = False 在 Form_Activated 下有效。
回答by WSR Michael
i had this problem, and making Autocheck=false, did the trick. and YES the RadioButton1 was TAB=0
我遇到了这个问题,并使 Autocheck=false 成功了。是的 RadioButton1 是 TAB=0
ThankYou
谢谢你
回答by Wells
Set Auto Check to "False" on all the radio buttons, both groups. Set them all back to "True". This worked for me. I also had to group boxes the one was good the second one came with the first button checked (selected).
在所有单选按钮上将自动检查设置为“假”,这两个组。将它们全部设置回“True”。这对我有用。我还必须对框进行分组,其中一个很好,第二个框选中(选中)第一个按钮。
回答by Arshdeep Singh Pannu
Just change the groupbox accessible role property from default to none It will work ??
只需将 groupbox 可访问角色属性从默认更改为无它就可以工作吗??

