Is there an easy way to display a messagebox in VB.NET with custom button captions? I came across What is an easy way to create a MessageBox with custom button text in Managed C++?, in the Stack Overflow archives, but it's for Managed C++.
相关问题
- Generic Generics in Managed C++
- How to Debug/Register a Permanent WMI Event Which
- 'System.Threading.ThreadAbortException' in
- Bulk update SQL Server C#
- Should I use static function in c# where many call
Here is a C# snippet that uses a Win32 hook to alter the button captions (sourced from http://icodesnip.com/snippet/csharp/custom-messagebox-buttons):
The solution by Daniel Nolan, code in VB.Net
No there is no method to access or redirect the Messagebox's default button text.
The only way to do this is to code your own or just use one of many free ones from the internet:
Free MsgBoxGo!
There is a solution. Via installing a CBT hook it is possible to adjust a wide variety of MessageBox visual settings on the fly: message and button fonts, dialog background, dialog positioning, icons, button captions, timeout, even inserting additional controls.
Complete solution: Extended MessageBox .NET Assembly http://www.news2news.com/vfp/?solution=5
It is a fully-functional trial version, the regular version includes complete C# source code.
MessageBox uses a plain window that can be messed with like any other window. This has been possible in Windows for a very long time, well over 20 years already. The techniques are getting obscure though, too many friendly class wrappers that hide the native winapi and don't expose everything you can do with it. So much so that programmers now automatically assume that this isn't possible, as you can tell from the upvoted answers. It is the kind of programming that Petzold taught us in his seminal "Programming Windows" book. Replacing MessageBox with a custom Form or Window is actually fairly hard to do, it does non-trivial automatic layout to fit the text and supports localization without help. Although that's exactly what you don't seem to like :)
Anyhoo, the message box window is easy to find back. It is owned by the UI thread and has a special class name that makes it unique. EnumThreadWindows() enumerates the windows owned by a thread, GetClassName() lets you check the kind of window. Then just poke the text into the button with SetWindowText().
Add a new class to your project and paste the code shown below. Invoke it with code like this:
Here's the code:
No.
You'll have to make a custom form with
FormBorderType = FixedDialog
.Here is a little tutorial:
Creating Dialog Boxes in .NET
by James D. Murray on Jun.12, 2007, under 70-526
Microsoft Certification Exam: 70-526 (MCTS)
Objective: Create and use custom dialog boxes in Windows Forms applications.
Language: Visual Basic 2005 (click here for the C# version of this entry)
I remember the first time I needed to create a dialog box in a .NET application that I was writing in C#. Being a long-time Visual Basic programmer, I assumed that this could easily be accomplished by using a dialog box template included with Visual Studio.NET. To my surprise, no such form template existed for C#, although one does for Visual Basic 2005. After wading through several books and Web pages filled with information on Windows Forms 2.0 programming, a basic set of steps became apparent to me for manually converting a .NET form into a Windows dialog box:
Step 1 : Add a Form to your .NET project and name it “DialogBoxForm”.
Step 2 : Drop two buttons in the lower right-hand area of the Form and name them “OKButton” and “CancelButton”.
Step 3 : Change the following properties of the Form to adjust its appearance and behavior to be like a standard dialog box:
These properties can be set using the Properties window for the form, or using code placed in the Form’s Load event:
Step 4 : Add the following button click event handlers to the Form:
Step 5 : Add properties that you need to move data into and out of the dialog box as you would for any Form:
Step 6 : Show the dialog box modally by calling the ShowDialog() of the form:
Step 7 : To show the dialog box modelessly, call the Show() method of DialogBoxForm instead. You will need to add an event handler to the Close event of DialogBoxForm to know when the user closes the dialog box: