我有一个使用MessageBox.Show显示MessageBox的表单,并尝试从MessageBox上的Help按钮接收事件,以便我可以执行自己的代码。 Microsoft文档显示了如何执行此操作;但是,使用建议的方法不起作用。这是我的代码的缩短版本:
Private Function MethodName() As Boolean
AddHandler Me.HelpRequested, AddressOf Me.MsgBoxHelpRequested
Select Case MessageBox.Show("Text", "Title", MessageButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, 0, True)
Case MsgBoxResult.Yes
' Do stuff
Case MsgBoxResult.No
' Do stuff
Case MsgBoxResult.Cancel
RemoveHandler Me.HelpRequested, AddressOf Me.MsgBoxHelpRequested
Return False
End Select
RemoveHandler Me.HelpRequested, AddressOf Me.MsgBoxHelpRequested
End Function
Private Sub MsgBoxHelpRequested(ByVal sender As Object, ByVal hlpevent As System.Windows.Forms.HelpEventArgs)
' Breakpoint that never gets hit
' More code
End Sub
我一直在寻找这个问题的解决方案,但我发现最好的是这个问题:How to detect Help button press in Windows Forms MessageBox?将我引回到似乎不起作用的相同Microsoft代码。< / p>
有人可以帮我吗?
谢谢。
答案 0 :(得分:2)
将 Me
作为MessageBox.Show
的第一个参数传递。
将处理程序添加到Form.ActiveForm
而不是Me
。
答案 1 :(得分:0)
这是C#,我会在一秒钟内将其自动翻译为VB。
将此代码放入表单的加载事件:
this.HelpRequested += new HelpEventHandler(Form1_HelpRequested);
然后将此代码添加到表单中:
void Form1_HelpRequested(object sender, HelpEventArgs hlpevent)
{
hlpevent.Handled = true; // this will prevent windows from also opening
// any associated help file
// do whatever you're gonna do here
}
然后像这样打电话给MessageBox
:
MessageBox.Show("message", "caption", MessageBoxButtons.OK,
MessageBoxIcon.Asterisk,
MessageBoxDefaultButton.Button1, 0, true);
这将显示一个带有OK和HELP按钮的消息框。单击HELP时,将调用Form1_HelpRequested。
VB.Net版本:
将此代码放入表单的加载事件:
AddHandler Me.HelpRequested, AddressOf Form1_HelpRequested
然后将此代码添加到表单中:
Private Sub Form1_HelpRequested(ByVal sender As Object, ByVal hlpevent As
HelpEventArgs)
' this will prevent windows from also opening
' any associated help file:
hlpevent.Handled = True
' do whatever you're gonna do here
End Sub
然后像这样打电话给MessageBox
:
MessageBox.Show("message", "caption", MessageBoxButtons.OK,
MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1, 0, _
True)
这将显示一个带有OK和HELP按钮的消息框。单击HELP时,将调用Form1_HelpRequested。
答案 2 :(得分:0)
如果为表单的构造函数(new)或表单的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
答案 3 :(得分:0)
事实证明,还有另一个窗口是Active,而不是调用MessageBox的Form。由于没有版本的MessageBox.Show允许您同时处理HelpRequested事件 AND 指定所有者,因此MessageBox正在寻找ActiveForm以获取事件的接收者,而不是将其发送到我的表单。做出以下改变终于让它发挥作用:
Private Function MethodName() As Boolean
Me.Activate() ' <-------------------!!!!!!!!!
AddHandler Me.HelpRequested, AddressOf Me.MsgBoxHelpRequested
Select Case MessageBox.Show("Text", "Title", MessageButtons.YesNoCancel, _
MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, 0, True)
Case MsgBoxResult.Yes
' Do stuff
Case MsgBoxResult.No
' Do stuff
Case MsgBoxResult.Cancel
RemoveHandler Me.HelpRequested, AddressOf Me.MsgBoxHelpRequested
Return False
End Select
RemoveHandler Me.HelpRequested, AddressOf Me.MsgBoxHelpRequested
End Function
Private Sub MsgBoxHelpRequested(ByVal sender As Object, _
ByVal hlpevent As System.Windows.Forms.HelpEventArgs)
' Breakpoint that **finally** gets hit
' More code
End Sub
我将使用与其他事物相关的代码来解决许多问题,但最终确定这一点确实很好。
感谢所有帮助过的人。