How to customize a "MsgBox" control in Visual Basic
Solution 1
You can use the MessageBox
function and its many variables. Here's an example of what it can do:
MessageBox.Show("The Displayed text in the messagebox", _
"the text displayed in the title bar", MessageBoxButtons.YesNoCancel, _
MessageBoxIcon.Error, MessageBoxDefaultButton.Button2)
Or you can still use MsgBox
and use its variables, though it gives you fewer options. Here's an example:
MsgBox("message text", MsgBoxStyle.Information, "title bar text")
Solution 2
You are using a reference to the MessageBox
class without specifying the Method
you want to call. You cannot create an instance of MessageBox
and therefore cannot pass a string as parameter to try and create one.
Use MessageBox.Show(string messageText)
to display the MessageBox
with the desired message.
As for your question, you should create your own MessageBox
class. With this solution, you get all the options you want and can customize it completely. The call will be a little different :
//C#
var myCustomMessageBox = new CustomMessageBox();
myCustomMessageBox.ShowDialog();
//Vb
Dim myCustomMessageBox As New CustomMessageBox()
myCustomMessageBox.ShowDialog()
The ShowDialog() will be used to create the effect of a messagebox.
Gavin
Updated on July 07, 2022Comments
-
Gavin almost 2 years
Is there a way to customize the MsgBox control in Visual Basic?
I use it quite often to alert users. However it never pops up on the screen; it just appears along the bottom task bar. It also always has a heading similar to "App_data_xxx". Can I improve this in any way?
My searches aren't throwing up much help.
For example:
' Queries user number and password against the database and returns user Dim matchingusers = From u In db.Users Where username = u.Email And password = u.Password Select u ' Checks if a valid user was returned If matchingusers.Count = 0 Then MsgBox("Invalid user entered, try again") Else SelectedDeveloper = 0 ' Set the logged in user for use in the project GlobalVariables.LoggedInUser = matchingusers.First
-
Sam about 11 yearsFor full customization just make a separate form, then make it visible when ever you want to alert the user...
-
user1703401 about 11 yearsMsgBox is the olden function, MessageBox is the newer one with more options. But even MsgBox has a Title argument, hard to not discover that. You are probably using it inappropriately if you feel a need to customize it. Its in-your-face reporting is pretty grating to a user, only use it sparingly and only for grave problems. Or you run the risk of the user routinely dismissing the box without reading it, missing the important ones. Lots of lesser evils available in a GUI class library.
-
dbasnett about 11 yearsIf you are using vb .net use MessageBox. If that doesn't have what you want make your own form.
-
Matt Wilko about 11 yearsCan you post the code you are using to show it
-
Gavin about 11 yearsi have edited post to include some code. When I try to use
MessageBox
I get "Cannot resolve Symbol MessageBox" -
SysDragon about 11 yearsCall it
Messagebox.Show()
. Check here the MSDN Documentation.
-