Формы не закрываются, когда они должны - PullRequest
0 голосов
/ 21 апреля 2010

Привет всем, у меня проблема с попыткой закрыть 2 формы в разное время с помощью таймера на каждой из 2 форм.

Это мой макет формы:

frmMain Frm1 Frm2

Frm1 и Frm2 идентичны друг другу. FrmMain имеет 2 кнопки. Одна кнопка для вызова Frm1, а другая для вызова Frm2.

Frm1 и Frm2 каждый имеет таймер. Он считает до 0, а затем выполняет .close (). Однако, если я сделаю паузу между нажатием второй кнопки, они оба, кажется, всегда исчезают одновременно.

Я бы сделал что-то подобное ...

Нажмите на первую кнопку (показывает форму Frm1) подождите около 10 секунд ... Нажмите на вторую кнопку (показывает форму Frm2) они оба показаны на экране ... затем они оба исчезают одновременно ... что не должно происходить, так как я щелкнул поле Frm2 через 10 секунд ПОСЛЕ Frm1 ... Таким образом, Frm1 должен скрыться раньше, чем Frm2 ... но это не так.

Любая помощь в решении этой проблемы была бы великолепной! :)

дополнено кодом

Public Sub showMessageBox(ByVal typeOfBox As String, ByVal theMessage As String)
    If Form2.theNumOpened <= 6 Then
        Form2.theNumOpened = Form2.theNumOpened + 1 'ADDED

        Me.Opacity = 0
        Me.Show()

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

        isLoading = True
        theType = typeOfBox
        lblSaying.Text = theMessage

        If typeOfBox = "OK" Then
            Me.BackColor = Color.FromArgb(192, 255, 192)
        ElseIf typeOfBox = "ERR" Then
            Me.BackColor = Color.FromArgb(255, 192, 192)
        Else
            Me.BackColor = Color.FromArgb(255, 255, 192)
        End If

        If Len(theMessage) <= 30 Then
            howLong = 4000
        ElseIf Len(theMessage) >= 31 And Len(theMessage) <= 80 Then
            howLong = 7000
        ElseIf Len(theMessage) >= 81 And Len(theMessage) <= 100 Then
            howLong = 12000
        Else
            howLong = 17000
        End If

        Me.opacityTimer.Start()
    Else
        Me.Close()
    End If
End Sub

Private Sub opacityTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles opacityTimer.Tick
    If isLoading Then
        Me.Opacity += 0.07

        If Me.Opacity >= 0.8 Then
            Me.opacityTimer.Stop()
            Me.opacityTimer.Dispose()
            isLoading = False
            Pause(howLong)
            Me.opacityTimer.Start()
        End If
    Else
        Me.Opacity -= 0.08

        If Me.Opacity <= 0 Then
            Form2.theNumOpened = Form2.theNumOpened - 1 'ADDED
            Me.opacityTimer.Stop()
            Me.Close()
            Me.Dispose()
        End If
    End If
End Sub

Public Sub Pause(ByVal Milliseconds As Integer)
    Dim dTimer As Date

    dTimer = Now.AddMilliseconds(Milliseconds)

    Do While dTimer > Now
        Application.DoEvents()
    Loop
End Sub

И как я это называю, это так:

 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim theForm As New frmInfo2

    Call theForm.showMessageBox("MID", "This is just a simple test here! And some more text to see how longer it is." & theNum)
    theNum = theNum + 1
 End Sub

David

Ответы [ 2 ]

0 голосов
/ 21 апреля 2010

Ну, пока нет ответов ...

Я расскажу о некоторых плохих практиках:

  1. Использование DoEvents для цикла

  2. Использование «статической» переменной или свойства в неизвестной «Form2», которая, кажется, не относится ни к одной из форм, используемых здесь.

  3. Запуск и остановка таймера внутри собственного события.

  4. Использование вызовов Windows API для наведения порядка в форме.

Я думаю, что у вас больше проблем, чем просто "оно не закроется в нужное время". Не совсем уверен, с чего начать.

0 голосов
/ 21 апреля 2010

Получил с помощью этого кода:

Private Enum FormState
    FadeIn
    Pause
    FadeOut
End Enum

Private Sub opacityTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles opacityTimer.Tick
    Select Case CurState
        Case FormState.FadeIn
            Me.Opacity += 0.07

            If Me.Opacity >= 0.8 Then
                Me.opacityTimer.Stop()
                CurState = FormState.Pause
                PreviousInterval = opacityTimer.Interval
                opacityTimer.Interval = howLong
                Me.opacityTimer.Start()
            End If

        Case FormState.Pause
            Me.opacityTimer.Stop()
            CurState = FormState.FadeOut
            opacityTimer.Interval = PreviousInterval
            Me.opacityTimer.Start()

        Case FormState.FadeOut
            Me.Opacity -= 0.08

            If Me.Opacity <= 0 Then
                Me.opacityTimer.Stop()
                Form2.theNumOpened = Form2.theNumOpened - 1 'ADDED
                Me.Close()
                Me.Dispose()
            End If
    End Select
End Sub

: о)

David

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