Click here to Skip to main content
13,291,690 members (59,672 online)
Click here to Skip to main content
Add your own
alternative version


7 bookmarked
Posted 23 May 2013

Custom Message Box Using Thread That Closes Automatically

, 29 May 2013
Rate this:
Please Sign up or sign in to vote.
Implimenting a message box that closes automatically using thread.


This is a simple article on making a custom dialog box to show a message to an application user. A dialog box that can be set to close automatically itself, which is implemented using a thread.

This article does not deal with Threading in depth. But a simple idea of using a thread can be taken.


This is the result of a task where I have to:

  • Avoid the use of VB.NET msgbox() function and make custom form to give info to user
  • Form that can be set to closed automatically after some time
  • Give unique look on all info to user over the project

Using the code

The form customMsgBox.vb is implemented with:

Public Class customMsgBox

    Private infoTitle, infoMessage As String
    Private infoWidth As Integer = 420
    Private infoHeight As Integer = 190
    Private AutoClose As Boolean = False
    Private waitTime As Integer  
    Public Sub New(ByVal Title As String, ByVal Message As String, _
                   ByVal isAutoClose As Boolean, ByVal duration As Integer)

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

        infoTitle = Title
        infoMessage = Message
        AutoClose = isAutoClose
        waitTime = duration

End Sub 

The main part of the customMsgBox load event is to check the AutoClose boolean and process as:

If AutoClose Then  'if autoclose then start thread
    Dim mthread As New Threading.Thread(AddressOf waitNclose)
End If

Here waitNclose is a sub that waits till the time defined and closes the form customMsgBox. Since waitNclose is run in a different thread, we can not access customMsgBox and its properties inside waitNclose. So the customMsgBox.close operation is implemented by calling another sub closeform.

Private Sub waitNclose()
    System.Threading.Thread.Sleep(waitTime * 1000) 'wait time
End Sub

Private Sub closeform(ByVal closeOrNot As Boolean)
    If Me.InvokeRequired Then
        Me.Invoke(New Action(Of Boolean)(AddressOf closeform), closeOrNot)
   End If
End Sub 

Using the customMsgBox inside frmmain.vb:

Dim myTitle, myMsg As String
Dim isAuto As Boolean
Dim waitTime As Integer

waitTime = Me.txtWait.Text
myTitle = Me.txtTitle.Text
myMsg = Me.rtbMsg.Text
isAuto = Me.txtWait.Text

Dim myMsgBox As New customMsgBox(myTitle, myMsg, isAuto, waitTime)

Points of Interest 

This is a simple article on using your own dialog box. It uses threads with a minimal approach. Hence it is targeted at beginners.


Download link updated.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Surendra Adhikari SA
Software Developer Government Of Nepal
Nepal Nepal
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionNot an article Pin
OriginalGriff23-May-13 4:39
protectorOriginalGriff23-May-13 4:39 
QuestionRe: Not an article Pin
Rajan Adhikari23-May-13 4:51
professionalRajan Adhikari23-May-13 4:51 
GeneralRe: Not an article Pin
OriginalGriff23-May-13 5:05
protectorOriginalGriff23-May-13 5:05 
QuestionRe: Not an article Pin
Rajan Adhikari23-May-13 5:09
professionalRajan Adhikari23-May-13 5:09 
AnswerRe: Not an article Pin
OriginalGriff23-May-13 5:16
protectorOriginalGriff23-May-13 5:16 
AnswerRe: Not an article Pin
Rajan Adhikari23-May-13 5:21
professionalRajan Adhikari23-May-13 5:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 29 May 2013
Article Copyright 2013 by Surendra Adhikari SA
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid