2010-04-21 64 views
-1

嘿所有,我有一个问题,试图获得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 

大卫

+1

我会建议在声明表单的地方张贴代码,在那里打开它们并启动计时器以及关闭它们的位置。 – 2010-04-21 08:09:48

+0

已更新为代码。 – StealthRT 2010-04-21 18:16:31

回答

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 

:O)

大卫

0

哦,不解答...

我附和了一些不好的做法:

  1. 使用的DoEvents循环

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

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

  4. 使用Windows API调用强制窗体顺序。

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