如何在vb.net中制作短代码

时间:2017-01-17 06:38:34

标签: vb.net winforms

任何人都可以帮我用几行来缩短这段代码吗? 我不知道如何缩短这段代码。

    If TBird.Bounds.IntersectsWith(P1.Bounds) Then
        GOver.Visible = True
        GOver.Location = New Point(227, 79)
    ElseIf TBird.Bounds.IntersectsWith(P2.Bounds) Then
        GOver.Visible = True
        GOver.Location = New Point(227, 79)
    ElseIf TBird.Bounds.IntersectsWith(P3.Bounds) Then
        GOver.Visible = True
        GOver.Location = New Point(227, 79)
    ElseIf TBird.Bounds.IntersectsWith(P4.Bounds) Then
        GOver.Visible = True
        GOver.Location = New Point(227, 79)
    ElseIf TBird.Bounds.IntersectsWith(P5.Bounds) Then
        GOver.Visible = True
        GOver.Location = New Point(227, 79)
    ElseIf TBird.Bounds.IntersectsWith(P6.Bounds) Then
        GOver.Visible = True
        GOver.Location = New Point(227, 79)
    ElseIf TBird.Bounds.IntersectsWith(P7.Bounds) Then
        GOver.Visible = True
        GOver.Location = New Point(227, 79)
    ElseIf TBird.Bounds.IntersectsWith(P8.Bounds) Then
        GOver.Visible = True
        GOver.Location = New Point(227, 79)

3 个答案:

答案 0 :(得分:3)

您可以将所有Bounds放在List中,然后使用LINQ Any来简化您的逻辑:

Dim list As New List(Of Rectangle)(New Rectangle() {P1.Bounds, P2.Bounds, P3.Bounds, P4.Bounds, P5.Bounds, P6.Bounds, P7.Bounds, P8.Bounds})
IF list.Any(Function(x) TBird.Bounds.IntersectsWith(x)) THEN
    GOver.Visible = True
    GOver.Location = New Point(227, 79)
END IF

答案 1 :(得分:1)

Dim HasIntersects As Boolean = True

With TBird.Bounds
    Select Case True
        Case .IntersectsWith(P1.Bounds)
        Case .IntersectsWith(P2.Bounds)
        Case .IntersectsWith(P3.Bounds)
        Case .IntersectsWith(P4.Bounds)
        Case .IntersectsWith(P5.Bounds)
        Case .IntersectsWith(P6.Bounds)
        Case .IntersectsWith(P7.Bounds)
        Case .IntersectsWith(P8.Bounds)
        Case Else
            HasIntersects = False
    End Select
End With

If HasIntersects Then
    GOver.Visible = True
    GOver.Location = New Point(227, 79)
End If

答案 2 :(得分:0)

If TBird.Bounds.IntersectsWith(P1.Bounds) OrElse 
    TBird.Bounds.IntersectsWith(P2.Bounds) OrElse 
    TBird.Bounds.IntersectsWith(P3.Bounds) OrElse 
    TBird.Bounds.IntersectsWith(P4.Bounds) OrElse 
    TBird.Bounds.IntersectsWith(P5.Bounds) OrElse 
    TBird.Bounds.IntersectsWith(P6.Bounds) OrElse 
    TBird.Bounds.IntersectsWith(P7.Bounds) OrElse 
    TBird.Bounds.IntersectsWith(P8.Bounds) Then
    GOver.Visible = True
    GOver.Location = New Point(227, 79)
End If