2010-05-04 116 views
0

嘿,所有人,我在這裏看看是否有人會有更好的方式在較少的代碼中完成下面的任務。在VB.net代碼中簡化CASE代碼

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)

大衛

+0

任何人有任何建議添加修復? – StealthRT 2010-05-05 01:50:03

回答

1

由於很多代碼是要麼只是重複或只是以某種方式遞增,某種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 

編輯:馬克

建議
+0

建議修改:在'For'語句中插入'Step -1';我相信'x'實際上是'mainMenu.theNumOpened'。 – 2010-05-04 05:44:24

+0

謝謝,我認爲有些事情我忘記了for循環,但是由於我在VB中創建了一個「向後」循環,所以時間太長了。 – 2010-05-04 07:18:33

+0

感謝您花時間做到這一點,ho。但是如果有多於一個的顯示,它似乎不會將下一個框向下移動?它只是將它們堆疊在一起。 – StealthRT 2010-05-04 19:35:46