winforms - VB.Net MessageBox.Show 沒有引發HelpRequested事件

  显示原文与译文双语对照的内容
145 5

我有一個表單,它使用 MessageBox.Show, 顯示一個I,並嘗試從tmodel上的I 按鈕接收事件,以便執行我自己的。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

我一直在尋找這個問題的解決方案但我找到的最好的就是這個問題:如何檢測幫助按鈕按 Windows 窗體 MessageBox這是指我回到了同樣的微軟代碼,它似乎沒有工作。

誰能幫我這個忙?

謝謝你。

时间:原作者:0个回答

60 3

Me 作為 MessageBox.Show的第一個參數傳遞。

將處理程序添加到 Form.ActiveForm 而不是 Me

原作者:
90 4

這是 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和幫助按鈕的消息框。單擊幫助時,將調用 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和幫助按鈕的消息框。單擊幫助時,將調用 Form1_HelpRequested 。

原作者:
92 4

如果對錶單( 新建)的構造函數或者在表單事件的負載中調用,則示例代碼將不起作用。這可能就是為什麼它不能為你工作。

構造函數是新的。你必須注意構造函數或者窗體事件的載入中的一些初始化。原因是控制項的句柄,它包括窗體,但還沒有創建。如果你測試項目工作,那麼將測試項目與你所擁有的。請考慮如何調用方法和。應用程序不能工作的最可以能原因是由於未添加該處理程序,因為沒有創建表單。( 當窗體變為可見時創建) 。你可以在添加處理程序之前嘗試添加一個 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
原作者:
65 3

結果是,有另一個窗口活動的窗口比調用tmodel的窗體。since版本不允許同時處理HelpRequested事件 and 指定所有者,MessageBox 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

仍然有許多事情需要用這個代碼來修復,但最終還是很好的解決了這一問題。

感謝大家幫助。

原作者:
...