Упростите CASE в коде VB.net - PullRequest
0 голосов
/ 04 мая 2010

Привет всем, я смотрю здесь, чтобы увидеть, есть ли у кого-нибудь лучший способ выполнить эту задачу ниже в меньшем количестве кода.

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), то это также было бы здорово узнать! : О)

Любая помощь / предложения будут великолепны! : О)

David

1 Ответ

1 голос
/ 04 мая 2010

Так как большая часть кода либо просто повторяется, либо просто увеличивается каким-то образом, можно использовать какой-то цикл For, я думаю, что приведенный ниже код должен работать, но он может быть отключен на 1 или аналогичный: 1001 *

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

Редактировать: добавлены исправления, предложенные Марком

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...