none
How to make the MessageBox dialog box close automatically in a few seconds RRS feed

Answers

  • There is a nice dialog available via NuGet. The samples are in C# (full source) but here are two in VB.NET, first ask a question, closes if no response in 2.5 seconds, second is an alert, closes in 1 second if no response. You control the timeout.

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim toBeOrNotToBeQuestion = AutoClosingMessageBox.
                    Factory(showMethod:=Function(caption, buttons)
                                            Return MessageBox.Show(Me, "To be or not to be?", caption, buttons, MessageBoxIcon.Question)
                                        End Function, caption:="The question")
            If DialogResult.Yes = toBeOrNotToBeQuestion.Show(timeout:=2500, buttons:=MessageBoxButtons.YesNo, defaultResult:=DialogResult.No) Then
                MessageBox.Show("Yes")
            Else
                MessageBox.Show("No")
            End If
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            AutoClosingMessageBox.
                Factory(showMethod:=Function(caption, buttons)
                                        Return MessageBox.Show(Me, "To be or not to be?",
                                                               caption, buttons, MessageBoxIcon.Exclamation)
                                    End Function, caption:="Alert").Show(timeout:=1000, buttons:=MessageBoxButtons.OK)
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Tuesday, July 31, 2018 2:40 AM
    Moderator

All replies

  • You could create your own dialog form instead, and add a Timer to it.

    Public Class MyDialogForm
      Private WithEvents tm As New Timer
      Private Sub MyDialogForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        timer.Interval = 3000
        timer.Start()
      End Sub
      Private Sub timer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles timer.Tick
        Close()
      End Sub
    End Class


    remember make the reply as answer and vote the reply as helpful if it helps.

    Tuesday, July 31, 2018 2:21 AM
  • There is a nice dialog available via NuGet. The samples are in C# (full source) but here are two in VB.NET, first ask a question, closes if no response in 2.5 seconds, second is an alert, closes in 1 second if no response. You control the timeout.

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim toBeOrNotToBeQuestion = AutoClosingMessageBox.
                    Factory(showMethod:=Function(caption, buttons)
                                            Return MessageBox.Show(Me, "To be or not to be?", caption, buttons, MessageBoxIcon.Question)
                                        End Function, caption:="The question")
            If DialogResult.Yes = toBeOrNotToBeQuestion.Show(timeout:=2500, buttons:=MessageBoxButtons.YesNo, defaultResult:=DialogResult.No) Then
                MessageBox.Show("Yes")
            Else
                MessageBox.Show("No")
            End If
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            AutoClosingMessageBox.
                Factory(showMethod:=Function(caption, buttons)
                                        Return MessageBox.Show(Me, "To be or not to be?",
                                                               caption, buttons, MessageBoxIcon.Exclamation)
                                    End Function, caption:="Alert").Show(timeout:=1000, buttons:=MessageBoxButtons.OK)
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Tuesday, July 31, 2018 2:40 AM
    Moderator
  • Hi

    I hope the code can help you.

    Imports System.Runtime.InteropServices
    
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        End Sub
        Private _caption As String
    
        Public Sub ShowM(ByVal text As String, ByVal caption As String, ByVal timeout As Integer)
            Me._caption = caption
            StartTimer(timeout)
            MsgBox(text, caption)
        End Sub
    
        Private Sub StartTimer(ByVal interval As Integer)
            Dim timer As Timer = New Timer()
            timer.Interval = interval
            'timer.Tick += New EventHandler(AddressOf Timer_Tick)
            AddHandler timer.Tick, AddressOf Timer_Tick
            timer.Enabled = True
        End Sub
    
        Private Sub Timer_Tick(ByVal sender As Object, ByVal e As EventArgs)
            KillMessageBox()
            CType(sender, Timer).Enabled = False
        End Sub
    
        <DllImport("user32.dll", EntryPoint:="FindWindow", CharSet:=CharSet.Auto)>
        Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
        End Function
        <DllImport("user32.dll", CharSet:=CharSet.Auto)>
        Public Shared Function PostMessage(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
        End Function
        Public Const WM_CLOSE As Integer = &H10
    
        Private Sub KillMessageBox()
            Dim ptr As IntPtr = FindWindow(Nothing, Me._caption)
    
            If ptr <> IntPtr.Zero Then
                PostMessage(ptr, WM_CLOSE, IntPtr.Zero, IntPtr.Zero)
            End If
        End Sub
    
    
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, July 31, 2018 6:02 AM
  • Hi

    Do you resolve the issue? If you resolve the issue, could you please mark the helpful as answer. it will be beneficial to other community.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, August 2, 2018 2:01 AM