美化这个vb代码

时间:2014-05-20 13:51:52

标签: asp.net vb.net

我有3个组合框,每个组合框中有3个相同的选项。如果用户第二次选择相同的选择,则第一个组合框将重置。我有3个其他部分将使用大约15个组合框进行相同的操作。我想知道是否有一种更短/更紧凑的方式来编码我正在做的事情?

  Protected Sub ComBox_GER1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComBox_GER1.SelectedIndexChanged
    Dim ValueSelected = ComBox_GER1.SelectedIndex

    If ComBox_GER2.SelectedIndex = ValueSelected Then
        System.Threading.Thread.Sleep(500)
        ComBox_GER2.ClearSelection()
    End If
    If ComBox_GER3.SelectedIndex = ValueSelected Then
        System.Threading.Thread.Sleep(500)
        ComBox_GER3.ClearSelection()
    End If

End Sub

Protected Sub ComBox_GER2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComBox_GER2.SelectedIndexChanged
    Dim ValueSelected = ComBox_GER2.SelectedIndex

    If ComBox_GER1.SelectedIndex = ValueSelected Then
        System.Threading.Thread.Sleep(500)
        ComBox_GER1.ClearSelection()
    End If
    If ComBox_GER3.SelectedIndex = ValueSelected Then
        System.Threading.Thread.Sleep(500)
        ComBox_GER3.ClearSelection()
    End If
End Sub

Protected Sub ComBox_GER3_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComBox_GER3.SelectedIndexChanged
    Dim ValueSelected = ComBox_GER3.SelectedIndex

    If ComBox_GER1.SelectedIndex = ValueSelected Then
        System.Threading.Thread.Sleep(500)
        ComBox_GER1.ClearSelection()
    End If
    If ComBox_GER2.SelectedIndex = ValueSelected Then
        System.Threading.Thread.Sleep(500)
        ComBox_GER2.ClearSelection()
    End If
End Sub

结束班

2 个答案:

答案 0 :(得分:2)

Protected Sub ComboboxChange(sender as Object, e as EventArgs) Handles  
 ComBox_GER1.SelectedIndexChanged, ComBox_GER2.SelectedIndexChanged
 ComBox_GER3.SelectedIndexChanged
Dim ComboBox = sender
Dim ValueSelected = ComboBox.SelectedValue
if ComboBox.Name = Box_GER1 then
  if ComBox_GER2.SelectedValue = ValueSelected then
     ComBox_GER2.ClearSelection()
  endif
  if ComBox_GER3SelectedValue = ValueSelected then
     ComBox_GER3ClearSelection()
  endif
else if

//等

答案 1 :(得分:0)

我假设你只是想知道如何集中逻辑,改变其他组合框的选择。如果是这样,可以用这样的东西来完成(根据@ imad-s):

Private Sub DoSelectedIndexChanged(ctl As ListControl, selectedIndex As Integer)
    If ctl.SelectedIndex = selectedIndex Then
        System.Threading.Thread.Sleep(500)
        ctl.ClearSelection()
    End If
End Sub

Protected Sub ComBox_GER1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComBox_GER1.SelectedIndexChanged
    Dim ValueSelected = ComBox_GER1.SelectedIndex
    DoSelectedIndexChanged(ComBox_GER2, ValueSelected)
    DoSelectedIndexChanged(ComBox_GER3, ValueSelected)
End Sub
Protected Sub ComBox_GER2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComBox_GER2.SelectedIndexChanged
    Dim ValueSelected = ComBox_GER1.SelectedIndex
    DoSelectedIndexChanged(ComBox_GER1, ValueSelected)
    DoSelectedIndexChanged(ComBox_GER3, ValueSelected)
End Sub

然而,仍有睡眠线程的问题 - 特别是在ASP.NET中这没有任何意义。

如果你有很多这些,那么另一个选项(根据@ dean-depue)是让所有下拉列表使用相同的事件处理程序。然后,在事件处理程序逻辑中,遍历所有组合框并在每个不等于发送方对象的控件上执行逻辑。它看起来像这样:

Protected Sub ComBoxes_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComBox_GER1.SelectedIndexChanged, ComBox_GER2.SelectedIndexChanged
    'Get the selected index of the sender
    Dim ValueSelected = CType(sender, ListControl).SelectedIndex

    'Iterate through every list control in the page
    Dim listControls = From c As Control In Controls
                       Where TypeOf (c) Is ListControl
                       Select c
    For Each listControl As ListControl In listControls
        If Not Object.Equals(sender, listControl) Then
            DoSelectedIndexChanged(listControl, ValueSelected)
        End If
    Next
End Sub