选中单选按钮时更改变量

时间:2017-11-21 16:47:31

标签: vb.net

有没有办法压缩我的代码?我有5个单选按钮,下面是我的代码。目前,代码根据选中的单选框更改我的最小和最大公共变量的值。这是我发现实时更改单选按钮的第一种方式,以便我的其余代码将具有最小和最大变量的正确值。

select t1.EMP_ID, t1.BENEFIT_Per, t2.bene_max = 100 from t1
left join (select EMP_ID, max(BENEFIT_Per) bene_max from t1 group by EMP_ID) t2
on t1.EMP_ID = t2.EMP_ID

3 个答案:

答案 0 :(得分:1)

通常我不会在答案中写完整的代码(为此目的有一个邻居论坛:CODE REVIEW)但这次我会例外。

  1. 创建一个类(ArtyValues)以创建arty值的对象。使用一个类,您将能够重复使用它以获得更多“artilary”单选按钮,并且您的代码将更易于维护。
  2. 当每个类实例的键是一个单选按钮时,将该类的实例存储在字典(ArtyDictionary)中(在将来,如果你需要花费arty对象,它会更容易)< / LI>
  3. 最好将您的字典声明为属性,而不是字段(它不是必须的,但这是一个很好的做法)。
  4. 只有一种方法可以在GUI中进行更改(RadArtyChckChanged()),所有单选按钮控件CheckedChanged事件都会调用该方法并传递sender(单选按钮控件,单击)以提取匹配的实例。
  5. 示例:

       Public Class ArtyCalculator
    
            Public _ArtyDictionary As Dictionary(Of RadioButton, ArtyValues)
            Public Property ArtyDictionary As Dictionary(Of RadioButton, ArtyValues)
                Get
                    If IsNothing(_ArtyDictionary) Then
                        _ArtyDictionary = New Dictionary(Of RadioButton, ArtyValues)
                        LoadDictionary()
                        Return _ArtyDictionary
                    Else
                        Return _ArtyDictionary
                    End If
                End Get
                Set(value As Dictionary(Of RadioButton, ArtyValues))
                    _ArtyDictionary = value
                End Set
            End Property
    
            Private Sub LoadDictionary()
                ' initilize Arty objects
                Dim Mortar As New ArtyValues(45, 65, "Mortar")
                Dim Howitzer As New ArtyValues(75, 150, "Howitzer")
                Dim Field_Artillery As New ArtyValues(75, 150, "Field_Artillery")
                Dim Gun_Boat As New ArtyValues(50, 100, "Gun_Boat")
                Dim Tank As New ArtyValues(1, 40, "Tank")
                ' create the dictionary, keys will be radiobuton and value will be the ArtyValues instance that fit the radiobutton 
                _ArtyDictionary.Add(rBtnMortar, Mortar)
                _ArtyDictionary.Add(rBtnHowitzer, Howitzer)
                _ArtyDictionary.Add(rBtnFieldArtillery, Field_Artillery)
                _ArtyDictionary.Add(rBtnGunBoat, Gun_Boat)
                _ArtyDictionary.Add(rBtnTank, Tank)
            End Sub
    
            Private Sub ArtyCalculator_Load(sender As Object, e As EventArgs) Handles MyBase.Load
                rBtnMortar.Checked = True
            End Sub
    
            Private Sub rBtnMortar_CheckedChanged(sender As Object, e As EventArgs) Handles rBtnMortar.CheckedChanged
                RadArtyChckChanged(sender)
            End Sub
    
            Private Sub rBtnHowitzer_CheckedChanged(sender As Object, e As EventArgs) Handles rBtnHowitzer.CheckedChanged
                RadArtyChckChanged(sender)
            End Sub
    
            Private Sub rBtnFieldArtillery_CheckedChanged(sender As Object, e As EventArgs) Handles rBtnFieldArtillery.CheckedChanged
                RadArtyChckChanged(sender)
            End Sub
    
            Private Sub rBtnGunBoat_CheckedChanged(sender As Object, e As EventArgs) Handles rBtnGunBoat.CheckedChanged
                RadArtyChckChanged(sender)
            End Sub
    
            Private Sub rBtnTank_CheckedChanged(sender As Object, e As EventArgs) Handles rBtnTank.CheckedChanged
                RadArtyChckChanged(sender)
            End Sub
    
            Private Sub RadArtyChckChanged(sender As Object)
                Dim obj As ArtyValues = ArtyDictionary(sender)
                Min = obj.Min
                Max = obj.Max
                NameofArty = obj.NameofArty
                txtMinimum.Text = obj.GetMinText()
                txtMaximum.Text = obj.GetMaxText()
                txtMinimum.Refresh()
                txtMaximum.Refresh()
            End Sub
    
            Public Min As Integer
            Public Max As Integer
            Public NameofArty As String
        End Class
    
        Public Class ArtyValues
    
            Public Min As Integer
            Public Max As Integer
            Public NameofArty As String
    
            Sub New(ByVal min As Integer, ByVal max As Integer, ByVal nameofarty As String)
                Me.Min = min
                Me.Max = max
                Me.NameofArty = nameofarty
            End Sub
    
            Public Function GetMinText() As String
                Return "Minimum Distance: " & Min
            End Function
    
            Public Function GetMaxText() As String
                Return "Maximum Distance: " & Max
            End Function
        End Class
    

