寻找一个能够满足我的需求并且类似于MessageBox .Net类的自定义MessageBox,仍然可以通过Visual Studio表单设计器进行设计,我想出了这个(下面的代码示例解释):
Public Class CustomMessageBox
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Private Class CustomMessageForm
Inherits System.Windows.Forms.Form
'Form shadows dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Public Shadows Sub Dispose()
Try
If components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(True)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(197, 226)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(75, 23)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
Me.Button1.UseVisualStyleBackColor = True
'
'CustomMessageBox
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(284, 261)
Me.Controls.Add(Me.Button1)
Me.Name = "CustomMessageBox"
Me.Text = "CustomMessageBox"
Me.ResumeLayout(False)
End Sub
Friend WithEvents Button1 As System.Windows.Forms.Button
End Class
Public Shared Shadows Function Show(ByVal Title As String) As DialogResult
Dim dlg As New CustomMessageForm()
dlg.Text = Title
Dim res = dlg.ShowDialog
dlg.Dispose()
Return res
End Function
End Class
内部私有类是由设计者直接填充的类,并声明为Partial
,我将其修改为Private
,因为我的目的也是让一个类(几乎)只有显示从外部可以访问的共享方法,就像使用MessageBox .Net类一样。
我还修改了Dispose方法以隐藏BaseClass,并始终让它释放托管资源。
运行时一切正常,但是当通过设计器修改表单时,正如预期的那样,我遇到了这个错误:
The designer could not be shown for this file because none of the classes within it can be designed. The designer inspected the following classes in the file: CustomMessageBox --- The base class Object could not be loaded. Ensure the assembly has been referenced and that all projects have been built.
正如我所说,这是预期的,因为设计师我认为没有达到内部私人课程,或者出于其他原因我目前没有关注。
是否有针对此和/或其他方式的解决方法以达到相同的简单代码达到目的?
谢谢。
P.S。
我希望尽可能将代码从单独的ClassLib中删除,并将其作为Windows窗体应用程序的同一程序集中的一个.vb文件。