Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net MessageBox.Show未引发HelpRequested事件_Vb.net_Winforms_Events_Msgbox - Fatal编程技术网

Vb.net MessageBox.Show未引发HelpRequested事件

Vb.net MessageBox.Show未引发HelpRequested事件,vb.net,winforms,events,msgbox,Vb.net,Winforms,Events,Msgbox,我有一个表单,它使用MessageBox.Show显示MessageBox,并尝试从MessageBox上的帮助按钮接收事件,以便我可以执行自己的代码。Microsoft文档说明了如何执行此操作;然而,使用所建议的方法是行不通的。以下是我的代码的简化版本: Private Function MethodName() As Boolean AddHandler Me.HelpRequested, AddressOf Me.MsgBoxHelpRequested S

我有一个表单,它使用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
我一直在寻找这个问题的解决方案,但我发现最好的是这个问题:这让我回到了那个似乎不起作用的Microsoft代码

有人能帮我吗

谢谢。

Me
作为第一个参数传递到
MessageBox.Show

将处理程序添加到
Form.ActiveForm
而不是
Me

这是C#,我将在一秒钟内自动将其转换为VB

将此代码放入表单的加载事件中:

this.HelpRequested += new HelpEventHandler(Form1_HelpRequested);
AddHandler Me.HelpRequested, AddressOf 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
}
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);
MessageBox.Show("message", "caption", MessageBoxButtons.OK, 
    MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1, 0, _ 
    True)
这将显示一个带有“确定”和“帮助”按钮的消息框。单击“帮助”时,将调用Form1\u HelpRequested

VB.Net版本:

将此代码放入表单的加载事件中:

this.HelpRequested += new HelpEventHandler(Form1_HelpRequested);
AddHandler Me.HelpRequested, AddressOf 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
}
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);
MessageBox.Show("message", "caption", MessageBoxButtons.OK, 
    MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1, 0, _ 
    True)

这将显示一个带有“确定”和“帮助”按钮的消息框。单击“帮助”时,将调用Form1\u HelpRequested。

如果为窗体的构造函数(新建)或在窗体的加载事件中调用MethodName,则示例代码将不起作用。这可能就是它不适合你的原因

构造函数是次新的。您必须小心构造函数或表单加载事件中的某些初始化。原因是控件的句柄(包括表单)尚未创建。如果您的测试项目有效,则将测试项目与您拥有的项目进行比较。考虑一下你是如何调用方法的。应用程序不工作的最可能原因是由于未创建表单而未添加处理程序。(它是在窗体可见时创建的。您可以在添加处理程序之前尝试添加form.CreateControl。)

此外,请尝试通过设计器将处理程序添加到表单中。这将保证正确分配处理程序。(MSDN示例手动完成所有操作,但不是一个好的示例。VB示例应向您展示如何使用简单的VB方法,而不是更高级的手动方法。)

公共类表单1
继承System.Windows.Forms.Form
#“设计器生成”区域
'窗体覆盖dispose以清理组件列表。
_
受保护的重写子处置(ByVal作为布尔值处置)
尝试
如果处理AndAlso组件不是什么,那么
components.Dispose()
如果结束
最后
MyBase.Dispose(Dispose)
结束尝试
端接头
Friend WithEvents按钮2作为System.Windows.Forms.Button
'是Windows窗体设计器所必需的
作为System.ComponentModel.IContainer的专用组件
'注意:Windows窗体设计器需要以下过程
'可以使用Windows窗体设计器对其进行修改。
'不要使用代码编辑器修改它。
_
私有子初始化组件()
Me.Button1=新建System.Windows.Forms.Button
Me.Button2=新建System.Windows.Forms.Button
Me.SuspendLayout()
'
“按钮1
'
Me.Button1.Location=新系统.图纸.点(0,0)
Me.Button1.Name=“Button1”
Me.Button1.Size=新系统图纸尺寸(75,23)
Me.Button1.TabIndex=0
Me.Button1.Text=“Button1”
Me.Button1.UseVisualStyleBackColor=真
'
'按钮2
'
Me.Button2.Location=新系统图纸点(0,29)
Me.Button2.Name=“Button2”
Me.Button2.Size=新系统图纸尺寸(75,23)
Me.Button2.TabIndex=1
Me.Button2.Text=“Button2”
Me.Button2.UseVisualStyleBackColor=True
'
“表格1
'
Me.AutoScaleDimensions=新系统.Drawing.SizeF(6.0!13.0!)
Me.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize=新系统.Drawing.Size(292266)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Name=“Form1”
Me.Text=“Form1”
Me.ResumeLayout(错误)
端接头
Friend WithEvents按钮1作为System.Windows.Forms.Button
#末端区域
公共分新()
'此调用是Windows窗体设计器所必需的。
初始化组件()
'在InitializeComponent()调用之后添加任何初始化。
MethodName()在此处不起作用
端接头
私有子表单1_Load(ByVal发送方作为System.Object,ByVal e作为System.EventArgs)处理MyBase.Load
MethodName()在此处不起作用
'Me.CreateControl()
MethodName2()仍然无法工作
端接头
作为布尔值的私有函数MethodName()
AddHandler Me.HelpRequested,AddressOf Me.MsgBoxHelpRequested
选择大小写MessageBox.Show(“文本”、“标题”、MessageBoxButtons.YesNoCancel、MessageBoxIcon.Question、MessageBoxDefaultButton.Button2、0、True)
案例Windows.Forms.DialogResult.Yes
“做事
案例Windows.Forms.DialogResult.No
“做事
案例Windows.Forms.DialogResult.Cancel
RemoveHandler Me.HelpRequested,AddressOf Me.MsgBoxHelpRequested
返回错误
结束选择
RemoveHandler Me.HelpRequested,AddressOf Me.MsgBoxHelpRequested
端函数
作为布尔值的私有函数MethodName2()
AddHandler Me.HelpRequested,AddressOf Me.MsgBoxHelpRequested2
选择大小写MessageBox.Show(“文本”、“标题”、MessageBoxButtons.YesNoCancel、MessageBoxIcon.Question、MessageBoxDefaultButton.Button2、0、True)
案例Windows.Forms.DialogResult.Yes
“做事