views:

8154

answers:

6

Is there an easy way to display a messagebox in VB.Net with custom button captions? I came across this, in the SO archives, but it's for managed C++

http://stackoverflow.com/questions/77293/what-is-an-easy-way-to-create-a-messagebox-with-custom-button-text-in-managed-c

+1  A: 

No.

You'll have to make a custom form with FormBorderType = FixedDialog

Here is a little tutorial: http://www.techexams.net/blogs/70-526/creating-dialog-boxes-in-net

BradC
A: 

i'll give it a shot - thanks

subrama6
+2  A: 

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!

PersistenceOfVision
A: 

is someone is looking for a C# code to accomplish the same thing, you can find it in an article under MSDN forum

balexandre
+2  A: 

Add a new class to your project and paste the code shown below. Invoke it with code like this:

Nobugz.PatchMsgBox(New String() {"Da", "Njet"})
MsgBox("gack", MsgBoxStyle.YesNo)

Here's the code:

Imports System.Text
Imports System.Runtime.InteropServices

Public Class Nobugz
  Private Shared mLabels() As String    '' Desired new labels
  Private Shared mLabelIndex As Integer '' Next caption to update

  Public Shared Sub PatchMsgBox(ByVal labels() As String)
    ''--- Updates message box buttons
    mLabels = labels
    Application.OpenForms(0).BeginInvoke(New FindWindowDelegate(AddressOf FindMsgBox), GetCurrentThreadId())
  End Sub

  Private Shared Sub FindMsgBox(ByVal tid As Integer)
    ''--- Enumerate the windows owned by the UI thread
    EnumThreadWindows(tid, AddressOf EnumWindow, IntPtr.Zero)
  End Sub

  Private Shared Function EnumWindow(ByVal hWnd As IntPtr, ByVal lp As IntPtr) As Boolean
    ''--- Is this the message box?
    Dim sb As New StringBuilder(256)
    GetClassName(hWnd, sb, sb.Capacity)
    If sb.ToString() <> "#32770" Then Return True
    ''--- Got it, now find the buttons
    mLabelIndex = 0
    EnumChildWindows(hWnd, AddressOf FindButtons, IntPtr.Zero)
    Return False
  End Function

  Private Shared Function FindButtons(ByVal hWnd As IntPtr, ByVal lp As IntPtr) As Boolean
    Dim sb As New StringBuilder(256)
    GetClassName(hWnd, sb, sb.Capacity)
    If sb.ToString() = "Button" And mLabelIndex <= UBound(mLabels) Then
      ''--- Got one, update text
      SetWindowText(hWnd, mLabels(mLabelIndex))
      mLabelIndex += 1
    End If
    Return True
  End Function

  ''--- P/Invoke declarations
  Private Delegate Sub FindWindowDelegate(ByVal tid As Integer)
  Private Delegate Function EnumWindowDelegate(ByVal hWnd As IntPtr, ByVal lp As IntPtr) As Boolean
  Private Declare Auto Function EnumThreadWindows Lib "user32.dll" (ByVal tid As Integer, ByVal callback As EnumWindowDelegate, ByVal lp As IntPtr) As Boolean
  Private Declare Auto Function EnumChildWindows Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal callback As EnumWindowDelegate, ByVal lp As IntPtr) As Boolean
  Private Declare Auto Function GetClassName Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal name As StringBuilder, ByVal maxlen As Integer) As Integer
  Private Declare Auto Function GetCurrentThreadId Lib "kernel32.dll" () As Integer
  Private Declare Auto Function SetWindowText Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal text As String) As Boolean
End Class
Hans Passant
A: 

There is a solution -- via installing CBT hook it is possible to adjust on the fly wide variety of MessageBox visual settings: 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

Fully-functional trial version, regular version includes complete C# source code.

Anatoliy Mogylevets