Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Newbie  »  Custom Message Box
 »  Home  »  Windows Development  »  Custom Message Box
Custom Message Box
by George Poth | Published  01/30/2004 | .NET Newbie Windows Development | Rating:
George Poth

I have been teaching English in Brazil since 1994 and always wanted to do more for learners than common textbooks can offer. This started with web sites that couldn't reach most students as computers and the Internet are not standard for most people in this country.

Computer tools to help Brazilian students learn a complex language like English are practically non-existent and so I sent some suggestions to software companies. Since Brazil is neither a target market for English textbooks nor for software of this kind, the rejection seemed natural.

As a result, I tried some free developer tools such as Borland's free C++ compiler, Free Pascal, and Envelope's Visual Basic. Envelope's Visual Basic, which is a Microsoft Visual Basic 1.0 clone and still available, suited my taste but I knew it was obsolete technology. In March 2003, I bought a copy of Microsoft Visual Basic .NET Standard and have been hopelessly contaminated with the programming virus ever since.

I mostly write programs for educational purposes. Having discovered the wonderful world of DirectX recently, I am diving into the most entertaining part of programming: games. One can connect teaching with pure entertainment, learning, and culture.

 

View all articles by George Poth...
Custom Message Box

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/ObjectSuggested NameSuggested Text
Form1frmStartupCustom Message Box
Button1btnShowSampleShow Sample

Control/ObjectSuggested NameSuggested TextDialogResult
Form2frmMessageBoxMy Message Box-
Label1lblMessage--
Button1btnYesGet me out of here!Yes
Button2btnNoNo way!No
Timer1tmrTextBlink--

If you want to have the same result as shown in Figure 02, you need to change some other properties, too.

Control/ObjectBackColorBorderStyleFontTextAlign
lblMessageFloralWhiteFixedSingleVerdana, Bold, ItalicMiddleCenter

Control/ObjectBackColorFont
btnYesFloralWhiteVerdana, Italic
btnNoFloralWhiteVerdana, 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.EventArgsHandles MyBase.Load

    '[Paint gradient form]
    SetStyle(ControlStyles.AllPaintingInWmPaint Or _
                ControlStyles.DoubleBuffer Or _
                ControlStyles.ResizeRedraw Or _
                ControlStyles.UserPaintTrue)
    '[/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(00), New _
          Point(Width0), Color.WhiteColor.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.EventArgsHandles 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.EventArgsHandles 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.

How would you rate the quality of this article?
1 2 3 4 5
Poor Excellent
Tell us why you rated this way (optional):

Article Rating
The average rating is: No-one else has rated this article yet.

Article rating:3.6772727272727 out of 5
 220 people have rated this page
Article Score78404
Sponsored Links