vb.net 如何简化这个关于单选按钮和 numberupdown 控件的可视化基本代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18946567/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:10:40  来源:igfitidea点击:

How to simplify this visual basic code about radio buttons and numberupdown controls

vb.netradio-button

提问by KeithSmith

I have a group with a NumberUpDown control, 0-10 by 1 and 11 radio buttons (equal to the range of the NumberUpDown control). I have been able to write code in the events of the controls such that when a radio button is checked the value of the NumberUpDown changes to that of the radio button checked and when the NumberUpDown value is changed, the correct radio button is checked.

我有一个带有 NumberUpDown 控件的组,0-10 x 1 和 11 个单选按钮(等于 NumberUpDown 控件的范围)。I have been able to write code in the events of the controls such that when a radio button is checked the value of the NumberUpDown changes to that of the radio button checked and when the NumberUpDown value is changed, the correct radio button is checked.

There are users that want to use a stylus to click on the radio buttons and there are those that want to use the keyboard to enter the values.

有些用户想要使用手写笔单击单选按钮,有些用户想要使用键盘输入值。

Below is the code I wrote. The LONG if/then/else should be able to be written a different way. It would seem that RadioButtonArray (VB6) would solve my problem, but that's old school. I'm just not familiar with VB.NET enough to figure out how. I'm using Visual Studio 2010.

下面是我写的代码。LONG if/then/else 应该能够以不同的方式编写。RadioButtonArray (VB6) 似乎可以解决我的问题,但那是老生常谈。我只是对 VB.NET 不够熟悉,无法弄清楚如何。我正在使用 Visual Studio 2010。

PainAcceptableis the NumberUpDown control. PainAxare the individual radio buttons.

PainAcceptable是 NumberUpDown 控件。PainAx是单独的单选按钮。

Private Sub PainAcceptable_ValueChanged(sender As System.Object, e As System.EventArgs) Handles PainAcceptable.ValueChanged
    If PainAcceptable.Value = 0 Then
        PainA0.Checked = True
    ElseIf PainAcceptable.Value = 1 Then
        PainA1.Checked = True
    ElseIf PainAcceptable.Value = 2 Then
        PainA2.Checked = True
    ElseIf PainAcceptable.Value = 3 Then
        PainA3.Checked = True
    ElseIf PainAcceptable.Value = 4 Then
        PainA4.Checked = True
    ElseIf PainAcceptable.Value = 5 Then
        PainA5.Checked = True
    ElseIf PainAcceptable.Value = 6 Then
        PainA6.Checked = True
    ElseIf PainAcceptable.Value = 7 Then
        PainA7.Checked = True
    ElseIf PainAcceptable.Value = 8 Then
        PainA8.Checked = True
    ElseIf PainAcceptable.Value = 9 Then
        PainA9.Checked = True
    ElseIf PainAcceptable.Value = 10 Then
        PainA10.Checked = True
    End If
End Sub
' When radio button PainA1 is changed, PainAcceptable is set to the value 1
Private Sub PainA1_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles PainA1.CheckedChanged
    If PainA1.Checked Then
        PainAcceptable.Value = 1
    End If
End Sub

回答by ??ssa P?ngj?rdenlarp

Assume MY radios are named rbPain##where ## is A0, A1 ... A9. My radios are zerobased. Since radios sort of need a group control for them to act as a group, I have all 10 (0 - 9) on a groupbox or panel. If you dont like the look fiddle with borderstyle etc. Finally, there are no other controls on the panel but my rbPains

假设我的收音机被命名为rbPain#### 是 A0、A1 ... A9。我的收音机是基础的。由于收音机需要一个组控件来作为一个组,我在一个组框或面板上有所有 10 (0 - 9) 个。如果您不喜欢使用边框样式等的外观。 最后,面板上没有其他控件,但我的rbPains

Dim rbNameToFind as string = "rbPain"

rbNameToFind & = PainAcceptable.Value.ToString
' rbNameToFind now matches my rb names... "rbPain00" ... "rbPain09"

panel.Controls(rbNametoFind).Checked = True

If you do have to have some other controls on the panel iterate thru them all to find each rbPain and the one named what you are looking for:

如果您必须在面板上有一些其他控件,请遍历它们以找到每个 rbPain 和命名您要查找的内容的控件:

Dim rb as Radiobutton

For each ctl as Control in painPnl.Controls
  if ctl.GetType is GetType(radiobutton) then

       ' found an rb!
       if ctl.Name =  rbNameToFind Then
           rb = Ctype(ctl, gettype(radiobutton))
           ' found ours!
            rb.Checked = true
            exit for                   ' hurry home
       end if
   end if
 next

EDIT

编辑

You can also simplify the checkchanged handler...instead of 10 of them:

您还可以简化 checkchanged 处理程序...而不是其中的 10 个:

 Private Sub PainA0_CheckedChanged(sender As System.Object, e As System.EventArgs) _ 
      Handles PainA1.CheckedChanged, PainA2.CheckedChanged, PainA3.CheckedChanged, _
              PainA4.CheckedChanged ... (you get the idea)

      ' I would not parse the name to get the value, but you could stash the 
      ' related value in .Tag where PainA0.Tag=1 or whatever.  1 line of code for
      ' all 10 RBs:

      PainAcceptable.Value = sender.Tag

      ' *** OR you could just move all the existing code to one sub basically:

      Dim Pain As Integer = 0
      Select Case sender.Name
          Case "PainA1"
              Pain = 1
          Case "PainA2"
              Pain = 2
          ...etc
      End Select
      PainAcceptable.Value = Pain

 End Sub

The other issue you might run into is 2 controls changing one value. When they change the NumericUpDn, you set the checkstate of a Radio which causes a CheckChanged event where you set the NumericUpDn value where you set the checkstate which causes....

您可能遇到的另一个问题是 2 个控件更改一个值。当他们更改 NumericUpDn 时,您设置 Radio 的 checkstate,这会导致 CheckChanged 事件,您可以在其中设置 NumericUpDn 值,在其中设置导致...的检查状态。

In this case it will only iterate once because once you set the Value or CheckState they dont actually change the second time (that is true for the NUD, not sure of the RBs but it will stop when the NUD doesnt actually change).

在这种情况下,它只会迭代一次,因为一旦你设置了 Value 或 CheckState,它们实际上不会第二次改变(对于 NUD 来说是这样,不确定 RB,但是当 NUD 没有真正改变时它会停止)。

Finally, 10 RBs is a lot of RBs. You could save yourself a lot of time and code if you were to use a ComboBox instead. cboPain.SelectedIndexwould tell you what you want without any IF/Else or Select Case....

最后,10 个 RB 是很多 RB。如果您改用 ComboBox,您可以为自己节省大量时间和代码。 cboPain.SelectedIndex会告诉你你想要什么,没有任何 IF/Else 或 Select Case ....

HTH

HTH