简体   繁体   English

按下Messagebox上的帮助按钮时未触发事件

[英]Event not firing when pressing help button on Messagebox

I have a C# app where I'm using Messagebox.Show with a help button, as per Microsoft's example at http://msdn.microsoft.com/en-us/library/szwxe9we.aspx 我有一个C#应用程序,我正在使用带有帮助按钮的Messagebox.Show,根据微软的例子http://msdn.microsoft.com/en-us/library/szwxe9we.aspx

I add the event to the form, but pressing the help button never fires the event - pressing F1 on the form DOES however. 我将事件添加到表单中,但按下帮助按钮永远不会触发事件 - 然而按表格上的F1键。 Even taking Microsoft's example almost completely does not fire the event. 即使完全采用微软的例子也不会引发这一事件。 The whole code is below. 整个代码如下。 Any ideas what I'm not doing? 我有什么想法吗?

There is another post where someone had noticed the same. 还有一个帖子 ,有人注意到了这一点。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            DialogResult r = AlertMessageWithCustomHelpWindow();
        }
        // Display a message box with a Help button. Show a custom Help window
        // by handling the HelpRequested event.
        private DialogResult AlertMessageWithCustomHelpWindow()
        {
            // Handle the HelpRequested event for the following message.
            this.HelpRequested += new System.Windows.Forms.HelpEventHandler(this.Form1_HelpRequested);

            this.Tag = "Message with Help button.";

            // Show a message box with OK and Help buttons.
            DialogResult r = MessageBox.Show("Message with Help button.",
                                              "Help Caption", MessageBoxButtons.OK,
                                              MessageBoxIcon.Question,
                                              MessageBoxDefaultButton.Button1,
                                              0, true);

            // Remove the HelpRequested event handler to keep the event
            // from being handled for other message boxes.
            this.HelpRequested -= new System.Windows.Forms.HelpEventHandler(this.Form1_HelpRequested);

            // Return the dialog box result.
            return r;
        }

        private void Form1_HelpRequested (System.Object sender, System.Windows.Forms.HelpEventArgs hlpevent)
        {
            // Create a custom Help window in response to the HelpRequested event.
            Form helpForm = new Form();

            // Set up the form position, size, and title caption.
            helpForm.StartPosition = FormStartPosition.Manual;
            helpForm.Size = new Size(200, 400);
            helpForm.DesktopLocation = new Point(this.DesktopBounds.X +
                                                  this.Size.Width,
                                                  this.DesktopBounds.Top);
            helpForm.Text = "Help Form";

            // Create a label to contain the Help text.
            Label helpLabel = new Label();

            // Add the label to the form and set its text.
            helpForm.Controls.Add(helpLabel);
            helpLabel.Dock = DockStyle.Fill;

            // Use the sender parameter to identify the context of the Help request.
            // The parameter must be cast to the Control type to get the Tag property.
            Control senderControl = sender as Control;

            helpLabel.Text = "Help information shown in response to user action on the '" +
                              (string)senderControl.Tag + "' message.";

            // Set the Help form to be owned by the main form. This helps
            // to ensure that the Help form is disposed of.
            this.AddOwnedForm(helpForm);

            // Show the custom Help window.
            helpForm.Show();

            // Indicate that the HelpRequested event is handled.
            hlpevent.Handled = true;

        }


    }
}

Take the line 走线

DialogResult r = AlertMessageWithCustomHelpWindow();

out of the Form1 constructor - maybe put it in a button click handler on the main form. 在Form1构造函数之外 - 可能将它放在主窗体上的按钮单击处理程序中。 It looks like you're blocking the UI thread with MessageBox.Show() stopping the help dialog displaying. 看起来你正在使用MessageBox.Show()阻止UI线程停止显示帮助对话框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM