We may want it dynamic, more animated, .... etc
Here is a custom message box library I created as an example of what you can do
you can then use this dll in all your projects as well
Let's Begin:
What I wanted to do with my message box is:
1- The message appears letter by letter in a nice animation
2- The picture in the box not to be one still picture, I wanted to select a path and view all the pictures in a sequence (just like GIF files)
3- I wanted to create custom buttons
I. First of all: (Creating the Form)

I created the form (msgfrm) and changed the opacity to 70%
included a picture box (pbPics)
and a label to include the message (lblMsg)
finally two timers to use in our animations one for text(Timer1) animation and the other for pictures animation (Timer2)
II. Creating a Global class of variables:
I created all.vb as a class containing all variables I need
Module all
Public title As String ' Title of the message box
Public list As New ArrayList ' List of pictures to view
Public message As String ' The message contained in the message box
Public temp As Char() ' array of characters for the message body
Public count1 As Integer = 0 ' Counter for the text timer
Public count2 As Integer = 0 ' counter for the picture timer
Public result As String ' name of the button clicked by the user
Public path As String ' path of the folder for pictures
Public type As String ' type of the custom message
Public names As New ArrayList ' array containing names of buttons to create
Public xcenter, ycenter As Integer ' position of the form
Public filename As String '
Public x, y As Integer ' variables to adjust buttons positions
Public btns ' array of buttons
Public clicked As Boolean = False ' to show a button is clicked
End Module
I think the comments are self-explaining
III. Creating The Message box class
as we will use forms and file system so we will import three name spaces
Imports System.IO Imports System.Windows.Forms Imports System.Drawing
I declared some variables to use through the class methods
Public Class Msg
Public messageBox As New msgfrm ' Creates a new instance (Object) from the form
' Enum to choose type of message buttons (you can create more if you wish)
Enum CustomType
OK = 1
OkCancel = 2
YesNo = 3
Custom = 4
End Enum
I created two overloaded functions to show the custom message
The first one is taking 4 parameters:
1- Title
2- Message
3- Path to pictures
4- Type
Public Function AniMsg(ByVal titlevar As String, ByVal MessageVar As String, ByVal PathVar As String, ByVal MsgType As CustomType) As String
title = titlevar ' set the title of the message
message = MessageVar ' set the body of the message
path = PathVar ' set the path of the pictures
type = MsgType ' set the type of the message
messageBox.Text = title ' Rename the title to the new title
' Set the messagebox position as the center of the screen
xcenter = (Screen.PrimaryScreen.WorkingArea.Width - messageBox.Width) / 2
ycenter = (Screen.PrimaryScreen.WorkingArea.Height - messageBox.Height) / 2
messageBox.Left = xcenter
messageBox.Top = ycenter
' Call the method responsible for showing body of the message
ShowMsg()
' Call the method responsible for showing the pictures
ShowPic()
' clears the buttons in the array
names.Clear()
' set the button names depending on the type chosen
Select Case type
Case CustomType.OK
names.Add("Ok")
Case CustomType.OkCancel
names.Add("Ok")
names.Add("Cancel")
Case CustomType.YesNo
names.Add("Yes")
names.Add("No")
End Select
' Create the buttons
ShowBtn()
' finally show the message box
messageBox.ShowDialog()
Return result ' returns the button name that was clicked by the user
End Function
The second one taking 7 parameters:
1- Title
2- Message
3- Path to pictures
4- Type
5- Custom Button Names
6- Top position
7- Left position
Public Function AniMsg(ByVal titlevar As String, ByVal MessageVar As String, ByVal PathVar As String, ByVal MsgType As CustomType, ByVal CustomAnswers As String(), ByVal Top As Integer, ByVal Left As Integer) As String
title = titlevar ' set the title of the message
message = MessageVar ' set the body of the message
path = PathVar ' set the path of the pictures
type = MsgType ' set the type of the message
messageBox.Text = title ' Rename the title to the new title
' Set the messagebox position as the specified parameters
xcenter = Left
ycenter = Top
MessageBox.Left = xcenter
MessageBox.Top = ycenter
' Call the method responsible for showing body of the message
ShowPic()
' Call the method responsible for showing the pictures
ShowMsg()
' clears the buttons in the array
names.Clear()
' set the button names depending on the type chosen
Select Case MsgType
Case CustomType.OK
names.Add("Ok")
Case CustomType.OkCancel
names.Add("Ok")
names.Add("Cancel")
Case CustomType.YesNo
names.Add("Yes")
names.Add("No")
Case CustomType.Custom ' if the user chose Custom type then he has to pass the answers array containing custom buttons
For Each t In CustomAnswers
names.Add(t)
Next
End Select
' Create the buttons
ShowBtn()
' finally show the message box
messageBox.ShowDialog()
Return result ' returns the button name that was clicked by the user
End Function
Then to the sub that creates the body of the message
Public Sub ShowMsg()
' make sure that the message height doesn't exceed the height of the form
If message.Length > 656 Then
' if so then increase the height of the form
' we divide the remaining of the message by 82 (the message single line height by experience for the selected font)
messageBox.Height += Math.Ceiling(((message.Length - 656) / 82))
End If
' make sure that the message doesn't exceed the label size
If message.Length > 82 Then
' if so then increase label height
' we increase it per message line height
messageBox.lblMsg.Height *= Math.Ceiling(message.Length / 82)
End If
messageBox.Text = ""
' convert the message to array of characters so we can show one letter in time
temp = message.ToCharArray()
' set the counter to zero
count1 = 0
' start the timer to animate text
messageBox.Timer1.Start()
End Sub
and here is the timer function
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim x As Char ' the character to be typed each timer tick
' if the counter exceeds the message length then stop
If count1 > (message.Length - 1) Then
Timer1.Stop()
Return
End If
' gets the character at the current position
x = temp(count1)
' add the character to the label
lblMsg.Text += x
count1 += 1 ' increase the counter
End Sub
Now the sub to show the pictures in the picture box
Public Sub ShowPic()
' make sure that the folder selected is valid
If Directory.Exists(path) Then
list.Clear() ' clears the picture list
For Each fi In Directory.GetFiles(path)
list.Add(fi) ' add files in this folder to the list
Next
' set the counter to zero
count2 = 0
' start the timer
messageBox.Timer2.Start()
' if this wasn't a directory then maybe a single file
ElseIf File.Exists(path) Then
' gets the file name of the picture
filename = path.Substring(path.LastIndexOf("\") + 1)
' then draw it to the picture box
messageBox.pbPics.Image = System.Drawing.Bitmap.FromFile(path)
End If
End Sub
And here is the timer of the pictures
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
' if the counter reached the end of the list then restart it from the first
If count2 = list.Count Then
count2 = 0
End If
' assign each picture to the picture box for this tick
Try
pbPics.Image = System.Drawing.Bitmap.FromFile(list(count2))
Catch ex As OutOfMemoryException
count2 += 1
End Try
' increase the counter
count2 += 1
End Sub
till now we showed the pictures and the body in a beautiful way
the missing is the buttons of the message
Public Sub ShowBtn()
' get the x position of the first button created to be the same x as the message label
' and the y co-ordinate to be below the message by 10 pixels
x = messageBox.lblMsg.Location.X
y = messageBox.lblMsg.Location.Y + messageBox.lblMsg.Size.Height + 10
' creates an array of buttons
' we are subtracting one as when you create the names array list, a null value is added to the last of the list
Dim btns(names.Count - 1) As Button
' Loop to create buttons
Dim i As Integer
For i = 0 To btns.Count - 1
btns(i) = New Button() ' create a new instance of the button
btns(i).Text = names(i) ' get the name of the button
btns(i).Size = New Size(btns(i).Text.Length * 15, 25) ' assign the length of the button to its contents, but with a constant height
AddHandler btns(i).Click, AddressOf BtnClick ' add an event handler to the button click
' after creating the first button
If i > 0 Then
' check if the width of the form can create another button
If (messageBox.Width - (btns(i - 1).Location.X + btns(i - 1).Size.Width + 10)) < btns(i).Size.Width Then
' if not, then set the x to the original x
x = messageBox.lblMsg.Location.X
' and the y to a position under the last created button
y = messageBox.lblMsg.Location.Y + messageBox.lblMsg.Size.Height + 38
End If
End If
' set the button location
btns(i).Location = New Point(x, y)
messageBox.Controls.Add(btns(i))
' add the x by 10 pixels to draw the next button
x += 10 + btns(i).Size.Width
Next
End Sub
here is the button click event handler
Public Sub BtnClick(ByVal sender As Object, ByVal e As EventArgs)
' set the name of the button to the result
result = CType(sender, Button).Text
' set clicked to be True
clicked = True
' close the form
messageBox.Close()
End Sub
We are finished
now build the library and move on to the next step
IV. Try the library
take the dll file and import it into a new project by adding it as a reference
I created a form with a button and here is the button click
Imports CustomMessage
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim title, message, path As String
Dim top, left As Integer
Dim buttons(0 To 3) As String
title = "Ahmedn1"
message = "This is a test message for the Custom message class,\nYou can try any thing in this dll (namespace),\nTrust me it is very useful"
path = "D:\Images\Anime"
top = 100
left = 200
buttons(0) = "Ok"
buttons(1) = "Cancel"
buttons(2) = "Go away"
buttons(3) = "Sure?!"
Dim myMsg As New Msg
MessageBox.Show(myMsg.AniMsg("Ahmedn1", message, path, Msg.CustomType.Custom, buttons, 100, 100))
End Sub
End Class
try it and you can see the result as I can't attach it as a picture because the result is an animated message box
P.S.: you can change the timers intervals as you wish
I hope this was a useful tutorial for you as it is my first






MultiQuote






|