Custom your MessageBox : MessageBox « GUI « VB.Net Tutorial






Custom your MessageBox
Imports System.IO
Imports System.Text.Encoding
Imports System.Drawing
Imports System.Drawing.Printing
Imports System.Windows.Forms

public class CustomMessageBox
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class


Public Class Form1
  Inherits System.Windows.Forms.Form

  Public Sub New()
    MyBase.New()

    InitializeComponent()

  End Sub

  Private components As System.ComponentModel.IContainer

  Friend WithEvents Label1 As System.Windows.Forms.Label
  Friend WithEvents btnShow As System.Windows.Forms.Button
  Friend WithEvents txtMsg As System.Windows.Forms.TextBox
  Friend WithEvents txtCaption As System.Windows.Forms.TextBox
  Friend WithEvents Label2 As System.Windows.Forms.Label
  Friend WithEvents grpDefaultButton As System.Windows.Forms.GroupBox
  Friend WithEvents DefaultButton3 As System.Windows.Forms.RadioButton
  Friend WithEvents DefaultButton2 As System.Windows.Forms.RadioButton
  Friend WithEvents DefaultButton1 As System.Windows.Forms.RadioButton
  Friend WithEvents GroupBox2 As System.Windows.Forms.GroupBox
  Friend WithEvents Button6 As System.Windows.Forms.RadioButton
  Friend WithEvents Button5 As System.Windows.Forms.RadioButton
  Friend WithEvents Button4 As System.Windows.Forms.RadioButton
  Friend WithEvents Button3 As System.Windows.Forms.RadioButton
  Friend WithEvents Button2 As System.Windows.Forms.RadioButton
  Friend WithEvents Button1 As System.Windows.Forms.RadioButton
  Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
  Friend WithEvents txtResult As System.Windows.Forms.TextBox
  Friend WithEvents lblResult As System.Windows.Forms.Label
  Friend WithEvents Icon8 As System.Windows.Forms.RadioButton
  Friend WithEvents Icon7 As System.Windows.Forms.RadioButton
  Friend WithEvents Icon6 As System.Windows.Forms.RadioButton
  Friend WithEvents Icon5 As System.Windows.Forms.RadioButton
  Friend WithEvents Icon4 As System.Windows.Forms.RadioButton
  Friend WithEvents Icon3 As System.Windows.Forms.RadioButton
  Friend WithEvents Icon2 As System.Windows.Forms.RadioButton
  Friend WithEvents Icon1 As System.Windows.Forms.RadioButton
  <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    Me.txtMsg = New System.Windows.Forms.TextBox()
    Me.Label1 = New System.Windows.Forms.Label()
    Me.btnShow = New System.Windows.Forms.Button()
    Me.txtCaption = New System.Windows.Forms.TextBox()
    Me.Label2 = New System.Windows.Forms.Label()
    Me.grpDefaultButton = New System.Windows.Forms.GroupBox()
    Me.DefaultButton3 = New System.Windows.Forms.RadioButton()
    Me.DefaultButton2 = New System.Windows.Forms.RadioButton()
    Me.DefaultButton1 = New System.Windows.Forms.RadioButton()
    Me.GroupBox2 = New System.Windows.Forms.GroupBox()
    Me.Button6 = New System.Windows.Forms.RadioButton()
    Me.Button5 = New System.Windows.Forms.RadioButton()
    Me.Button4 = New System.Windows.Forms.RadioButton()
    Me.Button3 = New System.Windows.Forms.RadioButton()
    Me.Button2 = New System.Windows.Forms.RadioButton()
    Me.Button1 = New System.Windows.Forms.RadioButton()
    Me.GroupBox1 = New System.Windows.Forms.GroupBox()
    Me.Icon8 = New System.Windows.Forms.RadioButton()
    Me.Icon7 = New System.Windows.Forms.RadioButton()
    Me.Icon6 = New System.Windows.Forms.RadioButton()
    Me.Icon5 = New System.Windows.Forms.RadioButton()
    Me.Icon4 = New System.Windows.Forms.RadioButton()
    Me.Icon3 = New System.Windows.Forms.RadioButton()
    Me.Icon2 = New System.Windows.Forms.RadioButton()
    Me.Icon1 = New System.Windows.Forms.RadioButton()
    Me.txtResult = New System.Windows.Forms.TextBox()
    Me.lblResult = New System.Windows.Forms.Label()
    Me.grpDefaultButton.SuspendLayout()
    Me.GroupBox2.SuspendLayout()
    Me.GroupBox1.SuspendLayout()
    Me.SuspendLayout()
    '
    'txtMsg
    '
    Me.txtMsg.Location = New System.Drawing.Point(68, 8)
    Me.txtMsg.Name = "txtMsg"
    Me.txtMsg.Size = New System.Drawing.Size(200, 22)
    Me.txtMsg.TabIndex = 0
    Me.txtMsg.Text = ""
    '
    'Label1
    '
    Me.Label1.Location = New System.Drawing.Point(12, 12)
    Me.Label1.Name = "Label1"
    Me.Label1.Size = New System.Drawing.Size(60, 16)
    Me.Label1.TabIndex = 13
    Me.Label1.Text = "Message:"
    '
    'btnShow
    '
    Me.btnShow.Location = New System.Drawing.Point(280, 8)
    Me.btnShow.Name = "btnShow"
    Me.btnShow.Size = New System.Drawing.Size(76, 24)
    Me.btnShow.TabIndex = 5
    Me.btnShow.Text = "Show"
    '
    'txtCaption
    '
    Me.txtCaption.Location = New System.Drawing.Point(68, 36)
    Me.txtCaption.Name = "txtCaption"
    Me.txtCaption.Size = New System.Drawing.Size(200, 22)
    Me.txtCaption.TabIndex = 1
    Me.txtCaption.Text = ""
    '
    'Label2
    '
    Me.Label2.Location = New System.Drawing.Point(12, 40)
    Me.Label2.Name = "Label2"
    Me.Label2.Size = New System.Drawing.Size(60, 16)
    Me.Label2.TabIndex = 16
    Me.Label2.Text = "Caption:"
    '
    'grpDefaultButton
    '
    Me.grpDefaultButton.Controls.AddRange(New System.Windows.Forms.Control() {Me.DefaultButton3, Me.DefaultButton2, Me.DefaultButton1})
    Me.grpDefaultButton.Location = New System.Drawing.Point(208, 72)
    Me.grpDefaultButton.Name = "grpDefaultButton"
    Me.grpDefaultButton.Size = New System.Drawing.Size(144, 124)
    Me.grpDefaultButton.TabIndex = 4
    Me.grpDefaultButton.TabStop = False
    Me.grpDefaultButton.Text = "Default Button"
    '
    'DefaultButton3
    '
    Me.DefaultButton3.Location = New System.Drawing.Point(12, 68)
    Me.DefaultButton3.Name = "DefaultButton3"
    Me.DefaultButton3.Size = New System.Drawing.Size(100, 20)
    Me.DefaultButton3.TabIndex = 2
    Me.DefaultButton3.Text = "DefaultButton3"
    '
    'DefaultButton2
    '
    Me.DefaultButton2.Location = New System.Drawing.Point(12, 44)
    Me.DefaultButton2.Name = "DefaultButton2"
    Me.DefaultButton2.Size = New System.Drawing.Size(100, 20)
    Me.DefaultButton2.TabIndex = 1
    Me.DefaultButton2.Text = "DefaultButton2"
    '
    'DefaultButton1
    '
    Me.DefaultButton1.Checked = True
    Me.DefaultButton1.Location = New System.Drawing.Point(12, 20)
    Me.DefaultButton1.Name = "DefaultButton1"
    Me.DefaultButton1.Size = New System.Drawing.Size(100, 20)
    Me.DefaultButton1.TabIndex = 0
    Me.DefaultButton1.TabStop = True
    Me.DefaultButton1.Text = "DefaultButton1"
    '
    'GroupBox2
    '
    Me.GroupBox2.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button6, Me.Button5, Me.Button4, Me.Button3, Me.Button2, Me.Button1})
    Me.GroupBox2.Location = New System.Drawing.Point(12, 200)
    Me.GroupBox2.Name = "GroupBox2"
    Me.GroupBox2.Size = New System.Drawing.Size(188, 160)
    Me.GroupBox2.TabIndex = 3
    Me.GroupBox2.TabStop = False
    Me.GroupBox2.Text = "Buttons"
    '
    'Button6
    '
    Me.Button6.Location = New System.Drawing.Point(12, 133)
    Me.Button6.Name = "Button6"
    Me.Button6.Size = New System.Drawing.Size(100, 19)
    Me.Button6.TabIndex = 5
    Me.Button6.Text = "YesNoCancel"
    '
    'Button5
    '
    Me.Button5.Checked = True
    Me.Button5.Location = New System.Drawing.Point(12, 110)
    Me.Button5.Name = "Button5"
    Me.Button5.Size = New System.Drawing.Size(100, 20)
    Me.Button5.TabIndex = 4
    Me.Button5.TabStop = True
    Me.Button5.Text = "YesNo"
    '
    'Button4
    '
    Me.Button4.Location = New System.Drawing.Point(12, 86)
    Me.Button4.Name = "Button4"
    Me.Button4.Size = New System.Drawing.Size(100, 21)
    Me.Button4.TabIndex = 3
    Me.Button4.Text = "RetryCancel"
    '
    'Button3
    '
    Me.Button3.Location = New System.Drawing.Point(12, 65)
    Me.Button3.Name = "Button3"
    Me.Button3.Size = New System.Drawing.Size(100, 18)
    Me.Button3.TabIndex = 2
    Me.Button3.Text = "OKCancel"
    '
    'Button2
    '
    Me.Button2.Location = New System.Drawing.Point(12, 43)
    Me.Button2.Name = "Button2"
    Me.Button2.Size = New System.Drawing.Size(100, 19)
    Me.Button2.TabIndex = 1
    Me.Button2.Text = "OK"
    '
    'Button1
    '
    Me.Button1.Location = New System.Drawing.Point(12, 20)
    Me.Button1.Name = "Button1"
    Me.Button1.Size = New System.Drawing.Size(112, 20)
    Me.Button1.TabIndex = 0
    Me.Button1.Text = "AbortRetryIgnore"
    '
    'GroupBox1
    '
    Me.GroupBox1.Controls.AddRange(New System.Windows.Forms.Control() {Me.Icon8, Me.Icon7, Me.Icon6, Me.Icon5, Me.Icon4, Me.Icon3, Me.Icon2, Me.Icon1})
    Me.GroupBox1.Location = New System.Drawing.Point(12, 72)
    Me.GroupBox1.Name = "GroupBox1"
    Me.GroupBox1.Size = New System.Drawing.Size(188, 124)
    Me.GroupBox1.TabIndex = 2
    Me.GroupBox1.TabStop = False
    Me.GroupBox1.Text = "Icon"
    '
    'Icon8
    '
    Me.Icon8.Location = New System.Drawing.Point(100, 92)
    Me.Icon8.Name = "Icon8"
    Me.Icon8.Size = New System.Drawing.Size(64, 20)
    Me.Icon8.TabIndex = 7
    Me.Icon8.Text = "Warning"
    '
    'Icon7
    '
    Me.Icon7.Location = New System.Drawing.Point(12, 92)
    Me.Icon7.Name = "Icon7"
    Me.Icon7.Size = New System.Drawing.Size(84, 20)
    Me.Icon7.TabIndex = 6
    Me.Icon7.Text = "Stop"
    '
    'Icon6
    '
    Me.Icon6.Location = New System.Drawing.Point(100, 68)
    Me.Icon6.Name = "Icon6"
    Me.Icon6.Size = New System.Drawing.Size(64, 20)
    Me.Icon6.TabIndex = 5
    Me.Icon6.Text = "Question"
    '
    'Icon5
    '
    Me.Icon5.Checked = True
    Me.Icon5.Location = New System.Drawing.Point(12, 68)
    Me.Icon5.Name = "Icon5"
    Me.Icon5.Size = New System.Drawing.Size(84, 20)
    Me.Icon5.TabIndex = 4
    Me.Icon5.TabStop = True
    Me.Icon5.Text = "Information"
    '
    'Icon4
    '
    Me.Icon4.Location = New System.Drawing.Point(100, 44)
    Me.Icon4.Name = "Icon4"
    Me.Icon4.Size = New System.Drawing.Size(64, 20)
    Me.Icon4.TabIndex = 3
    Me.Icon4.Text = "Hand"
    '
    'Icon3
    '
    Me.Icon3.Location = New System.Drawing.Point(12, 44)
    Me.Icon3.Name = "Icon3"
    Me.Icon3.Size = New System.Drawing.Size(84, 20)
    Me.Icon3.TabIndex = 2
    Me.Icon3.Text = "Exclamation"
    '
    'Icon2
    '
    Me.Icon2.Location = New System.Drawing.Point(100, 20)
    Me.Icon2.Name = "Icon2"
    Me.Icon2.Size = New System.Drawing.Size(64, 20)
    Me.Icon2.TabIndex = 1
    Me.Icon2.Text = "Error"
    '
    'Icon1
    '
    Me.Icon1.Location = New System.Drawing.Point(12, 20)
    Me.Icon1.Name = "Icon1"
    Me.Icon1.Size = New System.Drawing.Size(84, 20)
    Me.Icon1.TabIndex = 0
    Me.Icon1.Text = "Asterisk"
    '
    'txtResult
    '
    Me.txtResult.Enabled = False
    Me.txtResult.Location = New System.Drawing.Point(212, 232)
    Me.txtResult.Name = "txtResult"
    Me.txtResult.Size = New System.Drawing.Size(136, 22)
    Me.txtResult.TabIndex = 18
    Me.txtResult.Text = ""
    '
    'lblResult
    '
    Me.lblResult.Location = New System.Drawing.Point(212, 208)
    Me.lblResult.Name = "lblResult"
    Me.lblResult.Size = New System.Drawing.Size(56, 20)
    Me.lblResult.TabIndex = 17
    Me.lblResult.Text = "Result"
    '
    'Form1
    '
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 15)
    Me.ClientSize = New System.Drawing.Size(368, 369)
    Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.grpDefaultButton, Me.GroupBox2, Me.GroupBox1, Me.txtResult, Me.lblResult, Me.txtCaption, Me.Label2, Me.btnShow, Me.txtMsg, Me.Label1})
    Me.Name = "Form1"
    Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
    Me.Text = "MessageBox Application"
    Me.grpDefaultButton.ResumeLayout(False)
    Me.GroupBox2.ResumeLayout(False)
    Me.GroupBox1.ResumeLayout(False)
    Me.ResumeLayout(False)

  End Sub


  Private iIcon As Integer
  Private iButton As Integer
  Private iDefaultButton As Integer

  Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow.Click
    Dim iResult As Integer

    iResult = MessageBox.Show(Me,txtMsg.Text, txtCaption.Text, iButton, iIcon, iDefaultButton)

    Select Case iResult
      Case DialogResult.Abort
        txtResult.Text = "DialogResult.Abort"
      Case DialogResult.Retry
        txtResult.Text = "DialogResult.Retry"
      Case DialogResult.Ignore
        txtResult.Text = "DialogResult.Ignore"
      Case DialogResult.OK
        txtResult.Text = "DialogResult.OK"
      Case DialogResult.Cancel
        txtResult.Text = "DialogResult.Cancel"
      Case DialogResult.Yes
        txtResult.Text = "DialogResult.Yes"
      Case DialogResult.No
        txtResult.Text = "DialogResult.No"
    End Select
  End Sub

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    AddHandler Icon1.Click, AddressOf Icon_Click
    AddHandler Icon2.Click, AddressOf Icon_Click
    AddHandler Icon3.Click, AddressOf Icon_Click
    AddHandler Icon4.Click, AddressOf Icon_Click
    AddHandler Icon5.Click, AddressOf Icon_Click
    AddHandler Icon6.Click, AddressOf Icon_Click
    AddHandler Icon7.Click, AddressOf Icon_Click
    AddHandler Icon8.Click, AddressOf Icon_Click

    AddHandler Button1.Click, AddressOf Button_Click
    AddHandler Button2.Click, AddressOf Button_Click
    AddHandler Button3.Click, AddressOf Button_Click
    AddHandler Button4.Click, AddressOf Button_Click
    AddHandler Button5.Click, AddressOf Button_Click
    AddHandler Button6.Click, AddressOf Button_Click

    AddHandler DefaultButton1.Click, AddressOf DefaultButton_Click
    AddHandler DefaultButton2.Click, AddressOf DefaultButton_Click
    AddHandler DefaultButton3.Click, AddressOf DefaultButton_Click

    iIcon = MessageBoxIcon.Information
    iButton = MessageBoxButtons.YesNo
    iDefaultButton = MessageBoxDefaultButton.Button1
  End Sub

  Public Sub Icon_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Select Case sender.Name
      Case "Icon1"
        iIcon = MessageBoxIcon.Asterisk
      Case "Icon2"
        iIcon = MessageBoxIcon.Error
      Case "Icon3"
        iIcon = MessageBoxIcon.Exclamation
      Case "Icon4"
        iIcon = MessageBoxIcon.Hand
      Case "Icon5"
        iIcon = MessageBoxIcon.Information
      Case "Icon6"
        iIcon = MessageBoxIcon.Question
      Case "Icon7"
        iIcon = MessageBoxIcon.Stop
      Case "Icon8"
        iIcon = MessageBoxIcon.Warning
    End Select
  End Sub

  Public Sub Button_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Select Case sender.Name
      Case "Button1"
        iButton = MessageBoxButtons.AbortRetryIgnore
      Case "Button2"
        iButton = MessageBoxButtons.OK
      Case "Button3"
        iButton = MessageBoxButtons.OKCancel
      Case "Button4"
        iButton = MessageBoxButtons.RetryCancel
      Case "Button5"
        iButton = MessageBoxButtons.YesNo
      Case "Button6"
        iButton = MessageBoxButtons.YesNoCancel
    End Select
  End Sub

  Public Sub DefaultButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Select Case sender.Name
      Case "DefaultButton1"
        iDefaultButton = MessageBoxDefaultButton.Button1
      Case "DefaultButton2"
        iDefaultButton = MessageBoxDefaultButton.Button2
      Case "DefaultButton3"
        iDefaultButton = MessageBoxDefaultButton.Button3
    End Select
  End Sub

End Class








14.60.MessageBox
14.60.1.MessageBox with Message onlyMessageBox with Message only
14.60.2.MessageBox with Message and TitleMessageBox with Message and Title
14.60.3.MessageBox with OK, Cancel button and InformationMessageBox with OK, Cancel button and Information
14.60.4.MessageBox with Yes No Cancel button and Question iconMessageBox with Yes No Cancel button and Question icon
14.60.5.MessageBox OK and Cancel ButtonMessageBox OK and Cancel Button
14.60.6.MessageBox: Abort, Retry, Ignore buttons and Warning iconMessageBox: Abort, Retry, Ignore buttons and Warning icon
14.60.7.Set default button for MessageBoxSet default button for MessageBox
14.60.8.Displaying square root of 2 in dialog.
14.60.9.Return value from MessageBox
14.60.10.MessageBox Icon and ButtonMessageBox Icon and Button
14.60.11.Custom your MessageBoxCustom your MessageBox
14.60.12.Create MessageBox dynamicallyCreate MessageBox dynamically