How To Customize A "MsgBox" Control In Visual Basic - Stack Overflow
Có thể bạn quan tâm
-
- Home
- Questions
- Tags
- Users
- Companies
- Labs
- Jobs
- Discussions
- Collectives
-
Communities for your favorite technologies. Explore all Collectives
- Teams
Ask questions, find answers and collaborate at work with Stack Overflow for Teams.
Try Teams for free Explore Teams - Teams
-
Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams
Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about CollectivesTeams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about TeamsGet early access and see previews of new features.
Learn more about Labs How to customize a "MsgBox" control in Visual Basic Ask Question Asked 11 years, 7 months ago Modified 9 years, 10 months ago Viewed 50k times 0Is 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 Share Improve this question Follow edited Jan 13, 2015 at 11:11 Peter Mortensen 31.6k22 gold badges109 silver badges133 bronze badges asked Apr 15, 2013 at 12:21 GavinGavin 4372 gold badges8 silver badges20 bronze badges 6- 1 For full customization just make a separate form, then make it visible when ever you want to alert the user... – Sam Commented Apr 15, 2013 at 12:29
- 4 MsgBox 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. – Hans Passant Commented Apr 15, 2013 at 12:29
- If you are using vb .net use MessageBox. If that doesn't have what you want make your own form. – dbasnett Commented Apr 15, 2013 at 12:29
- Can you post the code you are using to show it – Matt Wilko Commented Apr 15, 2013 at 13:51
- i have edited post to include some code. When I try to use MessageBox I get "Cannot resolve Symbol MessageBox" – Gavin Commented Apr 15, 2013 at 14:23
3 Answers
Sorted by: Reset to default Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 3You 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") Share Improve this answer Follow edited Jan 13, 2015 at 16:43 Peter Mortensen 31.6k22 gold badges109 silver badges133 bronze badges answered Apr 15, 2013 at 19:22 Ayman El TemsahiAyman El Temsahi 2,6102 gold badges18 silver badges29 bronze badges Add a comment | 2You 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.
Share Improve this answer Follow answered Apr 15, 2013 at 15:22 phadaphunkphadaphunk 13.2k16 gold badges75 silver badges108 bronze badges 0 Add a comment | 0In Visual Basic 2008 I had a requirement for a message box that would only stay on for short times and that the time it was on to be variable. I also had the problem that when using extended screen that the msgbox showed up on the extended screen (which was a different form) instead of on the main computer screen. To overcome these problems I created a custom Message Box using a panel on the form I had on the main screen.
To call the message panel DoMessage("The message", Seconds, Buttons to show 1 = Ok only 2 = Yes(ok) and No, 3 = Yes, No and Cancel. If seconds is 0 or not specified then the time to show the panel is set to a long time (10000 seconds) If Buttons to show is not specified it is set to Ok button only. If seconds and buttons are both specified, If no button is clicked the panel will just hide after the timeout.
The responses are 1 if Ok or Yes is clicked, 2 if No is clicked, 3 if Cancel is clicked It is put into DoMsgResp so you see what is in it to handle the response.
Create the message panel when opening the form by calling MakeMsgPanel()
Dim MessagePanel As New Panel 'The panel Dim MessageLabel As New Label 'The message Dim MsgYes As New Button 'Yes or OK button Dim MsgNo As New Button 'no button Dim MsgCcl As New Button 'Cancel button Dim Sleepsecs As Integer 'How long panel shows for Dim DoMsgResp As Integer 'response 1, 2 or 3 depending which button clicked Private Sub MakeMsgPanel() Me.Controls.Add(MessagePanel) Me.MessagePanel.Controls.Add(MessageLabel) Me.MessagePanel.Controls.Add(MsgYes) Me.MessagePanel.Controls.Add(MsgNo) Me.MessagePanel.Controls.Add(MsgCcl) MessagePanel.Location = New System.Drawing.Point(Me.Width / 2 - 200, Me.Height / 2 - 100) MessagePanel.BackColor = Color.PaleGreen MessageLabel.BackColor = Color.PeachPuff MessagePanel.BorderStyle = BorderStyle.FixedSingle MessageLabel.Font = New Font("Arial", 12, FontStyle.Regular, GraphicsUnit.Point) MessageLabel.AutoSize = True MessagePanel.AutoSize = True MessagePanel.AutoSizeMode = Windows.Forms.AutoSizeMode.GrowOnly MessagePanel.Hide() MsgYes.Location = New System.Drawing.Point(205, 5) MsgNo.Location = New System.Drawing.Point(115, 5) MsgCcl.Location = New System.Drawing.Point(25, 5) MsgYes.Text = "Yes" MsgNo.Text = "No" MsgCcl.Text = "Cancel" AddHandler MsgYes.Click, AddressOf MsgYes_Click AddHandler MsgNo.Click, AddressOf MsgNo_Click AddHandler MsgCcl.Click, AddressOf MsgCcl_Click End Sub Private Sub MsgYes_Click() DoMsgResp = 1 Sleepsecs = 0 End Sub Private Sub MsgNo_Click() DoMsgResp = 2 Sleepsecs = 0 End Sub Private Sub MsgCcl_Click() DoMsgResp = 3 Sleepsecs = 0 End Sub Private Sub DoMessage(ByVal Msg As String, Optional ByVal Secs As Integer = 0, _ Optional ByVal Btns As Integer = 0) 'Information messages that can be timed Dim TheHeight As Integer Dim TheWidth As Integer Dim Labelx As Integer Dim Labely As Integer DoMsgResp = 0 MessageLabel.Text = Msg If MessageLabel.Height < 90 Then TheHeight = 100 Labely = (100 - MessageLabel.Height) / 2 Else TheHeight = MessageLabel.Height + 10 Labely = 5 End If If MessageLabel.Width < 140 Then TheWidth = 150 Labelx = (150 - MessageLabel.Width) / 2 Else TheWidth = MessageLabel.Width + 10 Labelx = 5 End If MessagePanel.Size = New System.Drawing.Size(TheWidth, TheHeight) MessageLabel.Location = New System.Drawing.Point(Labelx, Labely) MessageLabel.Show() MessagePanel.Show() MessagePanel.BringToFront() MsgYes.BringToFront() MsgNo.BringToFront() MsgCcl.BringToFront() MessagePanel.Focus() If Btns = 0 Or Btns > 3 Then Btns = 1 'Make ok button if none specified or number too high If Btns = 1 Then MsgYes.Text = "Ok" MsgNo.Hide() MsgCcl.Hide() Else 'is 2 or 3 MsgYes.Text = "Yes" MsgNo.Show() If Btns = 2 Then MsgCcl.Hide() Else MsgCcl.Show() End If If Secs = 0 Then Secs = 10000 'make a long time If Secs > 0 Then Sleepsecs = Secs * 2 Do Until Sleepsecs < 1 Threading.Thread.Sleep(500) Application.DoEvents() Application.RaiseIdle(New System.EventArgs) Sleepsecs = Sleepsecs - 1 Loop End If MessagePanel.Hide() End Sub Private Sub ButtonTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonTest.Click DoMessage("This is To see what happens with my message" & vbCrLf & _ "see if it works good", 0, 3) If DoMsgResp = 1 Then MsgBox("Ok was hit") End If If DoMsgResp = 2 Then MsgBox("No was hit") End If If DoMsgResp = 3 Then MsgBox("Cancel was hit") End If End Sub Share Improve this answer Follow answered Jul 4, 2014 at 7:41 Graeme WallaceGraeme Wallace 1 Add a comment |Your Answer
Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Draft saved Draft discardedSign up or log in
Sign up using Google Sign up using Email and Password SubmitPost as a guest
Name EmailRequired, but never shown
Post Your Answer DiscardBy clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Not the answer you're looking for? Browse other questions tagged
or ask your own question.- The Overflow Blog
- Your docs are your infrastructure
- Featured on Meta
- More network sites to see advertising test [updated with phase 2]
- We’re (finally!) going to the cloud!
- Call for testers for an early access release of a Stack Overflow extension...
Related
0 how to change color of msgbox in vb6? 0 How do I go about writing new controls in VB6? 1 How to add a control in vb.net? 2 How can I control the instrument “msgbox” in VB. net 0 How would I make a different MsgBox appear after the user saw the first one? 1 MsgBox with details window 0 How to customize a textbox 1 C# Special messagebox (or input box) 0 How to change buttons on a msgbox using a combo box in visual basic 2010 express? 3 simple dialog like msgbox with custom buttons (vb)Hot Network Questions
- How can I politely decline a request to join my project by a free rider professor
- Why don't routers answer ARP requests for IP addresses they can handle even if they aren't assigned that IP address themselves?
- Middle school geometry problem about a triangle inscribed in a circle with very particular properties
- What mechanism could cause a person not to cast a reflection?
- Bash builtin 'command' ignoring option '-p'
- Why is it safe to soak an electric motor in isopropyl alcohol but not distilled water?
- Why is Anarchism not considered fundamentally against the "democratic order" in Germany?
- How can a scalar particle be differentiated, experimentally, from a vector particle? How can we tell?
- T47 to BSA bottom bracket adapter - good idea?
- Clarification and Proof of Inequality (8.11) in Analytic Number Theory by Iwaniec and Kowalski
- The British used to (still?) classify their guns by weight in pounds rather than caliber. Was that a constant across shell types?
- Table structure with multiple foreign keys and values
- In GR, what is Gravity? A force or curvature of spacetime?
- If scent means a pleasant smell, why do we say "lovely scent" or "sweet scent"?
- What is small arch between two notes and how to play it?
- Likely source of a hot-cold crossover?
- Schrödinger's cat ++
- A novel about Earth crossing a toxic cloud of cosmic size
- Meaning of "I love my love with an S—" in Richard Burton's "Arabian Nights"
- Why is a pure copper cathode necessary in the electrolytic refining of copper?
- What kind of solvent is effective for removing roofing tar from shoes?
- Is BNF grammar in TeXbook correct?
- What is 擦边 as in the case of the athlete champion turned dancer?
- How can I reference sky photos to a star map?
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
lang-vbTừ khóa » Visual Basic Msgbox Style
-
MsgBox Function (Visual Basic For Applications) | Microsoft Docs
-
MsgBoxStyle Enum (Microsoft.VisualBasic)
-
The Message Box - Visual Basic Functions - FunctionX
-
Visual Basic Built-In Functions: The Message Box - FunctionX
-
VBA MsgBox Excel Examples - 100+ Message Box Macros
-
Excel VBA MsgBox [Message Box] - All You Need To Know!
-
Add And Change Messagebox Style In Visual Basic - YouTube
-
VBA - Message Box - Tutorialspoint
-
MsgBox / MessageBox Demo - The VB Programmer
-
MsgBoxStyle - Wisej.NET API
-
VBA MSGBOX - A Complete Guide To Message Box Function + ...
-
The Msg Box Function - Visual Basic Planet
-
VBA Msgbox - A Complete Guide To The VBA Message Box