表格不应该关闭

时间:2010-04-21 06:34:41

标签: vb.net visual-studio-2008

嘿所有,我有一个问题,试图在两个表格中的每一个上使用计时器在不同时间关闭两个表格。

这是我的表单布局:

frmMain Frm1中 FRM2

Frm1和Frm2彼此相同。 FrmMain有2个按钮。一个按钮调用Frm1,另一个按钮调用Frm2。

Frm1和Frm2每个都有一个计时器。它倒计数到0然后执行.close()。但是,如果我在点击第二个按钮之间暂停,它们似乎总是在同一时间消失。

我会做这样的事......

点击第一个按钮(显示Frm1表格) 等待大约10秒...... 单击第二个按钮(显示Frm2表单) 它们都显示在屏幕上...... 然后它们都会同时消失....这不应该发生,因为我在Frm1之后10秒钟点击了Frm2盒子......所以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

大卫

2 个答案:

答案 0 :(得分:0)

使用此代码获得它:

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

:○)

大卫

答案 1 :(得分:0)

还没有答案......

我会讨论一些不好的做法:

  1. 使用DoEvents循环

  2. 在未知的“Form2”中使用“静态”变量或属性,它似乎不属于此处使用的任何形式。

  3. 在自己的事件中启动和停止计时器。

  4. 使用Windows API调用强制执行表单。

  5. 我认为你有更多的问题而不仅仅是“它不会在合适的时间关闭”。 不确定从哪里开始。