message box and time question?

  • Thread starter Thread starter karim
  • Start date Start date
K

karim

Hello All,
Is it possible in vb 08 to set a message box to pop up when you click a
button and have a cod that sets a time in seconds so that lets say after 5
sec. the message box closes by itself when the counter reach 0 sec. ?

Thanks for all of your help...
 
Hello All,
Is it possible in vb 08 to set a message box to pop up when you click a
button and have a cod that sets a time in seconds so that lets say after 5
sec. the message box closes by itself when the counter reach 0 sec. ?

Thanks for all of your help...

Karim,
Closing messagebox automatically can be done by sending ENTER key to
the application within a Timer's tick event sub, so place a Timer
control on your form and set its interval property to "5000" (5
seconds). Then place a button onto form to show messagebox and to run
timer as follows:

'------------------------
Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Timer1.Enabled = True
MsgBox("Hello World")
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Timer1.Tick
SendKeys.Send("{ENTER}")
Timer1.Enabled = False
End Sub

End Class
'-------------------------------


Hope this helps,

Onur Güzel
 
Thank you very much. works great.
and one other thing if you don't mind. it's not really a big deal, but would
you be able to show the time counting down on the message box?
 
Hello karim,

This method works, but it has one catch: Let's say that you have a
Yes/No message box and the user is just moving the mouse pointer to
"No", but as the 5 seconds just expired "Yes" will be automatically
executed. I would suggest to use this only with "OkOnly" style message
boxes.

On the other hand I wonder: If a message box can be closed automatically
it seems that the information it shows is not important. Why then
display it in the first place?

Best regards,

Martin
 
Hello All,
Is it possible in vb 08 to set a message box to pop up when you click a
button and have a cod that sets a time in seconds so that lets say after 5
sec. the message box closes by itself when the counter reach 0 sec. ?

Thanks for all of your help...

While using SendKeys to close the message box will work, I would
recommend you create your own form to display. Creating MessageBox
type form should just take a matter of minutes, and once it's done,
expanding it to include the countdown functionality is as simple as
adding a label and a timer control.

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
 
Martin H. said:
This method works, but it has one catch: Let's say that you have a Yes/No
message box and the user is just moving the mouse pointer to "No", but as
the 5 seconds just expired "Yes" will be automatically executed. I would
suggest to use this only with "OkOnly" style message boxes.

And what if the message box loses it's focus during that five seconds. Then
some other application will receive that ENTER-key and that application
might just be asking "Do you really want to delete all files?"

So, what I'm trying to say is that if SendKeys is used you should check that
the message box is really focused.

-Teemu
 
And what if the message box loses it's focus during that five seconds. Then
some other application will receive that ENTER-key and that application
might just be asking "Do you really want to delete all files?"

So, what I'm trying to say is that if SendKeys is used you should check that
the message box is really focused.

 -Teemu

Yep - this is one of the many reason to just roll your own form. It's
much safer to wire the functionality into a new form than hack the
MessageBox with SendKeys.

Besides, how on earth could you test that the MessageBox was closed?

(To the OP) You are unit testing aren't you?

:-)

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
 
And what if the message box loses it's focus during that five seconds. Then
some other application will receive that ENTER-key and that application
might just be asking "Do you really want to delete all files?"

So, what I'm trying to say is that if SendKeys is used you should check that
the message box is really focused.

 -Teemu

That's somehow right, SendKeys solution is valid during the messagebox
has the focus, though 5 seconds can be considered short, you can't
know when message box can lose focus.

Plus, Sending ENTER key using SendKeys can close messagebox if it's a
OkOnly (simple) messagebox as Martin stated, in that case, developer
can consider sending ESCAPE or other keys to get other MessageBox
result rather than OK.

Onur Güzel
 
Thank you very much. works great.
and one other thing if you don't mind. it's not really a big deal, but would
you be able to show the time counting down on the message box?












- Show quoted text -

If you're meaning showing remaing seconds in messagebox in real-time,
as far as i know it's not possible to update MessageBox content,
however if you implement a small messagebox-like form, then you'll be
simply able to show remaining seconds on your form using a Timer
control with 1 seconds (1000 ms) interval.

'-------------------------------------
' Assuming you've added a small form and a timer on it
Public Class Form1
Dim remaining As Integer = 5
Private Sub Timer1_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Timer1.Tick

' Eg: Show remaining time in a Label
remaining -=1
Label1.Text = remaining

If remaining = 0 Then
Timer1.Enabled = False
' When time is up, close current form
Me.Close()

End Sub
'----------------------------------

Hope this helps,

Onur Güzel
 
Hi Onur,

I always forget this one and start making my own dialog for this kind of
purposes.

Thank you for reminding me.

Cor

"kimiraikkonen" <[email protected]> schreef in bericht
Hello All,
Is it possible in vb 08 to set a message box to pop up when you click a
button and have a cod that sets a time in seconds so that lets say after 5
sec. the message box closes by itself when the counter reach 0 sec. ?

Thanks for all of your help...

Karim,
Closing messagebox automatically can be done by sending ENTER key to
the application within a Timer's tick event sub, so place a Timer
control on your form and set its interval property to "5000" (5
seconds). Then place a button onto form to show messagebox and to run
timer as follows:

'------------------------
Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Timer1.Enabled = True
MsgBox("Hello World")
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Timer1.Tick
SendKeys.Send("{ENTER}")
Timer1.Enabled = False
End Sub

End Class
'-------------------------------


Hope this helps,

Onur Güzel
 
Back
Top