2010-05-04 80 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