In some cases it would be nice to have our own type of message box to display results.
For example, if you are creating a contact management system and a record has been saved successfully you would want to display the success message.
Now our project may not have any use in having another form created and displayed within our project so one approach we can take is to create and show the form only when needed.
Creating Our Module Structure
- From your solution explorer right click on your project name
- Click Add => New Folder
- Name it anything you want, i named mine Modules
- Right Click on the Modules folder or whatever you named it and choose Add => Module…
- Name it whatever you want, i just named mine”Added” without the double quotes
Once you click Add, your new Module should be created and should look like this
Now that we have our Empty Module created the first thing we need to do is add a new Method.
Within our Method will contain the code we want to execute when we call that method, so to create the method insert this line of code between the Module End Module block
Public Sub MsgAdded()
End Sub
MsgAdded is the method we will call on our submission from whatever form we want.
The next thing we need to do is create a new form instance, this will be the form that will be displayed when we call it.
Remember, this form will not be added to our project.
Dim AddedMessage As New Form
Now that we have added a declaration as a new form we now need to set some properties for that form, so lets do that.
AddedMessage.Name = "FormMessage"
AddedMessage.Text = "Contact Added"
AddedMessage.Size = New Size(313, 123)
AddedMessage.FormBorderStyle = FormBorderStyle.FixedDialog
AddedMessage.StartPosition = FormStartPosition.CenterScreen
AddedMessage.MinimizeBox = False
AddedMessage.MaximizeBox = False
AddedMessage.BackColor = Color.White
AddedMessage.ShowIcon = False
AddedMessage.Show()
AddedMessage.TopMost = True
Let me explain what these properties are and why they are set.
![]() |
VALUES |
AddedMessage.Name |
Identifies the control by the Name property |
AddedMessage.Text |
The text that is displayed in the title bar |
AddedMessage.Size |
Over rides the default form size, if not set, default size will be initiated |
AddedMessage.FormBorderStyle |
Sets the Border style of the fom |
AddedMessage.StartPosition |
Sets the positioning of the form when called |
AddedMessage.MinimizeBox |
Determines if the Minimize button is visible or not, Values are Boolean (True or False) |
AddedMessage.MaximizeBox |
Determines if the Maximize button is visible or not, Values are Boolean (True or False) |
AddedMessage.BackColor |
Sets the background color of the form |
AddedMessage.ShowIcon |
Determines weather to show the icon in the title bar or not, Values are Boolean(True or False) |
AddedMessage.TopMost |
Determines if the form should always remain over top of other windows |
Our new form is now set and can now be called by calling MsgAdded()
Now lets go a head and create our message to be displayed.
We are just going to use a Label control, and we are going to create this essentially the same way we did the form.
The first thing we need to do is create our Label to hold our message.
Insert this below AddedMessage.TopMost = True
Dim MessageLabel As New Label
Now we need to set some properties of our label.
MessageLabel.Name = "LabelMessage"
MessageLabel.Text = "Contact Has Been Saved Successfully"
MessageLabel.Width = AddedMessage.Width
MessageLabel.Location = New Point(55, 41)
Now that our label message has been created we have to tell the form to add the label to the form.
Under MessageLabel.Location = New Point(55, 41 insert this
AddedMessage.Controls.Add(MessageLabel)
Now when we call the new form, the form will add the Label control.
Remember, MessageLabel is the name of the label we created.
Anytime you add controls to a form this way you need to specify the Name of the control you want to be added inside the Controls.Add(ControlName) method.
So far we have accomplished the following tasks
- Created a new Form
- Set properties to our new form
- Created a message that will be stored in a label control
- Set the properties of the Label control
- Told our new form to add the Label control
The final task we need to is trigger a event to call our new form, this part is pretty easy and it can be used on any form you have in your project.
- Open your Form1
- Drag a button on to your form
- Double click on the button to create a Button1_Click event
- Type in the following code
MsgAdded()
Run the application (F5) or click the little Green Play Button at the top
When you click the button you will see your new form appear.
This concludes another Visual Basic tutorial.
I hope that you find use of this tutorial and can use this within your own project(s)



Recent Comments