Show a timed message

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

To my surprise, I could not find an easy way to show a timed message with
either MessageBox() or MsgBox(). Here is some code that I tried
unsuccessfully:

Dim t As New Threading.Thread(New Threading.ThreadStart(AddressOf
ShowMessage))
t.IsBackground = True
t.Start()
System.Threading.Thread.Sleep(5000)
t.Abort()
========================
Private Sub ShowMessage()
MessageBox.Show("This message is displayed for 5 seconds")
End Sub

My Internet searches were not successful. There must be an easy way to do
this.
 
I do not understand what you mean. I know how to use a timer to start
something like displaying a message or updating a database but once I open a
Message box, I do not know how to use a timer to force the message to close.

Can you provide an example?
 
Best method is to create a form for the message... Turn off the
control-box in the properties so it looks like a popup and set for "Fixed
Dialog".

Put a label in the middle of the form and a timer.

Then expose 2 properties and a function. Property 1 is a string called
MsgText, Property 2 is the length of time (in ms) to display. The function
is called display.

The function sets the timer interval to the length, the label text to the
MsgText and then does a me.showdialog..

If the timer.tick event just do a me.close to close the form.

To call the popup just instanciate the form, set the 2 properties and then
call the Display function.. ie

dim frm as new popupmessage
frm.MsgText="This is the message"
frm.MsgDisplayLength=5000
frm.display
frm.dispose

If you want to get advanced then wrap the whole lot up in a class.

Hope this helps
Simon
 
genojoe said:
To my surprise, I could not find an easy way to show a timed message
with either MessageBox() or MsgBox(). Here is some code that I
tried unsuccessfully:

Dim t As New Threading.Thread(New Threading.ThreadStart(AddressOf
ShowMessage))
t.IsBackground = True
t.Start()
System.Threading.Thread.Sleep(5000)
t.Abort()
========================
Private Sub ShowMessage()
MessageBox.Show("This message is displayed for 5 seconds")
End Sub

My Internet searches were not successful. There must be an easy way
to do this.

I recommend not to abort a thread this way. I admit I don't know why it
doesn't work this way. It's better to perform a controlled end of a thread.
Create your own Form instead and call it's Close method to close it (Invoke
required).


Armin
 
I'm a bit curious why you are using a separate thread instead of just
placing a timer control on your form and using it's procedures to turn your
message on or off?

Regards
Mike Enarson
 
Back
Top