答案 1 :(得分:0)

扩展djv的评论......

Private Sub SetArty(name As String, min As Integer, max As Integer)
    Me.Min = min
    Me.Max = max
    Me.NameofArty = name
    txtMinimum.Text = "Minimum Distance: " & Min
    txtMaximum.Text = "Maximum Distance: " & Max
    txtMinimum.Refresh()   ' Not really necessary, but you had it in your code
    txtMaximum.Refresh()
End Sub

Private Sub rBtnMortar_CheckedChanged(sender As Object, e As EventArgs) Handles rBtnMortar.CheckedChanged
    If rBtnMortar.Checked Then SetArty("Mortar", 45, 65)
End Sub

以类似的方式压缩其他事件处理程序。

答案 2 :(得分:0)

我可以看到几个选项。

选项1:

Private Sub RefreshArtillerySelection(Min As Integer, Max As integer, Name As String)
    Me.Min = Min
    Me.Max = Max
    Me.NameOfArty = Name
    txtMinimum.Text = "Minimum Distance: " & Min
    txtMaximum.Text = "Maximum Distance: " & Max
    txtMinimum.Refresh()
    txtMaximum.Refresh()
End Sub

Private Sub rBtnMortar_CheckedChanged(sender As Object, e As EventArgs) Handles rBtnMortar.CheckedChanged
    If rBtnMortar.Checked Then RefreshArtillerySelection(45, 65, "Mortar")
End Sub

Private Sub rBtnHowitzer_CheckedChanged(sender As Object, e As EventArgs) Handles rBtnHowitzer.CheckedChanged
    If rBtnHowitzer.Checked Then RefreshArtillerySelection(75, 150, "Howitzer")
End Sub

Private Sub rBtnFieldArtillery_CheckedChanged(sender As Object, e As EventArgs) Handles rBtnFieldArtillery.CheckedChanged
    If rBtnFieldArtillery.Checked Then RefreshArtillerySelection(75, 150, "Field Artillery")
End Sub

Private Sub rBtnGunBoat_CheckedChanged(sender As Object, e As EventArgs) Handles rBtnGunBoat.CheckedChanged
    If rBtnGunBoat.Checked Then RefreshArtillerySelection(50, 100, "Gun Boat")
End Sub

Private Sub rBtnTank_CheckedChanged(sender As Object, e As EventArgs) Handles rBtnTank.CheckedChanged
    If rBtnTank.Checked Then RefreshArtillerySelection(1, 40, "Tank")
End Sub

选项2:

定义此类:

Public Class Artillery
     Public Property Name As String
     Public Property MinRange As Integer
     Public Property MaxRange As Integer
     Public Sub New(Name As String, Min As Integer, Max As Integer)
        Me.Name = Name
        Me.MinRange = Min
        Me.MaxRange = Max
    End Sub
End Class

然后将其添加到Load:

Private ArtilleryMap As New Dictionary(Of RadioButton, Artillery)

Private Sub ArtyCalculator_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ArtilleryMap.Add(rBtnMortar, New Artillery("Mortar", 45, 65))
    ArtilleryMap.Add(rBtnHowitzer, New Artillery("Howitzer", 75, 150))
    ArtilleryMap.Add(rBtnMortar, New Artillery("Field Artillery", 75, 150))
    ArtilleryMap.Add(rBtnMortar, New Artillery("Gun Boat", 50, 100))
    ArtilleryMap.Add(rBtnMortar, New Artillery("Tank", 1, 40))

    rBtnMortar.Checked = True
End Sub

并创建一个处理所有radiobuttons的通用事件处理程序:

Private Sub Artillery_CheckedChanged(sender As Object, e As EventArgs) Handles rBtnMortar.CheckedChanged, rBtnHowitzer.CheckedChanged, rBtnFieldArtillery.CheckedChanged, rBtnGunBoat.CheckedChanged, rBtnTank.CheckedChanged

    Dim radio As RadioButton = TryCast(sender, RadioButton)
    If radio Is Nothing OrElse Not ArtilleryMap.ContainsKey(radio) Then Return

    Dim art As Artillery = ArtilleryMap(radio)

    Me.Min = art.Min
    Me.Max = ar.Max
    Me.NameOfArty = art.Name
    txtMinimum.Text = "Minimum Distance: " & art.Min
    txtMaximum.Text = "Maximum Distance: " & art.Max
    txtMinimum.Refresh()
    txtMaximum.Refresh()
End Sub