Localize Messagebox in Windows Form app in .NET

By default, Windows messagebox buttons can't be localized directly. So I made new form for messagebox and localized it. you can also modify or extend as per your requirement.

1. Create a New Form (say CMessage),
2. Drag Table Layout Panel and Put PictureBox (Pic),Label (Label1) and
buttons(btnOK,btnCancel)
3. Add following names and values in each resource file(e.g. resource.resx, resource.es-MX.cs… in My Project folder)
lblOK, lblCancel, MsgYes, MsgNo
4. Use Following Code


Public Class CMessageBox
Public Overloads Shared Function show( _
ByVal text As String, _
Optional ByVal caption As String = "", _
Optional ByVal buttons As System.Windows.Forms.MessageBoxButtons = MessageBoxButtons.OK, _
Optional ByVal icon As System.Windows.Forms.MessageBoxIcon = MessageBoxIcon.None, _
Optional ByVal defaultButton As System.Windows.Forms.MessageBoxDefaultButton = MessageBoxDefaultButton.Button1, _
Optional ByVal options As System.Windows.Forms.MessageBoxOptions = MessageBoxOptions.DefaultDesktopOnly) As DialogResult
Dim msgForm As New CMessageBox
msgForm.lblMsg.Text = text
msgForm.Text = caption
If buttons = MessageBoxButtons.OK Then
msgForm.SetBtn(msgForm.btnOK, My.Resources.lblOK, True, Windows.Forms.DialogResult.OK)
msgForm.SetBtn(msgForm.btnCancel, My.Resources.lblCancel, False, Windows.Forms.DialogResult.Cancel)
ElseIf buttons = MessageBoxButtons.OKCancel Then
msgForm.SetBtn(msgForm.btnOK, My.Resources.lblOK, True, Windows.Forms.DialogResult.OK)
msgForm.SetBtn(msgForm.btnCancel, My.Resources.lblCancel, True, Windows.Forms.DialogResult.Cancel)
ElseIf buttons = MessageBoxButtons.RetryCancel Then
msgForm.SetBtn(msgForm.btnOK, "Retry", True, Windows.Forms.DialogResult.Retry)
msgForm.SetBtn(msgForm.btnCancel, My.Resources.lblCancel, True, Windows.Forms.DialogResult.Cancel)
ElseIf buttons = MessageBoxButtons.YesNo Then
msgForm.SetBtn(msgForm.btnOK, My.Resources.MsgYes, True, Windows.Forms.DialogResult.Yes)
msgForm.SetBtn(msgForm.btnCancel, My.Resources.MsgNo, True, Windows.Forms.DialogResult.No)
End If
If Not msgForm.GetIcon(icon) Is Nothing Then
msgForm.pic.Visible = True
msgForm.pic.Image = msgForm.GetIcon(icon).ToBitmap
msgForm.pic.Size = msgForm.pic.Image.Size
Else
msgForm.pic.Visible = False
End If
Return msgForm.ShowDialog
End Function
Public Function GetIcon(ByVal msgIcon As MessageBoxIcon) As Icon
Select Case (msgIcon)
Case MessageBoxIcon.Error
Return System.Drawing.SystemIcons.Error
Case MessageBoxIcon.Exclamation
Return System.Drawing.SystemIcons.Exclamation
Case MessageBoxIcon.Hand
Return System.Drawing.SystemIcons.Hand
Case MessageBoxIcon.Information
Return System.Drawing.SystemIcons.Information
Case MessageBoxIcon.None
'Return System.Drawing.SystemIcons.Information
Return Nothing
Case MessageBoxIcon.Question
Return System.Drawing.SystemIcons.Question
Case MessageBoxIcon.Stop
Return Nothing
Case MessageBoxIcon.Warning
Return System.Drawing.SystemIcons.Warning
End Select
Return Nothing
End Function
Public Overloads Shared Function show( _
ByVal owner As System.Windows.Forms.IWin32Window, _
Optional ByVal text As String = "", _
Optional ByVal caption As String = "", _
Optional ByVal buttons As System.Windows.Forms.MessageBoxButtons = MessageBoxButtons.OK, _
Optional ByVal icon As System.Windows.Forms.MessageBoxIcon = MessageBoxIcon.None, _
Optional ByVal defaultButton As System.Windows.Forms.MessageBoxDefaultButton = MessageBoxDefaultButton.Button1) As DialogResult


End Function

Public Sub SetBtn(ByRef btn As Button, ByVal text As String, ByVal visible As Boolean, ByVal dr As DialogResult)
btn.Text = text
btn.Visible = visible
btn.DialogResult = dr
End Sub

Private Sub CMessageBox_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.MinimizeBox = False
Me.MaximizeBox = False
End Sub
End Class

5. Now you can call messagebox:
CMessageBox.Show([Message From Resource File], [Title From Resource File])

No comments:

Post a Comment