Click here to Skip to main content
15,870,165 members
Articles / General Programming / Threads
Tip/Trick

Custom Message Box Using Thread That Closes Automatically

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
29 May 2013CPOL1 min read 17.6K   300   7   6
Implimenting a message box that closes automatically using thread.

Image 1

Introduction

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.

Background

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:

VB.NET
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:

VB
If AutoClose Then  'if autoclose then start thread
    Dim mthread As New Threading.Thread(AddressOf waitNclose)
    mthread.Start()
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.

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

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

Using the customMsgBox inside frmmain.vb:

VB.NET
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)
myMsgBox.Show() 

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.

History

Download link updated.

License

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


Written By
Software Developer Firfire Technologies
Nepal Nepal
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionNot an article Pin
OriginalGriff23-May-13 3:39
mveOriginalGriff23-May-13 3:39 
QuestionRe: Not an article Pin
Surendra Adhikari SA23-May-13 3:51
professionalSurendra Adhikari SA23-May-13 3:51 
GeneralRe: Not an article Pin
OriginalGriff23-May-13 4:05
mveOriginalGriff23-May-13 4:05 
QuestionRe: Not an article Pin
Surendra Adhikari SA23-May-13 4:09
professionalSurendra Adhikari SA23-May-13 4:09 
AnswerRe: Not an article Pin
OriginalGriff23-May-13 4:16
mveOriginalGriff23-May-13 4:16 
AnswerRe: Not an article Pin
Surendra Adhikari SA23-May-13 4:21
professionalSurendra Adhikari SA23-May-13 4: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.