Custom MessageBox buttons

milosh

Active member
Joined
May 11, 2011
Messages
33
Programming Experience
1-3
Hi all.
Is here any way to "customize" MessageBox from vb.net to show different text on buttons OK,Cancel,Yes, NO etc.?

MessageBox.Show(" Some text '" & Me.Text & "'?", "Close", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
Instead Yes No buttos show Yes of course and Not now.
I know solution to create new form, but is there solution to change standard MessageBox?

Or if nobody do that, can you post your solutions of new forms?
Thanks
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,379
Location
Norway
Programming Experience
10+
MessageBox class can't be changed. To create your own dialog form start by Add "new form" or "new item" to project and select the Dialog form template. Then you customize that and call ShowDialog method to show it as a dialog.
 

milosh

Active member
Joined
May 11, 2011
Messages
33
Programming Experience
1-3
Thanks JohnH
I willl try. If you have some examples of modifying dialogs as messagebox please post.
 

BrailleSchool

New member
Joined
Mar 20, 2014
Messages
1
Programming Experience
3-5
Actually, you can change it using this class I found online a long time ago. I use it in my projects.
Imports System.Text
Imports System.Runtime.InteropServices

Public Class CustomMessageBox
Private Shared TheLabels() As String
Private Shared TheLabelIndex As Integer

Public Shared Sub CustomMsgBox(ByVal Labels() As String)
TheLabels = Labels
Application.OpenForms(0).BeginInvoke(New FindWindowDelegate(AddressOf FindMsgBox), GetCurrentThreadId())
End Sub

Private Shared Sub FindMsgBox(ByVal tid As Integer)
EnumThreadWindows(tid, AddressOf EnumWindow, IntPtr.Zero)
End Sub

Private Shared Function EnumWindow(ByVal hWnd As IntPtr, ByVal lp As IntPtr) As Boolean
Dim sb As New StringBuilder(256)

GetClassName(hWnd, sb, sb.Capacity)
If sb.ToString() <> "#32770" Then Return True
TheLabelIndex = 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 TheLabelIndex <= UBound(TheLabels) Then
SetWindowText(hWnd, TheLabels(TheLabelIndex))
TheLabelIndex += 1
End If

Return True
End Function

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
You call this like so:
CustomMessageBox.CustomMsgBox(New String() {"Open", "Pending", "Cancel"})
MessageBox.Show("You can mark this ticket as either Open or Pending when saving a ticket." _
& vbNewLine & vbNewLine & "What would you like to do?", _
"User Selection Required", _
MessageBoxButtons.YesNoCancel, _
MessageBoxIcon.Question, _
MessageBoxDefaultButton.Button3)
For "Open" in this example, you would check to see if the end user clicked on "Yes", for "Pending", the button would be "No" and for "Cancel" the button would be "Cancel".
 
Top Bottom