Who needs a customized message box anyway? You - you just don't know it yet. Take a look at Figure 01. Is this the kind of message box you like? If yes, you can go on to the next tutorial because that's not what I'm going to talk about. What I am going to talk about is what you can see in Figure 02.
(Figure 01)

(Figure 02)

Unfortunately, Figure 02 can't show you that the text is actually blinking. But I think you got interested and will soon go beyond this very simple example.
Some time ago I was "globalizing" an application to be run on operating systems in three languages: English, German, and Portuguese. Making my application "speak" three languages was no big problem, until I came to the message boxes. I really couldn't get those message boxes to show messages in German and Portuguese. The only way around this was to build my own message boxes.
But why should you - a hobby programmer - worry about that? Think about the huge possibilities: you can speedily come up with message boxes matching the color of your program instead of the dull gray ones. Just think about the buttons: you aren't restricted to "Yes", "No", "Cancel", and the like. And why not put in a gradient background, or make the box round, oval, or whatever? Change the icon to something dynamic - .gif images make it possible.
You need to start a new project with two forms. The following charts give you the necessary specifications.
Control/Object | Suggested Name | Suggested Text |
Form1 | frmStartup | Custom Message Box |
Button1 | btnShowSample | Show Sample |
|
Control/Object | Suggested Name | Suggested Text | DialogResult |
Form2 | frmMessageBox | My Message Box | - |
Label1 | lblMessage | - | - |
Button1 | btnYes | Get me out of here! | Yes |
Button2 | btnNo | No way! | No |
Timer1 | tmrTextBlink | - | - |
|
If you want to have the same result as shown in Figure 02, you need to change some other properties, too.
Control/Object | BackColor | BorderStyle | Font | TextAlign |
lblMessage | FloralWhite | FixedSingle | Verdana, Bold, Italic | MiddleCenter |
|
Control/Object | BackColor | Font |
btnYes | FloralWhite | Verdana, Italic |
btnNo | FloralWhite | Verdana, Italic |
|
If you prefer to have the controls in the same color as the gradient form, please set their BackColor property to transparent. Click the column on the right where it reads "BackColor" to activate the drop down box. Click the arrow button and then the Web tab. By default, "Transparent" is the first entry.
Code (Gradient Background):
Press F7 to go into code view and type Code 01 at the top of the file so that these become the very first lines of your file.
Code 01
'[Import drawing]
Imports System.Drawing.Drawing2D
'[/Import drawing]
Select "(Base Class Events)" from the Class Name list and "Load" from the Method Name list. Edit the generated code so that it looks like Code 02.
Code 02
Private Sub frmMessageBox_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
'[Paint gradient form]
SetStyle(ControlStyles.AllPaintingInWmPaint Or _
ControlStyles.DoubleBuffer Or _
ControlStyles.ResizeRedraw Or _
ControlStyles.UserPaint, True)
'[/Paint gradient form]
End Sub
Right below the region "Windows Form Designer generated code" is the place to type Code 03. I have used white and deep sky blue for the gradient background, but you can change this according to your taste.
Code 03
Protected Overrides Sub OnPaint( _
ByVal e As System.Windows.Forms.PaintEventArgs)
'[Declare variables]
Dim formGraphics As Graphics = e.Graphics
Dim gradientBrush As New _
LinearGradientBrush(New Point(0, 0), New _
Point(Width, 0), Color.White, Color.DeepSkyBlue)
formGraphics.FillRectangle(gradientBrush, _
ClientRectangle)
'[/Declare variables]
End Sub
Code (frmMessageBox):
Add Code 04 so that it goes before the "End Sub" of Code 02.
Code 04
'[Enable the timer]
tmrTextBlink.Enabled = True
'[/Enable the timer]
Select "tmrTextBlink" from the Class Name list and "Tick" from the Method Name list. And if you have any other entry aside from "Tick" in the Method Name list, you should hurry to contact Microsoft - you have a yet undiscovered version of Visual Basic .NET. The comments should be sufficient so that you know what's going on.
Code 05
Private Sub tmrTextBlink_Tick(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles tmrTextBlink.Tick
'[Declaration: must be static]
Static NoText As Boolean
'[/Declaration: must be static]
'[If the boolean is true]
If NoText = True Then
'[/If the boolean is true]
'[Erase the text]
lblMessage.Text = ""
'[/Erase the text]
'[Make the boolean false]
NoText = False
'[/Make the boolean false]
'[If the boolean is false]
ElseIf NoText = False Then
'[/If the boolean is false]
'[Show the text in red]
lblMessage.Text = "Do you want to exit the program?"
lblMessage.ForeColor = Color.Red
'[/Show the text in red]
'[Make the boolean true]
NoText = True
'[/Make the boolean true]
End If
End Sub
Save the changes by pressing Ctrl + S. Close the "frmMessageBox" so that the open windows won't confuse you.
Code (frmStartup):
Select "btnShowSample" from the Class Name list and "Click" from the Method Name list. Edit the generated code so that it looks like Code 06.
Code 06
Private Sub btnShowSample_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnShowSample.Click
'[Show the custom message box]
Dim frmMessage As New frmMessageBox()
frmMessage.ShowDialog()
'[/Show the custom message box]
Select Case frmMessage.DialogResult
'[If the user clicked "yes"]
Case DialogResult.Yes
'[/If the user clicked "yes"]
'[Exit the program]
Dispose(True)
'[/Exit the program]
'[If the user clicked "no"]
Case DialogResult.No
'[/If the user clicked "no"]
'[Ignore it]
Exit Sub
'[/Ignore it]
End Select
End Sub
Although I think that the comments will tell you most of the code, you should remember that "DialogResult.Yes" and "DialogResult.No" are just examples. You can change these dialog results according how you set the dialog result properties for the buttons in the "frmMessageBox". Take a look there and you will find all common message box dialog results.
Press F5 to run the program and then click the "Show Sample" button. Our message box will pop up showing you a flashy message. Click the button that reads "No Way!" and you'll get back to the startup form since the dialog result was set to "No" for this button. Click the button that reads "Get me out of here!" and the program will close since the dialog result for this button was set to "Yes".
Our custom message box is exactly like the ones you use(d) in your programs. The only difference is that we added color, less formal text on the buttons, and a flashy message. You don't have to put up with tedious message boxes in a modern program anymore. But watch out: some stiff and mentally old people won't be fond of this just as younger folks find the ordinary message boxes a real bore. Experiment with this and you will see that it can add to your program.