Quantcast
Channel: VBForums - Visual Basic 6 and Earlier
Viewing all articles
Browse latest Browse all 21100

[RESOLVED] Sleep Command at Form load/initiation

$
0
0
I want a very simple 'timed messagebox', but with huge fonts. All searches say to create your own, instead of using a timed messagebox (native messagebox with this code to create/use which does not allow font changes on the msgbox):

Code:

Option Explicit
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Private Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
Private Const WM_CLOSE      As Long = 16
Private CurMBTitle          As String
Public Function TimedMsgBox(Prompt As String, Optional ByVal TimeOut As Long = 0, Optional Icon As VbMsgBoxStyle = vbOKOnly, Optional Title As String = vbNullString)
  Dim TimerId  As Long
    CurMBTitle = Title
    If TimeOut = 0 Then
        TimeOut = 5 * 1000
        Else
            TimeOut = TimeOut * 1000
    End If
    TimerId = SetTimer(0, 0, TimeOut, AddressOf TimeOutMB)
    TimedMsgBox = MsgBox(Prompt, Icon, CurMBTitle)
    TimedMsgBox = 0
    KillTimer 0, TimerId
End Function

Private Sub TimeOutMB(hwnd As Long, uMsg As Long, idEvent As Long, dwTime As Long)
    SendMessage FindWindow(vbNullString, CurMBTitle), WM_CLOSE, 0&, 0&
End Sub

So, to create my own, I tried a simple form (borders modified and a label (whith large font)). Searches say the Sleep command is the way to go. Examples show this:

Code:

Private Sub Form_Load()   
Sleep 2000
'do whatever needs to be done
End Sub

Unfortunately, "do whatever needs to be done", which is Unload Me, fails (see below)

However, that fails to show the form and the label. So, I tried the sleep command in several form 'load' functions, with varying results...none of which I want...which is to simply show the form with my large-font label for 2 seconds and then unload it. See the commented-out code(s) below:
Code:

  Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

'Private Sub Form_Activate()  'from shows, but with white BG, no label.  Unloads in 2 secs
'Sleep 2000
'Unload Me
'End Sub
'
'Private Sub Form_GotFocus()  'from shows, but with white BG, no label.  Unloads in 2 secs
'Sleep 2000
'Unload Me
'End Sub
'
'Private Sub Form_Initialize()  'Form and label show after 2 secs, does not unload form
'Sleep 2000
'Unload Me
'End Sub
'
'Private Sub Form_Load()      'Nothing shows, program ends after 2 secs
'Sleep 2000
'do whatever needs to be done
'End Sub
'
'Private Sub Form_Paint()    'Form shows but no label.  Waits 2 secs then error msg-can't unload
'Sleep 2000
'Unload Me
'End Sub

I tried a timer, using a static variable like this...it works the first time, but when I load the form the the second time, the form load, but fails to unload:
Code:

Private Sub frmMsgBox_Load()
Timer1.Interval = 2000
Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()
Static x As Integer
x = x + 1
If x = 2 Then
    Unload Me
End If
End Sub


Viewing all articles
Browse latest Browse all 21100

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>