在VB.net代码中简化CASE

时间:2010-05-04 02:12:45

标签: vb.net visual-studio visual-studio-2008

嘿所有,我在这里看看是否有人有更好的方法在更少的代码中完成下面的任务。

Select Case mainMenu.theNumOpened
            Case 1
                Me.Text = "NBMsg1"
                Me.SetDesktopLocation(My.Computer.Screen.WorkingArea.Width - 302, 5)
            Case 2
                Me.Text = "NBMsg2"
                Dim hwnd As IntPtr = FindWindow(vbNullString, "NBMsg1")
                SetWindowPos(hwnd, 0, My.Computer.Screen.WorkingArea.Width - 302, Me.Height + 10, 0, 0, 1)
                Me.SetDesktopLocation(My.Computer.Screen.WorkingArea.Width - 302, 5)
            Case 3
                Me.Text = "NBMsg3"
                Dim hwnd As IntPtr = FindWindow(vbNullString, "NBMsg2")
                SetWindowPos(hwnd, 0, My.Computer.Screen.WorkingArea.Width - 302, Me.Height + 10, 0, 0, 1)
                hwnd = FindWindow(vbNullString, "NBMsg1")
                SetWindowPos(hwnd, 0, My.Computer.Screen.WorkingArea.Width - 302, (Me.Height * 2) + 15, 0, 0, 1)
                Me.SetDesktopLocation(My.Computer.Screen.WorkingArea.Width - 302, 5)
            Case 4
                Me.Text = "NBMsg4"
                Dim hwnd As IntPtr = FindWindow(vbNullString, "NBMsg3")
                SetWindowPos(hwnd, 0, My.Computer.Screen.WorkingArea.Width - 302, Me.Height + 10, 0, 0, 1)
                hwnd = FindWindow(vbNullString, "NBMsg2")
                SetWindowPos(hwnd, 0, My.Computer.Screen.WorkingArea.Width - 302, (Me.Height * 2) + 15, 0, 0, 1)
                hwnd = FindWindow(vbNullString, "NBMsg1")
                SetWindowPos(hwnd, 0, My.Computer.Screen.WorkingArea.Width - 302, (Me.Height * 3) + 20, 0, 0, 1)
                Me.SetDesktopLocation(My.Computer.Screen.WorkingArea.Width - 302, 5)
            Case 5
                Me.Text = "NBMsg5"
                Dim hwnd As IntPtr = FindWindow(vbNullString, "NBMsg4")
                SetWindowPos(hwnd, 0, My.Computer.Screen.WorkingArea.Width - 302, Me.Height + 10, 0, 0, 1)
                hwnd = FindWindow(vbNullString, "NBMsg3")
                SetWindowPos(hwnd, 0, My.Computer.Screen.WorkingArea.Width - 302, (Me.Height * 2) + 15, 0, 0, 1)
                hwnd = FindWindow(vbNullString, "NBMsg2")
                SetWindowPos(hwnd, 0, My.Computer.Screen.WorkingArea.Width - 302, (Me.Height * 3) + 20, 0, 0, 1)
                hwnd = FindWindow(vbNullString, "NBMsg1")
                SetWindowPos(hwnd, 0, My.Computer.Screen.WorkingArea.Width - 302, (Me.Height * 4) + 25, 0, 0, 1)
                Me.SetDesktopLocation(My.Computer.Screen.WorkingArea.Width - 302, 5)
            Case 6
                Me.Text = "NBMsg6"
                Dim hwnd As IntPtr = FindWindow(vbNullString, "NBMsg5")
                SetWindowPos(hwnd, 0, My.Computer.Screen.WorkingArea.Width - 302, Me.Height + 10, 0, 0, 1)
                hwnd = FindWindow(vbNullString, "NBMsg4")
                SetWindowPos(hwnd, 0, My.Computer.Screen.WorkingArea.Width - 302, (Me.Height * 2) + 15, 0, 0, 1)
                hwnd = FindWindow(vbNullString, "NBMsg3")
                SetWindowPos(hwnd, 0, My.Computer.Screen.WorkingArea.Width - 302, (Me.Height * 3) + 20, 0, 0, 1)
                hwnd = FindWindow(vbNullString, "NBMsg2")
                SetWindowPos(hwnd, 0, My.Computer.Screen.WorkingArea.Width - 302, (Me.Height * 4) + 25, 0, 0, 1)
                hwnd = FindWindow(vbNullString, "NBMsg1")
                SetWindowPos(hwnd, 0, My.Computer.Screen.WorkingArea.Width - 302, (Me.Height * 5) + 30, 0, 0, 1)
                Me.SetDesktopLocation(My.Computer.Screen.WorkingArea.Width - 302, 5)
            Case Else
                Me.Close()
                Me.Dispose()
        End Select

它的作用是传递给它现在许多窗口目前已经打开。因此,如果当然那么它将进入案例1.如果有2个已打开,那么它将最旧的向下移动并将最新的向上移动。我已经设置好了,一次最多只能打开6个盒子。

如果有人知道我怎么也可以“滑动”它们(有点像jQuery的效果)那么这也是,非常棒的知道! :O)

任何帮助/建议都会很棒! :O)

大卫

1 个答案:

答案 0 :(得分:1)

由于很多代码只是重复或只是以某种方式递增,所以某种For循环是可行的方法,我认为下面的代码应该可以工作,但它可能会被1或类似的:< / p>

If mainMenu.theNumOpened > 0 And mainMenu.theNumOpened <= 6 Then
    Me.Text = "NBMsg" & Cstr(mainMenu.theNumOpened)
    Dim hwnd As IntPtr
    Dim h as integer = 5
    For i As Integer = mainMenu.theNumOpened - 1 To 1 Step -1
        FindWindow(vbNullString, "NBMsg" & CStr(i))        
        h += Me.Height + 5
        SetWindowPos(hwnd, 0, My.Computer.Screen.WorkingArea.Width - 302, h, 0, 0, 1)
    Next
    Me.SetDesktopLocation(My.Computer.Screen.WorkingArea.Width - 302, 5)
End If

编辑:添加了Mark

建议的修补程序