如何使UserControl分派HelpRequest事件

4
当"HelpRequest-mode"中被点击时,如何让UserControl分派一个HelpRequest事件?
我尝试创建最简单的UserControl,只有一些有色背景。但是它无法正常工作。
已更新。
namespace SeoTools.UI.Components
{
    public partial class HelpRequestTest : UserControl
    {
        public HelpRequestTest()
        {
            InitializeComponent();
        }

        protected override void OnHelpRequested(HelpEventArgs hevent)
        {
            base.OnHelpRequested(hevent); //can't get it here either
        }
    }
}

namespace SeoTools.UI.Components
{
    partial class HelpRequestTest
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // HelpRequestTest
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackColor = System.Drawing.SystemColors.ActiveCaption;
            this.Name = "HelpRequestTest";
            this.Size = new System.Drawing.Size(114, 94);
            this.ResumeLayout(false);

        }

        #endregion
    }
}

...

private void WebHelpRequested(object sender, HelpEventArgs hlpevent)
{
    string tag = ((Control)sender).Tag.ToString();
    if (!string.IsNullOrEmpty(tag))
    {
        try
        {
            ProcessStartInfo sInfo = new ProcessStartInfo(tag);
            Process.Start(sInfo);
        }
        catch (Exception) { }
    }
    hlpevent.Handled = true;
}

...

// 
// helpRequestTest1
// 
this.helpRequestTest1.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.helpRequestTest1.Location = new System.Drawing.Point(91, 3);
this.helpRequestTest1.Name = "helpRequestTest1";
this.helpRequestTest1.Size = new System.Drawing.Size(114, 94);
this.helpRequestTest1.TabIndex = 1;
this.helpRequestTest1.Tag = "http://offerta.se";
this.helpRequestTest1.HelpRequested += new System.Windows.Forms.HelpEventHandler(this.WebHelpRequested);
1个回答

7
在最简单的形式中:
public partial class UserControl1 : UserControl {
  public UserControl1() {
    InitializeComponent();
  }
}

public partial class Form1 : Form {
  public Form1() {
    InitializeComponent();
    userControl11.Tag = "http://www.stackoverflow.com";
    userControl11.HelpRequested += userControl11_HelpRequested;
  }

  private void userControl11_HelpRequested(object sender, HelpEventArgs hlpevent) {
    string tag = ((Control)sender).Tag.ToString();
    if (!string.IsNullOrEmpty(tag)) {
      try {
        ProcessStartInfo sInfo = new ProcessStartInfo(tag);
        Process.Start(sInfo);
      }
      catch (Exception) { }
    }
    hlpevent.Handled = true;
  }
}

这在我的电脑上“原样”运行。唯一让它“停止”工作的方式是添加一个文本框控件到用户控件中,并处理它的HelpRequest事件。

public partial class UserControl1 : UserControl {
  public UserControl1() {
    InitializeComponent();
  }

  private void textBox1_HelpRequested(object sender, HelpEventArgs hlpevent) {
    //This prevents the UserControl from firing it's help request:
  }
}

目前唯一的建议是查看您的UserControl中的子控件,并查看它们是否干扰了UserControl调用事件的能力。


我无法使自定义用户控件触发 HelpRequested 事件。我已经附上了我的代码。 - Niels Bosma
2
@NielsBosma 您的代码可行。我更新了我的答案——我唯一的猜测是您UserControl中的子控件正在干扰。这是我让它“不起作用”的唯一方法。 - LarsTech

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接