如果您为表单的构造函数(新)或在表单的 Load 事件中调用 MethodName,您的示例代码将不起作用。这可能就是为什么它不适合你。
构造函数是 Sub New。您必须小心构造函数或表单的 Load 事件中的一些初始化。原因是尚未创建包含表单的控件句柄。如果您测试项目有效,则将测试项目与您拥有的项目进行比较。考虑如何调用这些方法以及在哪里调用。您的应用程序无法运行的最可能原因是由于未创建表单而未添加处理程序。(它是在表单变得可见时创建的。您可以尝试在添加处理程序之前添加一个 form.CreateControl。)
此外,尝试通过设计器将处理程序添加到表单中。这将保证处理程序被正确分配。(MSDN 示例手动完成所有操作,并不是一个好的示例。VB 示例应该向您展示如何以简单的 VB 方式完成此操作,而不是更高级的手动方式。)
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Designer-Generated "
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
Friend WithEvents Button2 As System.Windows.Forms.Button
'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.Button2 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(0, 0)
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
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(0, 29)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(75, 23)
Me.Button2.TabIndex = 1
Me.Button2.Text = "Button2"
Me.Button2.UseVisualStyleBackColor = True
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
Friend WithEvents Button1 As System.Windows.Forms.Button
#End Region
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
MethodName() 'will not work here
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MethodName() 'will not work here
'Me.CreateControl()
MethodName2() 'still will not work
End Sub
Private Function MethodName() As Boolean
AddHandler Me.HelpRequested, AddressOf Me.MsgBoxHelpRequested
Select Case MessageBox.Show("Text", "Title", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, 0, True)
Case Windows.Forms.DialogResult.Yes
' Do stuff
Case Windows.Forms.DialogResult.No
' Do stuff
Case Windows.Forms.DialogResult.Cancel
RemoveHandler Me.HelpRequested, AddressOf Me.MsgBoxHelpRequested
Return False
End Select
RemoveHandler Me.HelpRequested, AddressOf Me.MsgBoxHelpRequested
End Function
Private Function MethodName2() As Boolean
AddHandler Me.HelpRequested, AddressOf Me.MsgBoxHelpRequested2
Select Case MessageBox.Show("Text", "Title", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, 0, True)
Case Windows.Forms.DialogResult.Yes
' Do stuff
Case Windows.Forms.DialogResult.No
' Do stuff
Case Windows.Forms.DialogResult.Cancel
RemoveHandler Me.HelpRequested, AddressOf Me.MsgBoxHelpRequested2
Return False
End Select
RemoveHandler Me.HelpRequested, AddressOf Me.MsgBoxHelpRequested2
End Function
''' <summary>
''' AddHandler Me.HelpRequested, AddressOf Module1.MsgBoxHelpRequested3
''' </summary>
Private Function MethodName3() As Boolean
AddHandler Me.HelpRequested, AddressOf Module1.MsgBoxHelpRequested3
Select Case MessageBox.Show("Text", "Title", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button3, 0, True)
Case Windows.Forms.DialogResult.Yes
' Do stuff
Case Windows.Forms.DialogResult.No
' Do stuff
Case Windows.Forms.DialogResult.Cancel
RemoveHandler Me.HelpRequested, AddressOf Module1.MsgBoxHelpRequested3
Return False
End Select
RemoveHandler Me.HelpRequested, AddressOf Module1.MsgBoxHelpRequested3
End Function
Private Sub MsgBoxHelpRequested(ByVal sender As Object, ByVal hlpevent As System.Windows.Forms.HelpEventArgs)
' Breakpoint that never gets hit
MsgBox("Here I am to save the day!")
End Sub
Private Sub MsgBoxHelpRequested2(ByVal sender As Object, ByVal hlpevent As System.Windows.Forms.HelpEventArgs)
' Breakpoint that never gets hit
MsgBox("Shoot, still now working.")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MethodName() 'always works because all handles are created
End Sub
Private Sub Form1_HelpRequested(ByVal sender As System.Object, ByVal hlpevent As System.Windows.Forms.HelpEventArgs) Handles MyBase.HelpRequested
MsgBox("Always works! No need to add a handler because of Handles MyBase.HelpRequested.")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
MethodName3()
End Sub
End Class
Module Module1
Public Sub MsgBoxHelpRequested3(ByVal sender As Object, ByVal hlpevent As System.Windows.Forms.HelpEventArgs)
MsgBox("Being handled in a module.")
End Sub
End Module