How to display a avoidable message box in vb.net

sudhanshu22

Member
Joined
Nov 8, 2006
Messages
20
Programming Experience
Beginner
Hi all,
I would like to display a messagebox in vb.net that can be avoided.
When you display a messagebox like below
MessageBox.Show("Message Text", "Message Title", MessageBoxButtons.YesNo, MessageBoxIcon.None)

then, user has to click on yes/no, else the program will not move forward.
I want to display a message box in the form of warning such that evenif the messagebox occurs, the program continues whether user clicks on yes/no or not.
Some solution in the form of a small code would be appreciated.
Thanks
Sudhanshu
 

vis781

Well-known member
Joined
Aug 30, 2005
Messages
2,015
Location
Cambridge, UK
Programming Experience
5-10
Well that is kinda the point of a messagebox. It is designed to halt user interaction until such time that coonditions have been met i.e clicking a button. To do what you are asking you may just as well create a 'new' type of messagebox from a form plonk a couple of buttons on it and just don't display it modally.
 

sudhanshu22

Member
Joined
Nov 8, 2006
Messages
20
Programming Experience
Beginner
Thanks Vis,
I am kind of a new guy in vb.net and don't understand the term you said "don't display modally". Can you please elaborate more.

Again thanks for such a quick response..........
 

sudhanshu22

Member
Joined
Nov 8, 2006
Messages
20
Programming Experience
Beginner
I want a simple solution,without any threads etc. Only tell me how to implement the form in a modal thing etc.
How can we implement this thing in a safe and simple manner.
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,604
Location
Norway
Programming Experience
10+
Messagebox can't be cancelled from code. Instead create a new form to be used as warning dialog, for example name it WarningForm. Call a new thread to display it for some time, then close it if user didn't already. Example code, I use a delegate to pass the message parameter:
VB.NET:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
    showWarning("this is a warning")
End Sub
 
Sub showWarning(ByVal message As String)
    Dim d As New timeoutWarningDelegate(AddressOf timeoutWarning)
    d.BeginInvoke(message, Nothing, Nothing)
End Sub
 
Delegate Sub timeoutWarningDelegate(ByVal message As String)
 
Sub timeoutWarning(ByVal message As String)
    Dim f As New WarningForm(message)
    f.Show()
    For i As Short = 1 To 18
        Threading.Thread.Sleep(333)
        Application.DoEvents()
        If f.IsDisposed Then Exit Sub
    Next
    f.Close()
End Sub
Notice the form contructor used above where the message is passed, you can do this by writing a Sub New for the WarningForm that accepts a string parameter and use it to set the label text like this:
VB.NET:
Public Sub New(ByVal message As String)
    ' This call is required by the Windows Form Designer.
    InitializeComponent()
    ' Add any initialization after the InitializeComponent() call.
    Label1.text = message
End Sub
 
Top Bottom