3

MessageBox.Show を使用して MessageBox を表示しているフォームがあり、MessageBox の [ヘルプ] ボタンからイベントを受信して​​、独自のコードを実行できるようにしています。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 でヘルプ ボタンの押下を検出する方法は? それは、動作していないように見える同じ Microsoft コードに私を戻します。

誰でもこれで私を助けることができますか?

ありがとうございました。

4

4 に答える 4

2

Meの最初のパラメータとして渡しますMessageBox.Show

Form.ActiveFormの代わりにハンドラを追加しMeます。

于 2010-05-12T21:27:09.533 に答える
0

結局のところ、MessageBoxを呼び出すフォームとは別のアクティブなウィンドウがありました。MessageBox.Showのバージョンでは、 HelpRequestedイベントの処理と所有者の指定の両方を行うことができないため、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

他のものに関連してこのコードで修正することはまだたくさんありますが、最終的にこれを理解することは確かに素晴らしいことです。

助けてくれた皆さん、ありがとうございました。

于 2010-05-13T22:15:41.130 に答える
0

これは C# です。すぐに VB に自動変換します。

このコードをフォームの Load イベントに挿入します。

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 バージョン:

このコードをフォームの Load イベントに挿入します。

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 が呼び出されます。

于 2010-05-12T21:32:13.850 に答える
0

フォームのコンストラクター (新規) に対して、またはフォームの 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
于 2010-05-12T23:37:47.233 に答える