Guest User

Untitled

a guest
Jun 22nd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.68 KB | None | 0 0
  1. ' A custom MessageBox class
  2. ' Written to allow custom button text, specifically to allow for bilingual messageboxes
  3. '
  4. Public Class CustomMessageBox
  5.  
  6. 'Variables
  7. Private _btn1Return As DialogResult
  8. Private _btn2Return As DialogResult
  9. Private _btn3Return As DialogResult
  10.  
  11. 'Enumerate system icons
  12. Enum SystemIcons
  13. ErrorIcon = 1
  14. WarningIcon = 2
  15. QuestionIcon = 3
  16. InformationIcon = 4
  17. End Enum
  18.  
  19. 'Enumerate possible button combinations
  20. Enum ButtonTypes
  21. YesNo = 1
  22. YesNoCancel = 2
  23. Ok = 3
  24. OkCancel = 4
  25. End Enum
  26.  
  27. 'Enumerate possible default buttons
  28. Enum DefaultButton
  29. Button1 = 1
  30. Button2 = 2
  31. Button3 = 3
  32. End Enum
  33.  
  34.  
  35. #Region "Constructor"
  36. 'Constructor
  37. Public Sub New(ByVal text As String, ByVal caption As String, ByVal buttons As ButtonTypes, ByVal icon As SystemIcons, ByVal defaultButton As DefaultButton)
  38. InitializeComponent()
  39. Me.Text = caption
  40. Me.msgBoxText.Text = text
  41. SetupIcon(icon)
  42. SetupButtons(buttons, defaultButton)
  43. End Sub
  44. #End Region
  45.  
  46. #Region "Functions"
  47. 'Set text and return values of all buttons. Also set focus to default button.
  48. Private Sub SetupButtons(ByVal buttons As Integer, ByVal defaultButton As Integer)
  49. Select Case buttons
  50. Case 1
  51. msgBoxButton1.Text = My.Resources.Caption_Yes
  52. _btn1Return = Windows.Forms.DialogResult.Yes
  53. msgBoxButton2.Text = My.Resources.Caption_No
  54. _btn2Return = Windows.Forms.DialogResult.No
  55. msgBoxButton3.Visible = False
  56. Case 2
  57. msgBoxButton1.Text = My.Resources.Caption_Yes
  58. _btn1Return = Windows.Forms.DialogResult.Yes
  59. msgBoxButton2.Text = My.Resources.Caption_No
  60. _btn2Return = Windows.Forms.DialogResult.No
  61. msgBoxButton3.Text = My.Resources.Caption_Cancel
  62. _btn3Return = Windows.Forms.DialogResult.Cancel
  63. Case 3
  64. msgBoxButton1.Text = My.Resources.Caption_OK
  65. _btn1Return = Windows.Forms.DialogResult.OK
  66. msgBoxButton2.Visible = False
  67. msgBoxButton3.Visible = False
  68. Case 4
  69. msgBoxButton1.Text = My.Resources.Caption_OK
  70. _btn1Return = Windows.Forms.DialogResult.OK
  71. msgBoxButton2.Text = My.Resources.Caption_Cancel
  72. _btn2Return = Windows.Forms.DialogResult.Cancel
  73. msgBoxButton3.Visible = False
  74. End Select
  75.  
  76. 'Set focus to specified default button
  77. Select Case defaultButton
  78. Case 1
  79. msgBoxButton1.Focus()
  80. Case 2
  81. msgBoxButton2.Focus()
  82. Case 3
  83. msgBoxButton3.Focus()
  84. End Select
  85.  
  86. End Sub
  87.  
  88. 'Display specified icon
  89. Private Sub SetupIcon(ByVal iconValue As Integer)
  90. Dim icon As Icon = Nothing
  91. Select Case iconValue
  92. Case 1
  93. icon = System.Drawing.SystemIcons.Error
  94. Case 2
  95. icon = System.Drawing.SystemIcons.Warning
  96. Case 3
  97. icon = System.Drawing.SystemIcons.Question
  98. Case 4
  99. icon = System.Drawing.SystemIcons.Information
  100. End Select
  101.  
  102. Me.msgBoxIcon.Image = icon.ToBitmap
  103. End Sub
  104. #End Region
  105.  
  106. #Region "Events"
  107. 'Set return value when button is clicked.
  108. Private Sub msgBoxButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles msgBoxButton1.Click
  109. Me.DialogResult = _btn1Return
  110. Me.Close()
  111. End Sub
  112.  
  113. 'Set return value when button is clicked.
  114. Private Sub msgBoxButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles msgBoxButton2.Click
  115. Me.DialogResult = _btn2Return
  116. Me.Close()
  117. End Sub
  118.  
  119. 'Set return value when button is clicked.
  120. Private Sub msgBoxButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles msgBoxButton3.Click
  121. Me.DialogResult = _btn3Return
  122. Me.Close()
  123. End Sub
  124. #End Region
  125.  
  126. End Class
  127.  
  128. <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
  129. Partial Class CustomMessageBox
  130. Inherits System.Windows.Forms.Form
  131.  
  132. 'Form overrides dispose to clean up the component list.
  133. <System.Diagnostics.DebuggerNonUserCode()> _
  134. Protected Overrides Sub Dispose(ByVal disposing As Boolean)
  135. Try
  136. If disposing AndAlso components IsNot Nothing Then
  137. components.Dispose()
  138. End If
  139. Finally
  140. MyBase.Dispose(disposing)
  141. End Try
  142. End Sub
  143.  
  144. 'Required by the Windows Form Designer
  145. Private components As System.ComponentModel.IContainer
  146.  
  147. 'NOTE: The following procedure is required by the Windows Form Designer
  148. 'It can be modified using the Windows Form Designer.
  149. 'Do not modify it using the code editor.
  150. <System.Diagnostics.DebuggerStepThrough()> _
  151. Private Sub InitializeComponent()
  152. Me.msgBoxText = New System.Windows.Forms.Label
  153. Me.msgBoxIcon = New System.Windows.Forms.PictureBox
  154. Me.TableLayoutPanel1 = New System.Windows.Forms.TableLayoutPanel
  155. Me.msgBoxButton1 = New System.Windows.Forms.Button
  156. Me.msgBoxButton3 = New System.Windows.Forms.Button
  157. Me.msgBoxButton2 = New System.Windows.Forms.Button
  158. CType(Me.msgBoxIcon, System.ComponentModel.ISupportInitialize).BeginInit()
  159. Me.TableLayoutPanel1.SuspendLayout()
  160. Me.SuspendLayout()
  161. '
  162. 'msgBoxText
  163. '
  164. Me.msgBoxText.AutoSize = True
  165. Me.msgBoxText.Cursor = System.Windows.Forms.Cursors.Cross
  166. Me.msgBoxText.Dock = System.Windows.Forms.DockStyle.Fill
  167. Me.msgBoxText.FlatStyle = System.Windows.Forms.FlatStyle.Flat
  168. Me.msgBoxText.Location = New System.Drawing.Point(59, 0)
  169. Me.msgBoxText.MaximumSize = New System.Drawing.Size(245, 0)
  170. Me.msgBoxText.Name = "msgBoxText"
  171. Me.msgBoxText.Padding = New System.Windows.Forms.Padding(0, 10, 0, 10)
  172. Me.msgBoxText.Size = New System.Drawing.Size(39, 33)
  173. Me.msgBoxText.TabIndex = 6
  174. Me.msgBoxText.Text = "Label1"
  175. '
  176. 'msgBoxIcon
  177. '
  178. Me.msgBoxIcon.Dock = System.Windows.Forms.DockStyle.Left
  179. Me.msgBoxIcon.Location = New System.Drawing.Point(0, 0)
  180. Me.msgBoxIcon.Name = "msgBoxIcon"
  181. Me.msgBoxIcon.Padding = New System.Windows.Forms.Padding(15, 10, 0, 0)
  182. Me.msgBoxIcon.Size = New System.Drawing.Size(59, 53)
  183. Me.msgBoxIcon.TabIndex = 4
  184. Me.msgBoxIcon.TabStop = False
  185. '
  186. 'TableLayoutPanel1
  187. '
  188. Me.TableLayoutPanel1.AutoSize = True
  189. Me.TableLayoutPanel1.ColumnCount = 5
  190. Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50.0!))
  191. Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle)
  192. Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle)
  193. Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle)
  194. Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50.0!))
  195. Me.TableLayoutPanel1.Controls.Add(Me.msgBoxButton1, 1, 0)
  196. Me.TableLayoutPanel1.Controls.Add(Me.msgBoxButton3, 3, 0)
  197. Me.TableLayoutPanel1.Controls.Add(Me.msgBoxButton2, 2, 0)
  198. Me.TableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Bottom
  199. Me.TableLayoutPanel1.GrowStyle = System.Windows.Forms.TableLayoutPanelGrowStyle.FixedSize
  200. Me.TableLayoutPanel1.Location = New System.Drawing.Point(0, 53)
  201. Me.TableLayoutPanel1.Name = "TableLayoutPanel1"
  202. Me.TableLayoutPanel1.Padding = New System.Windows.Forms.Padding(0, 0, 0, 10)
  203. Me.TableLayoutPanel1.RowCount = 1
  204. Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle)
  205. Me.TableLayoutPanel1.Size = New System.Drawing.Size(305, 39)
  206. Me.TableLayoutPanel1.TabIndex = 5
  207. '
  208. 'msgBoxButton1
  209. '
  210. Me.msgBoxButton1.AutoSize = True
  211. Me.msgBoxButton1.Location = New System.Drawing.Point(34, 3)
  212. Me.msgBoxButton1.Name = "msgBoxButton1"
  213. Me.msgBoxButton1.Size = New System.Drawing.Size(75, 23)
  214. Me.msgBoxButton1.TabIndex = 0
  215. Me.msgBoxButton1.Text = "Button1"
  216. Me.msgBoxButton1.UseVisualStyleBackColor = True
  217. '
  218. 'msgBoxButton3
  219. '
  220. Me.msgBoxButton3.AutoSize = True
  221. Me.msgBoxButton3.Location = New System.Drawing.Point(196, 3)
  222. Me.msgBoxButton3.Name = "msgBoxButton3"
  223. Me.msgBoxButton3.Size = New System.Drawing.Size(75, 23)
  224. Me.msgBoxButton3.TabIndex = 2
  225. Me.msgBoxButton3.Text = "Button3"
  226. Me.msgBoxButton3.UseVisualStyleBackColor = True
  227. '
  228. 'msgBoxButton2
  229. '
  230. Me.msgBoxButton2.AutoSize = True
  231. Me.msgBoxButton2.Location = New System.Drawing.Point(115, 3)
  232. Me.msgBoxButton2.Name = "msgBoxButton2"
  233. Me.msgBoxButton2.Size = New System.Drawing.Size(75, 23)
  234. Me.msgBoxButton2.TabIndex = 1
  235. Me.msgBoxButton2.Text = "Button2"
  236. Me.msgBoxButton2.UseVisualStyleBackColor = True
  237. '
  238. 'CustomMessageBox
  239. '
  240. Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
  241. Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
  242. Me.AutoSize = True
  243. Me.ClientSize = New System.Drawing.Size(305, 92)
  244. Me.Controls.Add(Me.msgBoxText)
  245. Me.Controls.Add(Me.msgBoxIcon)
  246. Me.Controls.Add(Me.TableLayoutPanel1)
  247. Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
  248. Me.MaximizeBox = False
  249. Me.MinimizeBox = False
  250. Me.Name = "CustomMessageBox"
  251. Me.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide
  252. Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
  253. Me.Text = "MessageBox"
  254. Me.TopMost = True
  255. CType(Me.msgBoxIcon, System.ComponentModel.ISupportInitialize).EndInit()
  256. Me.TableLayoutPanel1.ResumeLayout(False)
  257. Me.TableLayoutPanel1.PerformLayout()
  258. Me.ResumeLayout(False)
  259. Me.PerformLayout()
  260.  
  261. End Sub
  262. Friend WithEvents msgBoxText As System.Windows.Forms.Label
  263. Friend WithEvents msgBoxIcon As System.Windows.Forms.PictureBox
  264. Friend WithEvents TableLayoutPanel1 As System.Windows.Forms.TableLayoutPanel
  265. Friend WithEvents msgBoxButton1 As System.Windows.Forms.Button
  266. Friend WithEvents msgBoxButton3 As System.Windows.Forms.Button
  267. Friend WithEvents msgBoxButton2 As System.Windows.Forms.Button
  268. End Class
Add Comment
Please, Sign In to add comment