一千萬個為什麽

搜索

嘿所有,我在這裏看看是否有人有更好的方法來在更少的代碼中完成下面的任務。

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)

大衛

最佳答案

由於很多代碼只是重復或只是以某種方式遞增,因此某種For循環是可行的方法,我認為下面的代碼應該可以工作,但它可能會被1或類似的代碼關閉:

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建議的修復程序

轉載註明原文: 在VB.net代碼中簡化CASE

猜你喜歡