Automatically close a MessageBox after x seconds

G

Guest

Is there a way to automatically close a MessageBox after a certain amount of
time has elapsed? If not, is there some dialog class that does this? I'd
like to display to users a message in C# for a few seconds and then close the
dialog box without any user input.
 
I

Imran Koradia

There's no direct way - you could create your own messagebox (inherit from a
Form) and add the functionality. Here's some code in VB.NET (hoepfully it
shouldn't be too hard to convert to C#) that will get you going (does not
have everything a standard messagebox has but just to give you an idea):

Public NotInheritable Class MessageBoxEx
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents MessageLabel As System.Windows.Forms.Label
Friend WithEvents btnOK As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.MessageLabel = New System.Windows.Forms.Label
Me.btnOK = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'MessageLabel
'
Me.MessageLabel.Location = New System.Drawing.Point(16, 16)
Me.MessageLabel.Name = "MessageLabel"
Me.MessageLabel.Size = New System.Drawing.Size(336, 23)
Me.MessageLabel.TabIndex = 0
Me.MessageLabel.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter
'
'btnOK
'
Me.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK
Me.btnOK.Location = New System.Drawing.Point(144, 56)
Me.btnOK.Name = "btnOK"
Me.btnOK.TabIndex = 1
Me.btnOK.Text = "OK"
'
'MessageBoxEx
'
Me.AcceptButton = Me.btnOK
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(368, 95)
Me.Controls.Add(Me.btnOK)
Me.Controls.Add(Me.MessageLabel)
Me.FormBorderStyle =
System.Windows.Forms.FormBorderStyle.FixedSingle
Me.MaximizeBox = False
Me.Name = "MessageBoxEx"
Me.ShowInTaskbar = False
Me.StartPosition =
System.Windows.Forms.FormStartPosition.CenterParent
Me.Text = "MessageBoxEx"
Me.ResumeLayout(False)

End Sub

#End Region

Private Shared m_Msg As String = ""

Private m_Timer As New Timers.Timer(3000)
Private m_FirstActivation As Boolean = False

Protected Shadows Sub Show()
MessageLabel.Text = m_Msg
MyBase.ShowDialog()
End Sub

Public Shared Shadows Sub Show(ByVal Message As String)
m_Msg = Message
Dim o As New MessageBoxEx
o.Show()
End Sub

Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnOK.Click
Me.Close()
End Sub

Private Sub MessageBoxEx_Activated(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Activated
If Not m_FirstActivation Then
m_FirstActivation = True
m_Timer.Start()
AddHandler m_Timer.Elapsed, AddressOf TimerElapsed
End If
End Sub

Private Sub TimerElapsed(ByVal sender As Object, ByVal e As
System.Timers.ElapsedEventArgs)
m_Timer.Stop()
Me.Close()
End Sub
End Class

This messagebox will close in 3 seconds. You can change the time by changing
the value in the constructor of the timer (each tick is a millisecond).

hope that helps..
Imran.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top