Monorepo/MessageBoxEx/MsgBoxEx.vb

190 lines
6.4 KiB
VB.net

Public Class MsgBoxEx
Public Sub New(message As String, title As String)
Me.New(message, title, MessageBoxIcon.None)
End Sub
Public Sub New(message As String, title As String, icon As MessageBoxIcon)
Me.New(message, title, getMessageBoxIcon(icon))
End Sub
Public Sub New(message As String, title As String, icon As Icon)
InitializeComponent()
messageLbl.Text = message
Text = title
m_sysIcon = icon
If m_sysIcon Is Nothing Then messageLbl.Location = New System.Drawing.Point(FORM_X_MARGIN, FORM_Y_MARGIN)
SetButtons("OK")
End Sub
Private Shared Function getMessageBoxIcon(icon As MessageBoxIcon) As Icon
Dim oIcon As Icon = Nothing
Select Case icon
Case MessageBoxIcon.Asterisk
oIcon = SystemIcons.Asterisk
Case MessageBoxIcon.[Error]
oIcon = SystemIcons.[Error]
Case MessageBoxIcon.Exclamation
oIcon = SystemIcons.Exclamation
Case MessageBoxIcon.Question
oIcon = SystemIcons.Question
End Select
If oIcon IsNot Nothing Then
Return DevExpress.Utils.Drawing.Helpers.StockIconHelper.GetWindows8AssociatedIcon(oIcon)
Else
Return Nothing
End If
End Function
Private m_minWidth As Integer
Private m_minHeight As Integer
Public Sub SetMinSize(width As Integer, height As Integer)
m_minWidth = width
m_minHeight = height
End Sub
Public Sub SetButtons(ParamArray names As String())
Dim drs As DialogResult() = New DialogResult(names.Length - 1) {}
For i As Integer = 0 To names.Length - 1
drs(i) = DialogResult.None
Next
SetButtons(names, drs)
End Sub
Public Sub SetButtons(names As String(), results As DialogResult())
SetButtons(names, results, 1)
End Sub
Public Sub SetButtons(names As String(), results As DialogResult(), def As Integer)
If names Is Nothing Then Throw New ArgumentNullException("btnText", "Button Text is null")
Dim count As Integer = names.Length
If count < 1 OrElse count > 3 Then Throw New ArgumentException("Invalid number of buttons. Must be between 1 and 3.")
m_minButtonRowWidth = setButtonParams(btn1, names(0), If(def = 1, 1, 2), results(0))
If count > 1 Then
m_minButtonRowWidth += setButtonParams(btn2, names(1), If(def = 2, 1, 3), results(1)) + BUTTON_SPACE
End If
If count > 2 Then
m_minButtonRowWidth += setButtonParams(btn3, names(2), If(def = 3, 1, 4), results(2)) + BUTTON_SPACE
End If
End Sub
Private m_minButtonRowWidth As Integer
Private Shared Function setButtonParams(btn As Button, text As String, tab As Integer, dr As DialogResult) As Integer
btn.Text = text
btn.Visible = True
btn.DialogResult = dr
btn.TabIndex = tab
Return btn.Size.Width
End Function
Public Sub SetCheckbox(text As String)
SetCheckbox(text, False)
End Sub
Public Sub SetCheckbox(text As String, chcked As Boolean)
chkBx.Visible = True
chkBx.Text = text
chkBx.Checked = chcked
m_minButtonRowWidth += chkBx.Size.Width + CHECKBOX_SPACE
End Sub
Private Sub DialogBox_Load(sender As Object, e As EventArgs)
If Not btn1.Visible Then SetButtons(New String() {"OK"}, New DialogResult() {DialogResult.OK})
m_minButtonRowWidth += 2 * FORM_X_MARGIN
setDialogSize()
setButtonRowLocations()
End Sub
Const FORM_Y_MARGIN As Integer = 10
Const FORM_X_MARGIN As Integer = 16
Const BUTTON_SPACE As Integer = 5
Const CHECKBOX_SPACE As Integer = 15
Const TEXT_Y_MARGIN As Integer = 30
Private Sub setDialogSize()
Dim requiredWidth As Integer = messageLbl.Location.X + messageLbl.Size.Width + FORM_X_MARGIN
requiredWidth = If(requiredWidth > m_minButtonRowWidth, requiredWidth, m_minButtonRowWidth)
Dim requiredHeight As Integer = messageLbl.Location.Y + messageLbl.Size.Height - btn2.Location.Y + ClientSize.Height + TEXT_Y_MARGIN
Dim minSetWidth As Integer = If(ClientSize.Width > m_minWidth, ClientSize.Width, m_minWidth)
Dim minSetHeight As Integer = If(ClientSize.Height > m_minHeight, ClientSize.Height, m_minHeight)
Dim s As Size = New Size()
s.Width = If(requiredWidth > minSetWidth, requiredWidth, minSetWidth)
s.Height = If(requiredHeight > minSetHeight, requiredHeight, minSetHeight)
ClientSize = s
End Sub
Private Sub setButtonRowLocations()
Dim formWidth As Integer = ClientRectangle.Width
Dim x As Integer = formWidth - FORM_X_MARGIN
Dim y As Integer = btn1.Location.Y
If btn3.Visible Then
x -= btn3.Size.Width
btn3.Location = New Point(x, y)
x -= BUTTON_SPACE
End If
If btn2.Visible Then
x -= btn2.Size.Width
btn2.Location = New Point(x, y)
x -= BUTTON_SPACE
End If
x -= btn1.Size.Width
btn1.Location = New Point(x, y)
If chkBx.Visible Then chkBx.Location = New Point(FORM_X_MARGIN, chkBx.Location.Y)
End Sub
Private m_sysIcon As Icon
Protected Overrides Sub OnPaint(e As PaintEventArgs)
If m_sysIcon IsNot Nothing Then
Dim g As Graphics = e.Graphics
g.DrawIconUnstretched(m_sysIcon, New Rectangle(FORM_X_MARGIN, FORM_Y_MARGIN, m_sysIcon.Width, m_sysIcon.Height))
End If
MyBase.OnPaint(e)
End Sub
Public ReadOnly Property CheckboxChecked As Boolean
Get
Return chkBx.Checked
End Get
End Property
Private m_result As DialogBoxResult
Public ReadOnly Property Result As DialogBoxResult
Get
Return m_result
End Get
End Property
Private Sub btn_Click(sender As Object, e As EventArgs) Handles btn1.Click, btn2.Click, btn3.Click
If sender.Equals(btn1) Then
m_result = DialogBoxResult.Button1
ElseIf sender.Equals(btn2) Then
m_result = DialogBoxResult.Button2
ElseIf sender.Equals(btn3) Then
m_result = DialogBoxResult.Button3
End If
If (CType(sender, Button)).DialogResult = DialogResult.None Then
Close()
End If
End Sub
Enum DialogBoxResult
Button1
Button2
Button3
End Enum
End Class